Skip to content

Commit c745edc

Browse files
feat: map to Statsig CustomIDs (#210)
Signed-off-by: Jens Henneberg <[email protected]>
1 parent 24818e7 commit c745edc

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

.github/workflows/dotnet-format.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
- name: Check out code
2121
uses: actions/checkout@v4
2222

23-
- name: Setup .NET Core 6.0
23+
- name: Setup .NET
2424
uses: actions/setup-dotnet@v4
2525
with:
26-
dotnet-version: 6.0.x
26+
global-json-file: global.json
2727

2828
- name: Install format tool
2929
run: dotnet tool install -g dotnet-format

src/OpenFeature.Contrib.Providers.Statsig/EvaluationContextExtensions.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OpenFeature.Model;
1+
using OpenFeature.Error;
2+
using OpenFeature.Model;
23
using Statsig;
34

45
namespace OpenFeature.Contrib.Providers.Statsig
@@ -14,6 +15,7 @@ internal static class EvaluationContextExtensions
1415
internal const string CONTEXT_LOCALE = "locale";
1516
internal const string CONTEXT_USER_AGENT = "userAgent";
1617
internal const string CONTEXT_PRIVATE_ATTRIBUTES = "privateAttributes";
18+
internal const string CONTEXT_CUSTOM_IDS = "customIDs";
1719

1820
public static StatsigUser AsStatsigUser(this EvaluationContext evaluationContext)
1921
{
@@ -49,7 +51,19 @@ public static StatsigUser AsStatsigUser(this EvaluationContext evaluationContext
4951
var privateAttributes = item.Value.AsStructure;
5052
foreach (var items in privateAttributes)
5153
{
52-
user.AddPrivateAttribute(items.Key, items.Value);
54+
user.AddPrivateAttribute(items.Key, items.Value.AsObject);
55+
}
56+
}
57+
break;
58+
case CONTEXT_CUSTOM_IDS:
59+
if (item.Value.IsStructure)
60+
{
61+
var customIds = item.Value.AsStructure;
62+
foreach (var customId in customIds)
63+
{
64+
if (customId.Value.IsString)
65+
user.AddCustomID(customId.Key, customId.Value.AsString);
66+
else throw new FeatureProviderException(Constant.ErrorType.TypeMismatch, "Only string values are supported for CustomIDs");
5367
}
5468
}
5569
break;

src/OpenFeature.Contrib.Providers.Statsig/OpenFeature.Contrib.Providers.Statsig.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<PackageId>OpenFeature.Contrib.Providers.Statsig</PackageId>
55
<VersionNumber>0.0.5</VersionNumber><!--x-release-please-version -->
66
<VersionPrefix>$(VersionNumber)</VersionPrefix>
7-
<VersionSuffix>preview</VersionSuffix>
87
<AssemblyVersion>$(VersionNumber)</AssemblyVersion>
98
<FileVersion>$(VersionNumber)</FileVersion>
109
<Description>Statsig provider for .NET</Description>

test/OpenFeature.Contrib.Providers.Statsig.Test/EvaluationContextExtensionsTests.cs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using AutoFixture.Xunit2;
2+
using OpenFeature.Error;
23
using OpenFeature.Model;
34
using System.Collections.Generic;
5+
using System.Linq;
46
using Xunit;
57

68
namespace OpenFeature.Contrib.Providers.Statsig.Test
@@ -80,7 +82,37 @@ public void AsStatsigUser_ShouldMapPrivateData(string key, string value)
8082
// Assert
8183
Assert.NotNull(statsigUser);
8284
Assert.True(statsigUser.PrivateAttributes.TryGetValue(key, out var mappedValue));
83-
Assert.Equal(value, (mappedValue as Value)?.AsString);
85+
Assert.Equal(value, mappedValue);
86+
}
87+
88+
[Theory]
89+
[AutoData]
90+
public void AsStatsigUser_ShouldMapCustomIds(Dictionary<string, string> customIdKeyValues)
91+
{
92+
93+
// Arrange
94+
var customIdStructure = new Structure(customIdKeyValues.ToDictionary(kvp => kvp.Key, kvp => new Value(kvp.Value)));
95+
var evaluationContext = EvaluationContext.Builder().Set(EvaluationContextExtensions.CONTEXT_CUSTOM_IDS, customIdStructure).Build();
96+
97+
// Act
98+
var statsigUser = evaluationContext.AsStatsigUser();
99+
100+
// Assert
101+
Assert.NotNull(statsigUser);
102+
var result = statsigUser.CustomIDs.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
103+
Assert.Equal(customIdKeyValues, result);
104+
}
105+
106+
[Theory]
107+
[AutoData]
108+
public void AsStatsigUser_ShouldFailOnNonStringCustomId(Dictionary<string, int> customIdKeyValues)
109+
{
110+
// Arrange
111+
var customIdStructure = new Structure(customIdKeyValues.ToDictionary(kvp => kvp.Key, kvp => new Value(kvp.Value)));
112+
var evaluationContext = EvaluationContext.Builder().Set(EvaluationContextExtensions.CONTEXT_CUSTOM_IDS, customIdStructure).Build();
113+
114+
// Act and Assert
115+
Assert.Throws<FeatureProviderException>(evaluationContext.AsStatsigUser);
84116
}
85117

86118
[Fact]

0 commit comments

Comments
 (0)