66 "fmt"
77 "log/slog"
88 "strings"
9- "sync"
109 "time"
1110
1211 "github.com/Flagsmith/flagsmith-go-client/v4/flagengine"
@@ -34,7 +33,7 @@ type Client struct {
3433 ctxLocalEval context.Context
3534 ctxAnalytics context.Context
3635
37- environment state
36+ state environmentState
3837
3938 analyticsProcessor * AnalyticsProcessor
4039 log * slog.Logger
@@ -78,7 +77,7 @@ func NewClient(apiKey string, options ...Option) (*Client, error) {
7877 }
7978 }
8079
81- if c .environment .IsOffline () {
80+ if c .state .IsOffline () {
8281 return c , nil
8382 }
8483 if c .defaultFlagHandler == nil && apiKey == "" {
@@ -174,7 +173,7 @@ func (c *Client) UpdateEnvironment(ctx context.Context) error {
174173 e := & APIError {response : resp .RawResponse }
175174 return c .handleError (e )
176175 }
177- c .environment .SetEnvironment (env )
176+ c .state .SetEnvironment (& env )
178177
179178 c .log .Info ("environment updated" , "environment" , env .APIKey )
180179 return nil
@@ -183,12 +182,12 @@ func (c *Client) UpdateEnvironment(ctx context.Context) error {
183182// GetIdentitySegments returns the segments that this evaluation context is a part of. It requires a local environment
184183// provided by [WithLocalEvaluation] and/or [WithOfflineHandler].
185184func (c * Client ) GetIdentitySegments (ec EvaluationContext ) (s []* segments.SegmentModel , err error ) {
186- env , ok := c .environment .GetEnvironment ()
185+ env , ok := c .state .GetEnvironment ()
187186 if ! ok {
188187 return s , errors .New ("GetIdentitySegments called with no local environment available" )
189188 }
190189 identity := c .getIdentityModel (ec .identifier , env .APIKey , ec .traits )
191- return flagengine .GetIdentitySegments (env , & identity ), nil
190+ return flagengine .GetIdentitySegments (env , identity ), nil
192191}
193192
194193// BulkIdentify can be used to create/overwrite identities(with traits) in bulk
@@ -219,7 +218,7 @@ func (c *Client) BulkIdentify(ctx context.Context, batch []*IdentityTraits) erro
219218
220219// GetEnvironmentFlags calls GetFlags for the default EvaluationContext.
221220func (c * Client ) GetEnvironmentFlags (ctx context.Context ) (f Flags , err error ) {
222- if c .environment .IsOffline () || c .localEvaluation {
221+ if c .state .IsOffline () || c .localEvaluation {
223222 f , err = c .getEnvironmentFlagsFromEnvironment ()
224223 } else {
225224 f , err = c .getEnvironmentFlagsFromAPI (ctx )
@@ -229,7 +228,7 @@ func (c *Client) GetEnvironmentFlags(ctx context.Context) (f Flags, err error) {
229228
230229// GetIdentityFlags calls GetFlags using this identifier and traits as the EvaluationContext.
231230func (c * Client ) GetIdentityFlags (ctx context.Context , identifier string , traits map [string ]interface {}) (f Flags , err error ) {
232- if c .environment .IsOffline () || c .localEvaluation {
231+ if c .state .IsOffline () || c .localEvaluation {
233232 f , err = c .getIdentityFlagsFromEnvironment (identifier , traits )
234233 } else {
235234 f , err = c .getIdentityFlagsFromAPI (ctx , identifier , traits )
@@ -240,7 +239,6 @@ func (c *Client) GetIdentityFlags(ctx context.Context, identifier string, traits
240239// getEnvironmentFlagsFromAPI tries to contact the Flagsmith API to get the latest environment data.
241240func (c * Client ) getEnvironmentFlagsFromAPI (ctx context.Context ) (Flags , error ) {
242241 req := c .client .NewRequest ()
243- fmt .Println (c .baseURL )
244242 resp , err := req .
245243 SetContext (ctx ).
246244 ForceContentType ("application/json" ).
@@ -282,7 +280,7 @@ func (c *Client) getIdentityFlagsFromAPI(ctx context.Context, identifier string,
282280}
283281
284282func (c * Client ) getEnvironmentFlagsFromEnvironment () (Flags , error ) {
285- env , ok := c .environment .GetEnvironment ()
283+ env , ok := c .state .GetEnvironment ()
286284 if ! ok {
287285 return Flags {}, fmt .Errorf ("getEnvironmentFlagsFromEnvironment: no local environment is available" )
288286 }
@@ -295,12 +293,12 @@ func (c *Client) getEnvironmentFlagsFromEnvironment() (Flags, error) {
295293}
296294
297295func (c * Client ) getIdentityFlagsFromEnvironment (identifier string , traits map [string ]interface {}) (Flags , error ) {
298- env , ok := c .environment .GetEnvironment ()
296+ env , ok := c .state .GetEnvironment ()
299297 if ! ok {
300298 return Flags {}, fmt .Errorf ("getIdentityFlagsFromDocument: no local environment is available" )
301299 }
302300 identity := c .getIdentityModel (identifier , env .APIKey , traits )
303- featureStates := flagengine .GetIdentityFeatureStates (env , & identity )
301+ featureStates := flagengine .GetIdentityFeatureStates (env , identity )
304302 flags := makeFlagsFromFeatureStates (
305303 featureStates ,
306304 c .analyticsProcessor ,
@@ -332,18 +330,18 @@ func (c *Client) pollEnvironment(ctx context.Context) {
332330 }
333331}
334332
335- func (c * Client ) getIdentityModel (identifier string , apiKey string , traits map [string ]interface {}) identities.IdentityModel {
333+ func (c * Client ) getIdentityModel (identifier string , apiKey string , traits map [string ]interface {}) * identities.IdentityModel {
336334 identityTraits := make ([]* enginetraits.TraitModel , 0 , len (traits ))
337335 for k , v := range traits {
338336 identityTraits = append (identityTraits , enginetraits .NewTrait (k , v ))
339337 }
340338
341- if identity , ok := c .environment .GetIdentityOverride (identifier ); ok {
339+ if identity , ok := c .state .GetIdentityOverride (identifier ); ok {
342340 identity .IdentityTraits = identityTraits
343341 return identity
344342 }
345343
346- return identities.IdentityModel {
344+ return & identities.IdentityModel {
347345 Identifier : identifier ,
348346 IdentityTraits : identityTraits ,
349347 EnvironmentAPIKey : apiKey ,
@@ -356,43 +354,3 @@ func (c *Client) handleError(err *APIError) *APIError {
356354 }
357355 return err
358356}
359-
360- type state struct {
361- environment environments.EnvironmentModel
362- offline bool
363- mu sync.RWMutex
364-
365- identityOverrides sync.Map
366- }
367-
368- func (es * state ) GetEnvironment () (environments.EnvironmentModel , bool ) {
369- es .mu .RLock ()
370- defer es .mu .RUnlock ()
371- return es .environment , es .environment .APIKey != ""
372- }
373-
374- func (es * state ) GetIdentityOverride (identifier string ) (identities.IdentityModel , bool ) {
375- i , ok := es .identityOverrides .Load (identifier )
376- if ok && i != nil {
377- return i .(identities.IdentityModel ), true
378- }
379- return identities.IdentityModel {}, ok
380- }
381-
382- func (es * state ) SetEnvironment (env environments.EnvironmentModel ) {
383- es .mu .Lock ()
384- defer es .mu .Unlock ()
385- es .environment = env
386- for _ , id := range env .IdentityOverrides {
387- es .identityOverrides .Store (id .Identifier , * id )
388- }
389- }
390-
391- func (es * state ) SetOfflineEnvironment (env environments.EnvironmentModel ) {
392- es .SetEnvironment (env )
393- es .offline = true
394- }
395-
396- func (es * state ) IsOffline () bool {
397- return es .offline
398- }
0 commit comments