@@ -173,20 +173,21 @@ func (c *Client) UpdateEnvironment(ctx context.Context) error {
173
173
e := & FlagsmithAPIError {Msg : msg , Err : nil , ResponseStatusCode : resp .StatusCode (), ResponseStatus : resp .Status ()}
174
174
return c .handleError (e )
175
175
}
176
- c .environment .SetEnvironment (& env )
176
+ c .environment .SetEnvironment (env )
177
177
178
178
c .log .Info ("environment updated" , "environment" , env .APIKey )
179
179
return nil
180
180
}
181
181
182
182
// GetIdentitySegments returns the segments that this evaluation context is a part of. It requires a local environment
183
183
// 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" )
188
188
}
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
190
191
}
191
192
192
193
// 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,
283
284
}
284
285
285
286
func (c * Client ) getEnvironmentFlagsFromEnvironment () (Flags , error ) {
286
- env := c .environment .GetEnvironment ()
287
- if env == nil {
287
+ env , ok := c .environment .GetEnvironment ()
288
+ if ! ok {
288
289
return Flags {}, fmt .Errorf ("getEnvironmentFlagsFromEnvironment: no local environment is available" )
289
290
}
290
291
return makeFlagsFromFeatureStates (
@@ -296,8 +297,8 @@ func (c *Client) getEnvironmentFlagsFromEnvironment() (Flags, error) {
296
297
}
297
298
298
299
func (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 {
301
302
return Flags {}, fmt .Errorf ("getIdentityFlagsFromDocument: no local environment is available" )
302
303
}
303
304
identity := c .getIdentityModel (identifier , env .APIKey , traits )
@@ -360,14 +361,14 @@ func (c *Client) handleError(err *FlagsmithAPIError) *FlagsmithAPIError {
360
361
361
362
type state struct {
362
363
mu sync.RWMutex
363
- environment * environments.EnvironmentModel
364
+ environment environments.EnvironmentModel
364
365
identityOverrides sync.Map
365
366
}
366
367
367
- func (es * state ) GetEnvironment () * environments.EnvironmentModel {
368
+ func (es * state ) GetEnvironment () ( environments.EnvironmentModel , bool ) {
368
369
es .mu .RLock ()
369
370
defer es .mu .RUnlock ()
370
- return es .environment
371
+ return es .environment , es . environment . APIKey != ""
371
372
}
372
373
373
374
func (es * state ) GetIdentityOverride (identifier string ) (identities.IdentityModel , bool ) {
@@ -378,7 +379,7 @@ func (es *state) GetIdentityOverride(identifier string) (identities.IdentityMode
378
379
return identities.IdentityModel {}, ok
379
380
}
380
381
381
- func (es * state ) SetEnvironment (env * environments.EnvironmentModel ) {
382
+ func (es * state ) SetEnvironment (env environments.EnvironmentModel ) {
382
383
es .mu .Lock ()
383
384
defer es .mu .Unlock ()
384
385
es .environment = env
0 commit comments