Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v4' into bumpToSupportedGoVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Aug 7, 2024
2 parents 8680485 + 5066446 commit ea2b7b5
Show file tree
Hide file tree
Showing 15 changed files with 559 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Build
on:
push:
branches:
- main
- v4

jobs:
build:
Expand Down
59 changes: 29 additions & 30 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
const (
deprecatedSuffix = "/features"
clientName = "unleash-client-go"
clientVersion = "4.0.0"
clientVersion = "4.1.2"
specVersion = "4.3.1"
)

var defaultStrategies = []strategy.Strategy{
Expand Down Expand Up @@ -253,12 +254,13 @@ func (uc *Client) IsEnabled(feature string, options ...FeatureOption) (enabled b
uc.metrics.count(feature, enabled)
}()

return uc.isEnabled(feature, options...).Enabled
result, _ := uc.isEnabled(feature, options...)
return result.Enabled
}

// isEnabled abstracts away the details of checking if a toggle is turned on or off
// without metrics
func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.StrategyResult {
func (uc *Client) isEnabled(feature string, options ...FeatureOption) (api.StrategyResult, *api.Feature) {
var opts featureOption
for _, o := range options {
o(&opts)
Expand All @@ -272,7 +274,7 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate
}

if f == nil {
return handleFallback(opts, feature, ctx)
return handleFallback(opts, feature, ctx), nil
}

if f.Dependencies != nil && len(*f.Dependencies) > 0 {
Expand All @@ -281,20 +283,20 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate
if !dependenciesSatisfied {
return api.StrategyResult{
Enabled: false,
}
}, f
}
}

if !f.Enabled {
return api.StrategyResult{
Enabled: false,
}
}, f
}

if len(f.Strategies) == 0 {
return api.StrategyResult{
Enabled: f.Enabled,
}
}, f
}

for _, s := range f.Strategies {
Expand All @@ -310,7 +312,7 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate
uc.errors <- err
return api.StrategyResult{
Enabled: false,
}
}, f
}

allConstraints := make([]api.Constraint, 0)
Expand All @@ -326,7 +328,7 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate
if !ok {
return api.StrategyResult{
Enabled: false,
}
}, f
}

return api.StrategyResult{
Expand All @@ -335,18 +337,18 @@ func (uc *Client) isEnabled(feature string, options ...FeatureOption) api.Strate
GroupId: groupId,
Variants: s.Variants,
}.GetVariant(ctx),
}
}, f
} else {
return api.StrategyResult{
Enabled: true,
}
}, f
}
}
}

return api.StrategyResult{
Enabled: false,
}
}, f
}

func (uc *Client) isParentDependencySatisfied(feature *api.Feature, context context.Context) bool {
Expand All @@ -364,7 +366,7 @@ func (uc *Client) isParentDependencySatisfied(feature *api.Feature, context cont
return false
}

enabledResult := uc.isEnabled(parent.Feature, WithContext(context))
enabledResult, _ := uc.isEnabled(parent.Feature, WithContext(context))
// According to the schema, if the enabled property is absent we assume it's true.
if parent.Enabled == nil || *parent.Enabled {
if parent.Variants != nil && len(*parent.Variants) > 0 && enabledResult.Variant != nil {
Expand Down Expand Up @@ -408,43 +410,40 @@ func (uc *Client) getVariantWithoutMetrics(feature string, options ...VariantOpt
}

var strategyResult api.StrategyResult

if opts.resolver != nil {
strategyResult = uc.isEnabled(feature, WithContext(*ctx), WithResolver(opts.resolver))
} else {
strategyResult = uc.isEnabled(feature, WithContext(*ctx))
}

if !strategyResult.Enabled {
return defaultVariant
}

var f *api.Feature
if opts.resolver != nil {
f = opts.resolver(feature)
strategyResult, f = uc.isEnabled(feature, WithContext(*ctx), WithResolver(opts.resolver))
} else {
f = uc.repository.getToggle(feature)
strategyResult, f = uc.isEnabled(feature, WithContext(*ctx))
}

if f == nil {
getFallbackVariant := func(featureEnabled bool) *api.Variant {
if opts.variantFallbackFunc != nil {
return opts.variantFallbackFunc(feature, ctx)
} else if opts.variantFallback != nil {
return opts.variantFallback
}

if featureEnabled {
return disabledVariantFeatureEnabled
}
return defaultVariant
}

if !f.Enabled {
return defaultVariant
if !strategyResult.Enabled {
return getFallbackVariant(false)
}

if f == nil || !f.Enabled {
return getFallbackVariant(false)
}

if strategyResult.Variant != nil {
return strategyResult.Variant
}

if len(f.Variants) == 0 {
return disabledVariantFeatureEnabled
return getFallbackVariant(true)
}

return api.VariantCollection{
Expand Down
Loading

0 comments on commit ea2b7b5

Please sign in to comment.