Skip to content

Commit

Permalink
feat!: consolidated configuration change events into one event
Browse files Browse the repository at this point in the history
Signed-off-by: Skye Gill <gill.skye95@gmail.com>
  • Loading branch information
skyerus committed Jan 3, 2023
1 parent af6f133 commit f308986
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
15 changes: 15 additions & 0 deletions pkg/eval/ievaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ func (s *StateChangeNotification) ToMap() map[string]interface{} {
"flagKey": s.FlagKey,
}
}

func StateChangeNotificationsToMap(scns []StateChangeNotification) map[string]interface{} {
m := make(map[string]interface{}, len(scns))

for _, scn := range scns {
m[scn.FlagKey] = map[string]interface{}{
"type": string(scn.Type),
"source": scn.Source,
}
}

return map[string]interface{}{
"flags": m,
}
}
58 changes: 36 additions & 22 deletions pkg/eval/json_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,35 +739,40 @@ func BenchmarkResolveObjectValue(b *testing.B) {
func TestMergeFlags(t *testing.T) {
t.Parallel()
tests := []struct {
name string
current eval.Flags
new eval.Flags
newSource string
want eval.Flags
name string
current eval.Flags
new eval.Flags
newSource string
want eval.Flags
wantNotifs []eval.StateChangeNotification
}{
{
name: "both nil",
current: eval.Flags{Flags: nil},
new: eval.Flags{Flags: nil},
want: eval.Flags{Flags: map[string]eval.Flag{}},
name: "both nil",
current: eval.Flags{Flags: nil},
new: eval.Flags{Flags: nil},
want: eval.Flags{Flags: map[string]eval.Flag{}},
wantNotifs: []eval.StateChangeNotification{},
},
{
name: "both empty flags",
current: eval.Flags{Flags: map[string]eval.Flag{}},
new: eval.Flags{Flags: map[string]eval.Flag{}},
want: eval.Flags{Flags: map[string]eval.Flag{}},
name: "both empty flags",
current: eval.Flags{Flags: map[string]eval.Flag{}},
new: eval.Flags{Flags: map[string]eval.Flag{}},
want: eval.Flags{Flags: map[string]eval.Flag{}},
wantNotifs: []eval.StateChangeNotification{},
},
{
name: "empty current",
current: eval.Flags{Flags: nil},
new: eval.Flags{Flags: map[string]eval.Flag{}},
want: eval.Flags{Flags: map[string]eval.Flag{}},
name: "empty current",
current: eval.Flags{Flags: nil},
new: eval.Flags{Flags: map[string]eval.Flag{}},
want: eval.Flags{Flags: map[string]eval.Flag{}},
wantNotifs: []eval.StateChangeNotification{},
},
{
name: "empty new",
current: eval.Flags{Flags: map[string]eval.Flag{}},
new: eval.Flags{Flags: nil},
want: eval.Flags{Flags: map[string]eval.Flag{}},
name: "empty new",
current: eval.Flags{Flags: map[string]eval.Flag{}},
new: eval.Flags{Flags: nil},
want: eval.Flags{Flags: map[string]eval.Flag{}},
wantNotifs: []eval.StateChangeNotification{},
},
{
name: "extra fields on each",
Expand All @@ -793,6 +798,9 @@ func TestMergeFlags(t *testing.T) {
Source: "2",
},
}},
wantNotifs: []eval.StateChangeNotification{
{Type: "write", Source: "2", FlagKey: "paka"},
},
},
{
name: "override",
Expand All @@ -807,6 +815,10 @@ func TestMergeFlags(t *testing.T) {
"waka": {DefaultVariant: "on"},
"paka": {DefaultVariant: "on"},
}},
wantNotifs: []eval.StateChangeNotification{
{Type: "update", FlagKey: "waka"},
{Type: "write", FlagKey: "paka"},
},
},
{
name: "identical",
Expand All @@ -819,15 +831,17 @@ func TestMergeFlags(t *testing.T) {
want: eval.Flags{Flags: map[string]eval.Flag{
"hello": {DefaultVariant: "off"},
}},
wantNotifs: []eval.StateChangeNotification{},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, _ := tt.current.Merge(logger.NewLogger(nil, false), tt.newSource, tt.new)
got, gotNotifs := tt.current.Merge(logger.NewLogger(nil, false), tt.newSource, tt.new)
require.Equal(t, tt.want, got)
require.Equal(t, tt.wantNotifs, gotNotifs)
})
}
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ func (r *Runtime) updateState(ctx context.Context, syncr sync.ISync) error {
if err != nil {
return fmt.Errorf("set state: %w", err)
}
for _, n := range notifications {
r.Logger.Info(fmt.Sprintf("configuration change (%s) for flagKey %s (%s)", n.Type, n.FlagKey, n.Source))
r.Service.Notify(service.Notification{
Type: service.ConfigurationChange,
Data: n.ToMap(),
})
}

r.Service.Notify(service.Notification{
Type: service.ConfigurationChange,
Data: eval.StateChangeNotificationsToMap(notifications),
})
return nil
}

0 comments on commit f308986

Please sign in to comment.