@@ -173,20 +173,21 @@ func (c *Client) UpdateEnvironment(ctx context.Context) error {
173173 e := & FlagsmithAPIError {Msg : msg , Err : nil , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
174174 return c .handleError (e )
175175 }
176- c .environment .SetEnvironment (& env )
176+ c .environment .SetEnvironment (env )
177177
178178 c .log .Info ("environment updated" , "environment" , env .APIKey )
179179 return nil
180180}
181181
182182// GetIdentitySegments returns the segments that this evaluation context is a part of. It requires a local environment
183183// provided by [WithLocalEvaluation] and/or [WithOfflineHandler].
184- func (c * Client ) GetIdentitySegments (ec EvaluationContext ) ([]* segments.SegmentModel , error ) {
185- if env := c .environment .GetEnvironment (); env != nil {
186- identity := c . getIdentityModel ( ec . identifier , env . APIKey , ec . traits )
187- return flagengine . GetIdentitySegments ( env , & identity ), nil
184+ func (c * Client ) GetIdentitySegments (ec EvaluationContext ) (s []* segments.SegmentModel , err error ) {
185+ env , ok := c .environment .GetEnvironment ()
186+ if ! ok {
187+ return s , errors . New ( "GetIdentitySegments called with no local environment available" )
188188 }
189- return nil , & FlagsmithClientError {msg : "no local environment available to calculate identity segments" }
189+ identity := c .getIdentityModel (ec .identifier , env .APIKey , ec .traits )
190+ return flagengine .GetIdentitySegments (env , & identity ), nil
190191}
191192
192193// BulkIdentify can be used to create/overwrite identities(with traits) in bulk
@@ -283,8 +284,8 @@ func (c *Client) getIdentityFlagsFromAPI(ctx context.Context, identifier string,
283284}
284285
285286func (c * Client ) getEnvironmentFlagsFromEnvironment () (Flags , error ) {
286- env := c .environment .GetEnvironment ()
287- if env == nil {
287+ env , ok := c .environment .GetEnvironment ()
288+ if ! ok {
288289 return Flags {}, fmt .Errorf ("getEnvironmentFlagsFromEnvironment: no local environment is available" )
289290 }
290291 return makeFlagsFromFeatureStates (
@@ -296,8 +297,8 @@ func (c *Client) getEnvironmentFlagsFromEnvironment() (Flags, error) {
296297}
297298
298299func (c * Client ) getIdentityFlagsFromEnvironment (identifier string , traits map [string ]interface {}) (Flags , error ) {
299- env := c .environment .GetEnvironment ()
300- if env := c . environment . GetEnvironment (); env == nil {
300+ env , ok := c .environment .GetEnvironment ()
301+ if ! ok {
301302 return Flags {}, fmt .Errorf ("getIdentityFlagsFromDocument: no local environment is available" )
302303 }
303304 identity := c .getIdentityModel (identifier , env .APIKey , traits )
@@ -360,14 +361,14 @@ func (c *Client) handleError(err *FlagsmithAPIError) *FlagsmithAPIError {
360361
361362type state struct {
362363 mu sync.RWMutex
363- environment * environments.EnvironmentModel
364+ environment environments.EnvironmentModel
364365 identityOverrides sync.Map
365366}
366367
367- func (es * state ) GetEnvironment () * environments.EnvironmentModel {
368+ func (es * state ) GetEnvironment () ( environments.EnvironmentModel , bool ) {
368369 es .mu .RLock ()
369370 defer es .mu .RUnlock ()
370- return es .environment
371+ return es .environment , es . environment . APIKey != ""
371372}
372373
373374func (es * state ) GetIdentityOverride (identifier string ) (identities.IdentityModel , bool ) {
@@ -378,7 +379,7 @@ func (es *state) GetIdentityOverride(identifier string) (identities.IdentityMode
378379 return identities.IdentityModel {}, ok
379380}
380381
381- func (es * state ) SetEnvironment (env * environments.EnvironmentModel ) {
382+ func (es * state ) SetEnvironment (env environments.EnvironmentModel ) {
382383 es .mu .Lock ()
383384 defer es .mu .Unlock ()
384385 es .environment = env
0 commit comments