-
Notifications
You must be signed in to change notification settings - Fork 55
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
Feat/dependent feature toggles #140
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
41102d8
feat: dependable toggles
FredrikOseberg b521b27
fix: clean up isEnabled
FredrikOseberg b2f726b
feat: add test for parent metrics
FredrikOseberg e01a9cd
fix: add warning
FredrikOseberg ec6bd84
feat: add custom every function
FredrikOseberg b80180c
fix: format
FredrikOseberg cb2b29e
fix: test for every
FredrikOseberg f200237
fix: remove comments
FredrikOseberg 5a8ba9f
fix: avoid panicing
FredrikOseberg e228c44
fix: improve error message
FredrikOseberg 0d06ff3
fix: rename dependencies
FredrikOseberg 94ece70
fix: add more test cases
FredrikOseberg 921b3f8
fix: finish last test
FredrikOseberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,31 +254,25 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate | |
o(&opts) | ||
} | ||
|
||
var f *api.Feature | ||
if opts.resolver != nil { | ||
f = opts.resolver(feature) | ||
} else { | ||
f = uc.repository.getToggle(feature) | ||
} | ||
f := resolveToggle(uc, opts, feature) | ||
|
||
ctx := uc.staticContext | ||
if opts.ctx != nil { | ||
ctx = ctx.Override(*opts.ctx) | ||
} | ||
|
||
if f == nil { | ||
if opts.fallbackFunc != nil { | ||
return api.StrategyResult{ | ||
Enabled: opts.fallbackFunc(feature, ctx), | ||
} | ||
} else if opts.fallback != nil { | ||
return handleFallback(opts, feature, ctx) | ||
} | ||
|
||
if f.Dependencies != nil && len(*f.Dependencies) > 0 { | ||
dependenciesSatisfied := uc.isParentDependencySatisfied(f, *ctx) | ||
|
||
if !dependenciesSatisfied { | ||
return api.StrategyResult{ | ||
Enabled: *opts.fallback, | ||
Enabled: false, | ||
} | ||
} | ||
return api.StrategyResult{ | ||
Enabled: false, | ||
} | ||
} | ||
|
||
if !f.Enabled { | ||
|
@@ -345,6 +339,44 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate | |
} | ||
} | ||
|
||
func (uc *Client) isParentDependencySatisfied(feature *api.Feature, context context.Context) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless we write an every helper, we can't return out here or we will exit the entire function without properly looping over all the dependencies. |
||
warnOnce := &WarnOnce{} | ||
|
||
dependenciesSatisfied := func(parent api.Dependency) bool { | ||
parentToggle := uc.repository.getToggle(parent.Feature) | ||
|
||
if parentToggle == nil { | ||
warnOnce.Warn("the parent toggle was not found in the cache, the evaluation of this dependency will always be false") | ||
return false | ||
} | ||
|
||
if parentToggle.Dependencies != nil && len(*parentToggle.Dependencies) > 0 { | ||
return false | ||
} | ||
|
||
// According to the schema, if the enabled property is absent we assume it's true. | ||
if parent.Enabled == nil { | ||
if parent.Variants != nil && len(*parent.Variants) > 0 { | ||
variantName := uc.getVariantWithoutMetrics(parent.Feature, WithVariantContext(context)).Name | ||
return contains(*parent.Variants, variantName) | ||
} | ||
return uc.isEnabled(parent.Feature, WithContext(context)).Enabled | ||
} | ||
|
||
return !uc.isEnabled(parent.Feature, WithContext(context)).Enabled | ||
} | ||
|
||
allDependenciesSatisfied := every(*feature.Dependencies, func(parent interface{}) bool { | ||
return dependenciesSatisfied(parent.(api.Dependency)) | ||
}) | ||
|
||
if !allDependenciesSatisfied { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// GetVariant queries a variant as the specified feature is enabled. | ||
// | ||
// It is safe to call this method from multiple goroutines concurrently. | ||
|
@@ -483,3 +515,30 @@ func (uc *Client) WaitForReady() { | |
func (uc *Client) ListFeatures() []api.Feature { | ||
return uc.repository.list() | ||
} | ||
|
||
func resolveToggle(unleashClient *Client, opts featureOption, featureName string) *api.Feature { | ||
var feature *api.Feature | ||
if opts.resolver != nil { | ||
feature = opts.resolver(featureName) | ||
} else { | ||
feature = unleashClient.repository.getToggle(featureName) | ||
} | ||
|
||
return feature | ||
} | ||
|
||
func handleFallback(opts featureOption, featureName string, ctx *context.Context) api.StrategyResult { | ||
if opts.fallbackFunc != nil { | ||
return api.StrategyResult{ | ||
Enabled: opts.fallbackFunc(featureName, ctx), | ||
} | ||
} else if opts.fallback != nil { | ||
return api.StrategyResult{ | ||
Enabled: *opts.fallback, | ||
} | ||
} | ||
|
||
return api.StrategyResult{ | ||
Enabled: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why you decided to do this check outside the isParentDependencySatisfied. I made the opposite decision but didn't have a strong opinion here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me it made more sense readability wise. If I don't have dependencies or the length of dependencies is 0 then I don't need to do check if
isParentDependencySatisfied(f, *ctx)
. Moving it inside that function obfuscates the check that determines whether or not that function should actually be run. I don't have strong opinions about this either, but I found it easier to reason about this way because now the function is limited to doing one thing and one thing only.