10
10
import dev .openfeature .contrib .providers .flagd .resolver .process .model .FeatureFlag ;
11
11
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .FlagStore ;
12
12
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .Storage ;
13
+ import dev .openfeature .contrib .providers .flagd .resolver .process .storage .StorageQueryResult ;
13
14
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .StorageState ;
14
15
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .StorageStateChange ;
15
16
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .Connector ;
25
26
import dev .openfeature .sdk .Value ;
26
27
import dev .openfeature .sdk .exceptions .ParseError ;
27
28
import dev .openfeature .sdk .exceptions .TypeMismatchError ;
29
+ import java .util .Map ;
28
30
import java .util .function .Consumer ;
29
31
import java .util .function .Supplier ;
30
32
import lombok .extern .slf4j .Slf4j ;
@@ -40,8 +42,8 @@ public class InProcessResolver implements Resolver {
40
42
private final Consumer <ConnectionEvent > onConnectionEvent ;
41
43
private final Operator operator ;
42
44
private final long deadline ;
43
- private final ImmutableMetadata metadata ;
44
45
private final Supplier <Boolean > connectedSupplier ;
46
+ private final String scope ;
45
47
46
48
/**
47
49
* Resolves flag values using
@@ -63,11 +65,7 @@ public InProcessResolver(
63
65
this .onConnectionEvent = onConnectionEvent ;
64
66
this .operator = new Operator ();
65
67
this .connectedSupplier = connectedSupplier ;
66
- this .metadata = options .getSelector () == null
67
- ? null
68
- : ImmutableMetadata .builder ()
69
- .addString ("scope" , options .getSelector ())
70
- .build ();
68
+ this .scope = options .getSelector ();
71
69
}
72
70
73
71
/**
@@ -167,13 +165,15 @@ static Connector getConnector(final FlagdOptions options, Consumer<ConnectionEve
167
165
}
168
166
169
167
private <T > ProviderEvaluation <T > resolve (Class <T > type , String key , EvaluationContext ctx ) {
170
- final FeatureFlag flag = flagStore .getFlag (key );
168
+ final StorageQueryResult storageQueryResult = flagStore .getFlag (key );
169
+ final FeatureFlag flag = storageQueryResult .getFeatureFlag ();
171
170
172
171
// missing flag
173
172
if (flag == null ) {
174
173
return ProviderEvaluation .<T >builder ()
175
174
.errorMessage ("flag: " + key + " not found" )
176
175
.errorCode (ErrorCode .FLAG_NOT_FOUND )
176
+ .flagMetadata (getFlagMetadata (storageQueryResult ))
177
177
.build ();
178
178
}
179
179
@@ -182,6 +182,7 @@ private <T> ProviderEvaluation<T> resolve(Class<T> type, String key, EvaluationC
182
182
return ProviderEvaluation .<T >builder ()
183
183
.errorMessage ("flag: " + key + " is disabled" )
184
184
.errorCode (ErrorCode .FLAG_NOT_FOUND )
185
+ .flagMetadata (getFlagMetadata (storageQueryResult ))
185
186
.build ();
186
187
}
187
188
@@ -228,13 +229,59 @@ private <T> ProviderEvaluation<T> resolve(Class<T> type, String key, EvaluationC
228
229
throw new TypeMismatchError (message );
229
230
}
230
231
231
- final ProviderEvaluation . ProviderEvaluationBuilder < T > evaluationBuilder = ProviderEvaluation .<T >builder ()
232
+ return ProviderEvaluation .<T >builder ()
232
233
.value ((T ) value )
233
234
.variant (resolvedVariant )
234
- .reason (reason );
235
+ .reason (reason )
236
+ .flagMetadata (getFlagMetadata (storageQueryResult ))
237
+ .build ();
238
+ }
239
+
240
+ private ImmutableMetadata getFlagMetadata (StorageQueryResult storageQueryResult ) {
241
+ ImmutableMetadata .ImmutableMetadataBuilder metadataBuilder = ImmutableMetadata .builder ();
242
+ for (Map .Entry <String , Object > entry :
243
+ storageQueryResult .getFlagSetMetadata ().entrySet ()) {
244
+ addEntryToMetadataBuilder (metadataBuilder , entry .getKey (), entry .getValue ());
245
+ }
235
246
236
- return this .metadata == null
237
- ? evaluationBuilder .build ()
238
- : evaluationBuilder .flagMetadata (this .metadata ).build ();
247
+ if (scope != null ) {
248
+ metadataBuilder .addString ("scope" , scope );
249
+ }
250
+
251
+ FeatureFlag flag = storageQueryResult .getFeatureFlag ();
252
+ if (flag != null ) {
253
+ for (Map .Entry <String , Object > entry : flag .getMetadata ().entrySet ()) {
254
+ addEntryToMetadataBuilder (metadataBuilder , entry .getKey (), entry .getValue ());
255
+ }
256
+ }
257
+
258
+ return metadataBuilder .build ();
259
+ }
260
+
261
+ private void addEntryToMetadataBuilder (
262
+ ImmutableMetadata .ImmutableMetadataBuilder metadataBuilder , String key , Object value ) {
263
+ if (value instanceof Number ) {
264
+ if (value instanceof Long ) {
265
+ metadataBuilder .addLong (key , (Long ) value );
266
+ return ;
267
+ } else if (value instanceof Double ) {
268
+ metadataBuilder .addDouble (key , (Double ) value );
269
+ return ;
270
+ } else if (value instanceof Integer ) {
271
+ metadataBuilder .addInteger (key , (Integer ) value );
272
+ return ;
273
+ } else if (value instanceof Float ) {
274
+ metadataBuilder .addFloat (key , (Float ) value );
275
+ return ;
276
+ }
277
+ } else if (value instanceof Boolean ) {
278
+ metadataBuilder .addBoolean (key , (Boolean ) value );
279
+ return ;
280
+ } else if (value instanceof String ) {
281
+ metadataBuilder .addString (key , (String ) value );
282
+ return ;
283
+ }
284
+ throw new IllegalArgumentException (
285
+ "The type of the Metadata entry with key " + key + " and value " + value + " is not supported" );
239
286
}
240
287
}
0 commit comments