@@ -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.
4545func (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
5575type 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.
7399func (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}
0 commit comments