Skip to content

Commit 04df666

Browse files
authored
feat: Statsig provider evaluate boolean updates (#691)
Signed-off-by: liran2000 <[email protected]>
1 parent 66d19e2 commit 04df666

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

providers/statsig/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
<!-- x-release-please-end-version -->
1919

2020
## Concepts
21-
* Boolean evaluation gets [gate](https://docs.statsig.com/server/javaSdk#checking-a-gate) status.
2221
* String/Integer/Double evaluations evaluation gets [Dynamic config](https://docs.statsig.com/server/javaSdk#reading-a-dynamic-config) or [Layer](https://docs.statsig.com/server/javaSdk#getting-an-layerexperiment) evaluation.
2322
As the key represents an inner attribute, feature config is required as a parameter with data needed for evaluation.
2423
For an example of dynamic config of product alias, need to differentiate between dynamic config or layer, and the dynamic config name.
24+
* Boolean evaluation gets [gate](https://docs.statsig.com/server/javaSdk#checking-a-gate) status when feature config is not passed.
25+
When feature config exists, it evaluates to the config/layer attribute, similar to String/Integer/Float evaluations.
2526
* Object evaluation gets a structure representing the dynamic config or layer.
2627
* [Private Attributes](https://docs.statsig.com/server/javaSdk#private-attributes) are supported as 'privateAttributes' context key.
2728

@@ -71,3 +72,8 @@ Unit test based on Statsig [Local Overrides](https://docs.statsig.com/server/jav
7172
As it is limited, evaluation context based tests are limited.
7273
See [statsigProviderTest](./src/test/java/dev/openfeature/contrib/providers/statsig/StatsigProviderTest.java)
7374
for more information.
75+
76+
## Known issues
77+
- Gate BooleanEvaluation with default value true cannot fallback to true.
78+
https://github.com/statsig-io/java-server-sdk/issues/22
79+

providers/statsig/src/main/java/dev/openfeature/contrib/providers/statsig/StatsigProvider.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,27 @@ public Metadata getMetadata() {
8585
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
8686
verifyEvaluation();
8787
StatsigUser user = ContextTransformer.transform(ctx);
88-
Future<Boolean> featureOn = Statsig.checkGateAsync(user, key);
89-
Boolean evaluatedValue = featureOn.get();
88+
Boolean evaluatedValue = defaultValue;
89+
try {
90+
FeatureConfig featureConfig = parseFeatureConfig(ctx);
91+
switch (featureConfig.getType()) {
92+
case CONFIG:
93+
DynamicConfig dynamicConfig = fetchDynamicConfig(user, featureConfig);
94+
evaluatedValue = dynamicConfig.getBoolean(key, defaultValue);
95+
break;
96+
case LAYER:
97+
Layer layer = fetchLayer(user, featureConfig);
98+
evaluatedValue = layer.getBoolean(key, defaultValue);
99+
break;
100+
default:
101+
break;
102+
}
103+
} catch (Exception e) {
104+
log.debug("could not fetch feature config. checking gate {}.", key);
105+
Future<Boolean> featureOn = Statsig.checkGateAsync(user, key);
106+
evaluatedValue = featureOn.get();
107+
}
108+
90109
return ProviderEvaluation.<Boolean>builder()
91110
.value(evaluatedValue)
92111
.build();

providers/statsig/src/test/java/dev/openfeature/contrib/providers/statsig/StatsigProviderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static void setUp() {
7575
private static void buildFlags() {
7676
Statsig.overrideGate(FLAG_NAME, true);
7777
Map<String, Object> configMap = new HashMap<>();
78+
configMap.put("boolean", true);
7879
configMap.put("alias", "test");
7980
configMap.put("revision", INT_FLAG_VALUE);
8081
configMap.put("price", DOUBLE_FLAG_VALUE);
@@ -118,6 +119,17 @@ void getBooleanEvaluation() {
118119
assertEquals(true, client.getBooleanValue(FLAG_NAME, false));
119120
assertEquals(false, statsigProvider.getBooleanEvaluation("non-existing", false, new ImmutableContext()).getValue());
120121
assertEquals(false, client.getBooleanValue("non-existing", false));
122+
123+
// expected to succeed when https://github.com/statsig-io/java-server-sdk/issues/22 is resolved and adopted
124+
// assertEquals(true, client.getBooleanValue("non-existing", true));
125+
126+
MutableContext evaluationContext = new MutableContext();
127+
MutableContext featureConfig = new MutableContext();
128+
featureConfig.add("type", "CONFIG");
129+
featureConfig.add("name", "product");
130+
evaluationContext.add("feature_config", featureConfig);
131+
assertEquals(true, statsigProvider.getBooleanEvaluation("boolean", false,
132+
evaluationContext).getValue());
121133
}
122134

123135
@Test

0 commit comments

Comments
 (0)