Skip to content
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

fix: Use new Statsig Api to return default value when flag is not defined #177

Merged
merged 1 commit into from
Apr 16, 2024
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
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
Loading