Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ForcedDecision): remove config-ready check from forced-decision apis #328

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions pkg/client/optimizely_user_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ func (o *OptimizelyUserContext) TrackEvent(eventKey string, eventTags map[string
// SetForcedDecision sets the forced decision (variation key) for a given decision context (flag key and optional rule key).
// returns true if the forced decision has been set successfully.
func (o *OptimizelyUserContext) SetForcedDecision(context pkgDecision.OptimizelyDecisionContext, decision pkgDecision.OptimizelyForcedDecision) bool {
if _, err := o.optimizely.getProjectConfig(); err != nil {
o.optimizely.logger.Error("Optimizely instance is not valid, failing setForcedDecision call.", err)
return false
}
if o.forcedDecisionService == nil {
o.forcedDecisionService = pkgDecision.NewForcedDecisionService(o.GetUserID())
}
Expand All @@ -133,23 +129,14 @@ func (o *OptimizelyUserContext) SetForcedDecision(context pkgDecision.Optimizely

// GetForcedDecision returns the forced decision for a given flag and an optional rule
func (o *OptimizelyUserContext) GetForcedDecision(context pkgDecision.OptimizelyDecisionContext) (pkgDecision.OptimizelyForcedDecision, error) {
forcedDecision := pkgDecision.OptimizelyForcedDecision{}
if _, err := o.optimizely.getProjectConfig(); err != nil {
o.optimizely.logger.Error("Optimizely instance is not valid, failing getForcedDecision call.", err)
return forcedDecision, err
}
if o.forcedDecisionService == nil {
return forcedDecision, errors.New("decision not found")
return pkgDecision.OptimizelyForcedDecision{}, errors.New("decision not found")
}
return o.forcedDecisionService.GetForcedDecision(context)
}

// RemoveForcedDecision removes the forced decision for a given flag and an optional rule.
func (o *OptimizelyUserContext) RemoveForcedDecision(context pkgDecision.OptimizelyDecisionContext) bool {
if _, err := o.optimizely.getProjectConfig(); err != nil {
o.optimizely.logger.Error("Optimizely instance is not valid, failing removeForcedDecision call.", err)
return false
}
if o.forcedDecisionService == nil {
return false
}
Expand All @@ -158,10 +145,6 @@ func (o *OptimizelyUserContext) RemoveForcedDecision(context pkgDecision.Optimiz

// RemoveAllForcedDecisions removes all forced decisions bound to this user context.
func (o *OptimizelyUserContext) RemoveAllForcedDecisions() bool {
if _, err := o.optimizely.getProjectConfig(); err != nil {
o.optimizely.logger.Error("Optimizely instance is not valid, failing removeForcedDecision call.", err)
return false
}
if o.forcedDecisionService == nil {
return true
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/optimizely_user_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,14 +1063,14 @@ func (s *OptimizelyUserContextTestSuite) TestForcedDecisionWithNilConfig() {
user := s.OptimizelyClient.CreateUserContext(s.userID, nil)
s.Nil(user.forcedDecisionService)

s.False(user.SetForcedDecision(decision.OptimizelyDecisionContext{FlagKey: flagKeyA, RuleKey: ruleKey}, decision.OptimizelyForcedDecision{VariationKey: variationKeyA}))
s.Nil(user.forcedDecisionService)
s.True(user.SetForcedDecision(decision.OptimizelyDecisionContext{FlagKey: flagKeyA, RuleKey: ruleKey}, decision.OptimizelyForcedDecision{VariationKey: variationKeyA}))
s.NotNil(user.forcedDecisionService)

forcedDecision, err := user.GetForcedDecision(decision.OptimizelyDecisionContext{FlagKey: flagKeyA, RuleKey: ruleKey})
s.Equal("", forcedDecision.VariationKey)
s.Error(err)
s.False(user.RemoveForcedDecision(decision.OptimizelyDecisionContext{FlagKey: flagKeyA, RuleKey: ruleKey}))
s.False(user.RemoveAllForcedDecisions())
s.Equal(variationKeyA, forcedDecision.VariationKey)
s.NoError(err)
s.True(user.RemoveForcedDecision(decision.OptimizelyDecisionContext{FlagKey: flagKeyA, RuleKey: ruleKey}))
s.True(user.RemoveAllForcedDecisions())
}

func (s *OptimizelyUserContextTestSuite) TestForcedDecision() {
Expand Down