Skip to content

Commit c1ef5e1

Browse files
committed
fix client-side patch event representation
1 parent ebf147a commit c1ef5e1

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

ldservices/client_sdk_data.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,37 @@ func (f FlagValueData) Event() string {
4343
// Data is for the eventsource.Event interface. It provides the marshalled data in the format used by the streaming
4444
// service.
4545
func (f FlagValueData) Data() string {
46-
bytes, _ := json.Marshal(f)
47-
return string(bytes)
46+
return string(f.ToJSON(true))
4847
}
4948

50-
// ClientSDKData is a set of flag value data as provided by the client-side SDK endpoints.
51-
//
52-
// It also implements the eventsource.Event interface, simulating a "put" event for the streaming service.
53-
type ClientSDKData map[string]flagValueDataJSON
49+
// ToJSON returns the JSON representation of the flag data.
50+
func (f FlagValueData) ToJSON(withKey bool) []byte {
51+
d := flagValueDataJSON{
52+
Version: f.Version,
53+
FlagVersion: f.FlagVersion,
54+
Value: f.Value,
55+
}
56+
if withKey {
57+
d.Key = &f.Key
58+
}
59+
if f.VariationIndex >= 0 {
60+
d.VariationIndex = &f.VariationIndex
61+
}
62+
if !f.Reason.IsNull() {
63+
d.Reason = &f.Reason
64+
}
65+
if f.TrackEvents {
66+
d.TrackEvents = &f.TrackEvents
67+
}
68+
if f.DebugEventsUntilDate > 0 {
69+
d.DebugEventsUntilDate = &f.DebugEventsUntilDate
70+
}
71+
json, _ := json.Marshal(d)
72+
return json
73+
}
5474

5575
type flagValueDataJSON struct {
76+
Key *string `json:"key,omitempty"`
5677
Version int `json:"version"`
5778
FlagVersion int `json:"flagVersion"`
5879
Value ldvalue.Value `json:"value"`
@@ -62,6 +83,11 @@ type flagValueDataJSON struct {
6283
DebugEventsUntilDate *uint64 `json:"debugEventsUntilDate,omitempty"`
6384
}
6485

86+
// ClientSDKData is a set of flag value data as provided by the client-side SDK endpoints.
87+
//
88+
// It also implements the eventsource.Event interface, simulating a "put" event for the streaming service.
89+
type ClientSDKData map[string]json.RawMessage
90+
6591
// NewClientSDKData creates a ClientSDKData instance.
6692
//
6793
// This constructor is provided in case we ever change the implementation to be something other than just a map.
@@ -72,25 +98,7 @@ func NewClientSDKData() ClientSDKData {
7298
// Flags adds the specified items to the flags map.
7399
func (c ClientSDKData) Flags(flags ...FlagValueData) ClientSDKData {
74100
for _, flag := range flags {
75-
f := flag
76-
d := flagValueDataJSON{
77-
Version: f.Version,
78-
FlagVersion: f.FlagVersion,
79-
Value: f.Value,
80-
}
81-
if f.VariationIndex >= 0 {
82-
d.VariationIndex = &f.VariationIndex
83-
}
84-
if !f.Reason.IsNull() {
85-
d.Reason = &f.Reason
86-
}
87-
if f.TrackEvents {
88-
d.TrackEvents = &f.TrackEvents
89-
}
90-
if f.DebugEventsUntilDate > 0 {
91-
d.DebugEventsUntilDate = &f.DebugEventsUntilDate
92-
}
93-
c[f.Key] = d
101+
c[flag.Key] = flag.ToJSON(false)
94102
}
95103
return c
96104
}

ldservices/client_sdk_data_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12+
func TestFlagValuePatchEvent(t *testing.T) {
13+
f := FlagValueData{Key: "flagkey", Version: 1, FlagVersion: 1000, Value: ldvalue.Bool(true), VariationIndex: 1}
14+
15+
bytes1 := f.ToJSON(true)
16+
expectedJSON1 := `{"key": "flagkey", "version": 1, "flagVersion": 1000, "value": true, "variation": 1}`
17+
assertJSONEqual(t, expectedJSON1, string(bytes1))
18+
19+
bytes2 := f.ToJSON(false)
20+
expectedJSON2 := `{"version": 1, "flagVersion": 1000, "value": true, "variation": 1}`
21+
assertJSONEqual(t, expectedJSON2, string(bytes2))
22+
}
23+
1224
func TestEmptyClientSDKData(t *testing.T) {
1325
expectedJSON := `{}`
1426
data := NewClientSDKData()

0 commit comments

Comments
 (0)