Skip to content

fix: IntEvalution, FloatEvalution when disabled or missing flags #610

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

Merged
merged 1 commit into from
Jan 9, 2025
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: 17 additions & 2 deletions providers/unleash/pkg/demo_app_toggles.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
],
"variants": [
{
"name": "aaaa",
"name": "int-flag-variant",
"weight": 1000,
"payload": {
"type": "number",
Expand All @@ -148,7 +148,7 @@
],
"variants": [
{
"name": "aaaa",
"name": "double-flag-variant",
"weight": 1000,
"payload": {
"type": "number",
Expand All @@ -160,6 +160,21 @@
}
]
},
{
"name": "disabled-flag",
"type": "release",
"enabled": false,
"variants": [
{
"name": "disabled-flag-variant",
"weight": 1000,
"payload": {
"type": "string",
"value": "disabled flag variant value"
}
}
]
},
{
"name": "DateExample",
"type": "release",
Expand Down
16 changes: 16 additions & 0 deletions providers/unleash/pkg/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ func (p *Provider) BooleanEvaluation(ctx context.Context, flag string, defaultVa

func (p *Provider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, evalCtx of.FlattenedContext) of.FloatResolutionDetail {
res := p.ObjectEvaluation(ctx, flag, defaultValue, evalCtx)

if value, ok := res.Value.(float64); ok {
return of.FloatResolutionDetail{
Value: value,
ProviderResolutionDetail: res.ProviderResolutionDetail,
}
}

if strValue, ok := res.Value.(string); ok {
value, err := strconv.ParseFloat(strValue, 64)
if err == nil {
Expand All @@ -127,6 +135,14 @@ func (p *Provider) FloatEvaluation(ctx context.Context, flag string, defaultValu

func (p *Provider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, evalCtx of.FlattenedContext) of.IntResolutionDetail {
res := p.ObjectEvaluation(ctx, flag, defaultValue, evalCtx)

if value, ok := res.Value.(int64); ok {
return of.IntResolutionDetail{
Value: value,
ProviderResolutionDetail: res.ProviderResolutionDetail,
}
}

if strValue, ok := res.Value.(string); ok {
value, err := strconv.ParseInt(strValue, 10, 64)
if err == nil {
Expand Down
73 changes: 57 additions & 16 deletions providers/unleash/pkg/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,63 @@ func TestBooleanEvaluation(t *testing.T) {

func TestIntEvaluation(t *testing.T) {
defaultValue := int64(0)
expectedValue := int64(123)
resolution := provider.IntEvaluation(context.Background(), "int-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
if !enabled {
t.Fatalf("Expected feature to be enabled")
}
if resolution.ProviderResolutionDetail.Variant != "aaaa" {
t.Fatalf("Expected variant name")
}
if resolution.Value != expectedValue {
t.Fatalf("Expected one of the variant payloads")
}

t.Run("evalCtx empty", func(t *testing.T) {
resolution := provider.IntEvaluation(context.Background(), "non-existing-flag", defaultValue, nil)
t.Run("int-flag", func(t *testing.T) {
resolution := provider.IntEvaluation(context.Background(), "int-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
require.True(t, enabled)
require.Equal(t, "int-flag-variant", resolution.ProviderResolutionDetail.Variant)
require.Equal(t, int64(123), resolution.Value)
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
})

t.Run("disabled-flag", func(t *testing.T) {
resolution := provider.IntEvaluation(context.Background(), "disabled-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
require.False(t, enabled)
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
require.Equal(t, defaultValue, resolution.Value)
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
})

t.Run("evalCtx empty fallback to default", func(t *testing.T) {
t.Run("non-existing-flag", func(t *testing.T) {
resolution := provider.IntEvaluation(context.Background(), "non-existing-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
require.False(t, enabled)
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
require.Equal(t, defaultValue, resolution.Value)
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
})
}

func TestFloatEvaluation(t *testing.T) {
defaultValue := 0.0

t.Run("int-flag", func(t *testing.T) {
resolution := provider.FloatEvaluation(context.Background(), "double-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
require.True(t, enabled)
require.Equal(t, "double-flag-variant", resolution.ProviderResolutionDetail.Variant)
require.Equal(t, 1.23, resolution.Value)
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
})

t.Run("disabled-flag", func(t *testing.T) {
resolution := provider.FloatEvaluation(context.Background(), "disabled-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
require.False(t, enabled)
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
require.Equal(t, defaultValue, resolution.Value)
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
})

t.Run("non-existing-flag", func(t *testing.T) {
resolution := provider.FloatEvaluation(context.Background(), "non-existing-flag", defaultValue, nil)
enabled, _ := resolution.ProviderResolutionDetail.FlagMetadata.GetBool("enabled")
require.False(t, enabled)
require.Equal(t, "", resolution.ProviderResolutionDetail.Variant)
require.Equal(t, defaultValue, resolution.Value)
require.Equal(t, of.ErrorCode(""), resolution.ResolutionDetail().ErrorCode)
})
}

Expand Down Expand Up @@ -228,13 +264,17 @@ func TestMain(m *testing.M) {
}
defer demoReader.Close()

appName := "my-application"
backupFile := fmt.Sprintf("unleash-repo-schema-v1-%s.json", appName)

providerOptions := unleashProvider.ProviderConfig{
Options: []unleash.ConfigOption{
unleash.WithListener(&unleash.DebugListener{}),
unleash.WithAppName("my-application"),
unleash.WithAppName(appName),
unleash.WithRefreshInterval(5 * time.Second),
unleash.WithMetricsInterval(5 * time.Second),
unleash.WithStorage(&unleash.BootstrapStorage{Reader: demoReader}),
unleash.WithBackupPath("./"),
unleash.WithUrl("https://localhost:4242"),
},
}
Expand All @@ -258,5 +298,6 @@ func TestMain(m *testing.M) {

cleanup()

os.Remove(backupFile)
os.Exit(exitCode)
}
Loading