From 62bdbfebc192cf63e2d6cb3dbe07d117f03c8cab Mon Sep 17 00:00:00 2001 From: Jens Henneberg Date: Wed, 10 Apr 2024 14:49:10 +1200 Subject: [PATCH] Return default value when flag is not defined Signed-off-by: Jens Henneberg --- .../README.md | 3 -- .../StatsigProvider.cs | 37 +++++++++++++++---- .../StatsigProviderTest.cs | 18 ++++++--- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/OpenFeature.Contrib.Providers.Statsig/README.md b/src/OpenFeature.Contrib.Providers.Statsig/README.md index 070c5571..91e679a3 100644 --- a/src/OpenFeature.Contrib.Providers.Statsig/README.md +++ b/src/OpenFeature.Contrib.Providers.Statsig/README.md @@ -94,6 +94,3 @@ The following parameters are mapped to the corresponding Statsig pre-defined par ## Known issues and limitations - Only `ResolveBooleanValue` implemented for now - -- Gate BooleanEvaluation with default value true cannot fallback to true. - https://github.com/statsig-io/dotnet-sdk/issues/33 diff --git a/src/OpenFeature.Contrib.Providers.Statsig/StatsigProvider.cs b/src/OpenFeature.Contrib.Providers.Statsig/StatsigProvider.cs index ee9287b7..b6bff9bc 100644 --- a/src/OpenFeature.Contrib.Providers.Statsig/StatsigProvider.cs +++ b/src/OpenFeature.Contrib.Providers.Statsig/StatsigProvider.cs @@ -1,8 +1,8 @@ using OpenFeature.Constant; -using OpenFeature.Error; using OpenFeature.Model; using Statsig; using Statsig.Server; +using Statsig.Server.Evaluation; using System; using System.Threading.Tasks; @@ -46,13 +46,34 @@ public StatsigProvider(string sdkKey = null, StatsigServerOptions statsigServerO /// public override Task> ResolveBooleanValue(string flagKey, bool defaultValue, EvaluationContext context = null) { - //TODO: defaultvalue = true not yet supported due to https://github.com/statsig-io/dotnet-sdk/issues/33 - if (defaultValue == true) - throw new FeatureProviderException(ErrorType.General, "defaultvalue = true not supported (https://github.com/statsig-io/dotnet-sdk/issues/33)"); - if (GetStatus() != ProviderStatus.Ready) - return Task.FromResult(new ResolutionDetails(flagKey, defaultValue, ErrorType.ProviderNotReady)); - var result = ServerDriver.CheckGateSync(context.AsStatsigUser(), flagKey); - return Task.FromResult(new ResolutionDetails(flagKey, result)); + var result = ServerDriver.GetFeatureGate(context.AsStatsigUser(), flagKey); + var gateFound = false; + var responseType = ErrorType.None; + + switch (result.Reason) + { + case EvaluationReason.Network: + case EvaluationReason.LocalOverride: + case EvaluationReason.Bootstrap: + case EvaluationReason.DataAdapter: + gateFound = true; + break; + case EvaluationReason.Unrecognized: + responseType = ErrorType.FlagNotFound; + break; + case EvaluationReason.Uninitialized: + responseType = ErrorType.ProviderNotReady; + break; + case EvaluationReason.Unsupported: + responseType = ErrorType.InvalidContext; + break; + case EvaluationReason.Error: + responseType = ErrorType.General; + break; + case null: + break; + } + return Task.FromResult(new ResolutionDetails(flagKey, gateFound ? result.Value : defaultValue, responseType)); } /// diff --git a/test/OpenFeature.Contrib.Providers.Statsig.Test/StatsigProviderTest.cs b/test/OpenFeature.Contrib.Providers.Statsig.Test/StatsigProviderTest.cs index 73a09b5c..aaf1e23b 100644 --- a/test/OpenFeature.Contrib.Providers.Statsig.Test/StatsigProviderTest.cs +++ b/test/OpenFeature.Contrib.Providers.Statsig.Test/StatsigProviderTest.cs @@ -1,6 +1,5 @@ using AutoFixture.Xunit2; using OpenFeature.Constant; -using OpenFeature.Error; using OpenFeature.Model; using System.Threading.Tasks; using Xunit; @@ -41,25 +40,32 @@ public async Task GetBooleanValue_ForFeatureWithContext(bool flagValue, bool exp [Theory] [InlineAutoData(true, false)] [InlineAutoData(false, false)] - public async Task GetBooleanValue_ForFeatureWithNoContext_ReturnsFalse(bool flagValue, bool expectedValue, string flagName) + public async Task GetBooleanValue_ForFeatureWithNoContext_ReturnsDefaultValue(bool flagValue, bool defaultValue, string flagName) { // Arrange await statsigProvider.Initialize(null); statsigProvider.ServerDriver.OverrideGate(flagName, flagValue); // Act & Assert - Assert.Equal(expectedValue, statsigProvider.ResolveBooleanValue(flagName, false).Result.Value); + Assert.Equal(defaultValue, statsigProvider.ResolveBooleanValue(flagName, defaultValue).Result.Value); } [Theory] [AutoData] - public async Task GetBooleanValue_ForFeatureWithDefaultTrue_ThrowsException(string flagName) + [InlineAutoData(false)] + [InlineAutoData(true)] + public async Task GetBooleanValue_ForFeatureWithDefault(bool defaultValue, string flagName, string userId) { // Arrange await statsigProvider.Initialize(null); - // Act & Assert - Assert.ThrowsAny(() => statsigProvider.ResolveBooleanValue(flagName, true).Result.Value); + var ec = EvaluationContext.Builder().SetTargetingKey(userId).Build(); + + // Act + var result = await statsigProvider.ResolveBooleanValue(flagName, defaultValue, ec); + + //Assert + Assert.Equal(defaultValue, result.Value); } [Fact]