Skip to content

Commit

Permalink
fix: Use new Statsig Api to return default value when flag is not def…
Browse files Browse the repository at this point in the history
…ined (#177)

Signed-off-by: Jens Henneberg <[email protected]>
  • Loading branch information
jenshenneberg authored Apr 16, 2024
1 parent 5ff0e5f commit 5efc8a6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/OpenFeature.Contrib.Providers.Statsig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 29 additions & 8 deletions src/OpenFeature.Contrib.Providers.Statsig/StatsigProvider.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -46,13 +46,34 @@ public StatsigProvider(string sdkKey = null, StatsigServerOptions statsigServerO
/// <inheritdoc/>
public override Task<ResolutionDetails<bool>> 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<bool>(flagKey, defaultValue, ErrorType.ProviderNotReady));
var result = ServerDriver.CheckGateSync(context.AsStatsigUser(), flagKey);
return Task.FromResult(new ResolutionDetails<bool>(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<bool>(flagKey, gateFound ? result.Value : defaultValue, responseType));
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using AutoFixture.Xunit2;
using OpenFeature.Constant;
using OpenFeature.Error;
using OpenFeature.Model;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -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<FeatureProviderException>(() => 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]
Expand Down

0 comments on commit 5efc8a6

Please sign in to comment.