Skip to content

Commit 67f0950

Browse files
committed
flagd utilize initialization context for evals
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent 66d7dd7 commit 67f0950

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdProvider.java

+16-6
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public class FlagdProvider extends EventProvider implements FeatureProvider {
2828

2929
private final ReadWriteLock lock = new ReentrantReadWriteLock();
3030
private final Resolver flagResolver;
31-
3231
private ProviderState state = ProviderState.NOT_READY;
3332

33+
private EvaluationContext evaluationContext;
34+
3435
/**
3536
* Create a new FlagdProvider instance with default options.
3637
*/
@@ -60,6 +61,7 @@ public FlagdProvider(final FlagdOptions options) {
6061

6162
@Override
6263
public void initialize(EvaluationContext evaluationContext) throws Exception {
64+
this.evaluationContext = evaluationContext;
6365
this.flagResolver.init();
6466
}
6567

@@ -91,27 +93,35 @@ public Metadata getMetadata() {
9193

9294
@Override
9395
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
94-
return this.flagResolver.booleanEvaluation(key, defaultValue, ctx);
96+
return this.flagResolver.booleanEvaluation(key, defaultValue, mergeContext(ctx));
9597
}
9698

9799
@Override
98100
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
99-
return this.flagResolver.stringEvaluation(key, defaultValue, ctx);
101+
return this.flagResolver.stringEvaluation(key, defaultValue, mergeContext(ctx));
100102
}
101103

102104
@Override
103105
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) {
104-
return this.flagResolver.doubleEvaluation(key, defaultValue, ctx);
106+
return this.flagResolver.doubleEvaluation(key, defaultValue, mergeContext(ctx));
105107
}
106108

107109
@Override
108110
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
109-
return this.flagResolver.integerEvaluation(key, defaultValue, ctx);
111+
return this.flagResolver.integerEvaluation(key, defaultValue, mergeContext(ctx));
110112
}
111113

112114
@Override
113115
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) {
114-
return this.flagResolver.objectEvaluation(key, defaultValue, ctx);
116+
return this.flagResolver.objectEvaluation(key, defaultValue, mergeContext(ctx));
117+
}
118+
119+
private EvaluationContext mergeContext(final EvaluationContext clientCallCtx) {
120+
if (this.evaluationContext != null) {
121+
return evaluationContext.merge(clientCallCtx);
122+
}
123+
124+
return clientCallCtx;
115125
}
116126

117127
private void setState(ProviderState newState) {

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/FlagdProviderTest.java

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package dev.openfeature.contrib.providers.flagd;
22

33
import com.google.protobuf.Struct;
4-
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.Cache;
5-
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.CacheType;
4+
import dev.openfeature.contrib.providers.flagd.resolver.Resolver;
65
import dev.openfeature.contrib.providers.flagd.resolver.grpc.GrpcConnector;
76
import dev.openfeature.contrib.providers.flagd.resolver.grpc.GrpcResolver;
7+
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.Cache;
8+
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.CacheType;
89
import dev.openfeature.flagd.grpc.Schema.EventStreamResponse;
910
import dev.openfeature.flagd.grpc.Schema.ResolveBooleanRequest;
1011
import dev.openfeature.flagd.grpc.Schema.ResolveBooleanResponse;
@@ -16,6 +17,7 @@
1617
import dev.openfeature.flagd.grpc.ServiceGrpc.ServiceBlockingStub;
1718
import dev.openfeature.flagd.grpc.ServiceGrpc.ServiceStub;
1819
import dev.openfeature.sdk.FlagEvaluationDetails;
20+
import dev.openfeature.sdk.ImmutableContext;
1921
import dev.openfeature.sdk.ImmutableMetadata;
2022
import dev.openfeature.sdk.MutableContext;
2123
import dev.openfeature.sdk.MutableStructure;
@@ -48,6 +50,7 @@
4850
import static org.mockito.ArgumentMatchers.argThat;
4951
import static org.mockito.Mockito.mock;
5052
import static org.mockito.Mockito.mockStatic;
53+
import static org.mockito.Mockito.verify;
5154
import static org.mockito.Mockito.when;
5255

5356
class FlagdProviderTest {
@@ -763,6 +766,39 @@ void disabled_cache() {
763766
assertEquals(STATIC_REASON, objectDetails.getReason());
764767
}
765768

769+
@Test
770+
void contextMerging() throws Exception {
771+
// given
772+
final FlagdProvider provider = new FlagdProvider();
773+
774+
final Resolver resolverMock = mock(Resolver.class);
775+
776+
Field flagResolver = FlagdProvider.class.getDeclaredField("flagResolver");
777+
flagResolver.setAccessible(true);
778+
flagResolver.set(provider, resolverMock);
779+
780+
final HashMap<String, Value> globalCtxMap = new HashMap<>();
781+
globalCtxMap.put("id", new Value("GlobalID"));
782+
globalCtxMap.put("env", new Value("A"));
783+
784+
final HashMap<String, Value> localCtxMap = new HashMap<>();
785+
localCtxMap.put("id", new Value("localID"));
786+
localCtxMap.put("client", new Value("999"));
787+
788+
final HashMap<String, Value> expectedCtx = new HashMap<>();
789+
expectedCtx.put("id", new Value("localID"));
790+
expectedCtx.put("env", new Value("A"));
791+
localCtxMap.put("client", new Value("999"));
792+
793+
// when
794+
provider.initialize(new ImmutableContext(globalCtxMap));
795+
provider.getBooleanEvaluation("ket", false, new ImmutableContext(localCtxMap));
796+
797+
// then
798+
verify(resolverMock).booleanEvaluation(any(), any(), argThat(
799+
ctx -> ctx.asMap().entrySet().containsAll(expectedCtx.entrySet())));
800+
}
801+
766802
// test utils
767803

768804
// create provider with given grpc connector

0 commit comments

Comments
 (0)