9
9
import dev .openfeature .contrib .providers .flagd .resolver .process .model .FeatureFlag ;
10
10
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .FlagStore ;
11
11
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .Storage ;
12
+ import dev .openfeature .contrib .providers .flagd .resolver .process .storage .StorageQueryResult ;
12
13
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .StorageStateChange ;
13
14
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .Connector ;
14
15
import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .file .FileConnector ;
@@ -46,7 +47,7 @@ public class InProcessResolver implements Resolver {
46
47
* Resolves flag values using https://buf.build/open-feature/flagd/docs/main:flagd.sync.v1. Flags
47
48
* are evaluated locally.
48
49
*
49
- * @param options flagd options
50
+ * @param options flagd options
50
51
* @param connectedSupplier lambda providing current connection status from caller
51
52
* @param onConnectionEvent lambda which handles changes in the connection/stream
52
53
*/
@@ -161,14 +162,15 @@ static Connector getConnector(final FlagdOptions options) {
161
162
}
162
163
163
164
private <T > ProviderEvaluation <T > resolve (Class <T > type , String key , EvaluationContext ctx ) {
164
- final FeatureFlag flag = flagStore .getFlag (key );
165
+ final StorageQueryResult storageQueryResult = flagStore .getFlag (key );
166
+ final FeatureFlag flag = storageQueryResult .getFeatureFlag ();
165
167
166
168
// missing flag
167
169
if (flag == null ) {
168
170
return ProviderEvaluation .<T >builder ()
169
171
.errorMessage ("flag: " + key + " not found" )
170
172
.errorCode (ErrorCode .FLAG_NOT_FOUND )
171
- .flagMetadata (fallBackMetadata )
173
+ .flagMetadata (getFlagMetadata ( storageQueryResult ) )
172
174
.build ();
173
175
}
174
176
@@ -177,7 +179,7 @@ private <T> ProviderEvaluation<T> resolve(Class<T> type, String key, EvaluationC
177
179
return ProviderEvaluation .<T >builder ()
178
180
.errorMessage ("flag: " + key + " is disabled" )
179
181
.errorCode (ErrorCode .FLAG_NOT_FOUND )
180
- .flagMetadata (getFlagMetadata (flag ))
182
+ .flagMetadata (getFlagMetadata (storageQueryResult ))
181
183
.build ();
182
184
}
183
185
@@ -228,47 +230,55 @@ private <T> ProviderEvaluation<T> resolve(Class<T> type, String key, EvaluationC
228
230
.value ((T ) value )
229
231
.variant (resolvedVariant )
230
232
.reason (reason )
231
- .flagMetadata (getFlagMetadata (flag ))
233
+ .flagMetadata (getFlagMetadata (storageQueryResult ))
232
234
.build ();
233
235
}
234
236
235
- private ImmutableMetadata getFlagMetadata (FeatureFlag flag ) {
236
- if (flag == null ) {
237
- return fallBackMetadata ;
237
+ private ImmutableMetadata getFlagMetadata (StorageQueryResult storageQueryResult ) {
238
+ ImmutableMetadata .ImmutableMetadataBuilder metadataBuilder = ImmutableMetadata .builder ();
239
+ for (Map .Entry <String , Object > entry :
240
+ storageQueryResult .getGlobalFlagMetadata ().entrySet ()) {
241
+ addEntryToMetadataBuilder (metadataBuilder , entry .getKey (), entry .getValue ());
238
242
}
239
243
240
- ImmutableMetadata .ImmutableMetadataBuilder metadataBuilder = ImmutableMetadata .builder ();
241
244
if (scope != null ) {
242
245
metadataBuilder .addString ("scope" , scope );
243
246
}
244
247
245
- for (Map .Entry <String , Object > entry : flag .getMetadata ().entrySet ()) {
246
- Object value = entry .getValue ();
247
- if (value instanceof Number ) {
248
- if (value instanceof Long ) {
249
- metadataBuilder .addLong (entry .getKey (), (Long ) value );
250
- continue ;
251
- } else if (value instanceof Double ) {
252
- metadataBuilder .addDouble (entry .getKey (), (Double ) value );
253
- continue ;
254
- } else if (value instanceof Integer ) {
255
- metadataBuilder .addInteger (entry .getKey (), (Integer ) value );
256
- continue ;
257
- } else if (value instanceof Float ) {
258
- metadataBuilder .addFloat (entry .getKey (), (Float ) value );
259
- continue ;
260
- }
261
- } else if (value instanceof Boolean ) {
262
- metadataBuilder .addBoolean (entry .getKey (), (Boolean ) value );
263
- continue ;
264
- } else if (value instanceof String ) {
265
- metadataBuilder .addString (entry .getKey (), (String ) value );
266
- continue ;
248
+ FeatureFlag flag = storageQueryResult .getFeatureFlag ();
249
+ if (flag != null ) {
250
+ for (Map .Entry <String , Object > entry : flag .getMetadata ().entrySet ()) {
251
+ addEntryToMetadataBuilder (metadataBuilder , entry .getKey (), entry .getValue ());
267
252
}
268
- throw new IllegalArgumentException ("The type of the Metadata entry with key " + entry .getKey ()
269
- + " and value " + entry .getValue () + " is not supported" );
270
253
}
271
254
272
255
return metadataBuilder .build ();
273
256
}
257
+
258
+ private void addEntryToMetadataBuilder (
259
+ ImmutableMetadata .ImmutableMetadataBuilder metadataBuilder , String key , Object value ) {
260
+ if (value instanceof Number ) {
261
+ if (value instanceof Long ) {
262
+ metadataBuilder .addLong (key , (Long ) value );
263
+ return ;
264
+ } else if (value instanceof Double ) {
265
+ metadataBuilder .addDouble (key , (Double ) value );
266
+ return ;
267
+ } else if (value instanceof Integer ) {
268
+ metadataBuilder .addInteger (key , (Integer ) value );
269
+ return ;
270
+ } else if (value instanceof Float ) {
271
+ metadataBuilder .addFloat (key , (Float ) value );
272
+ return ;
273
+ }
274
+ } else if (value instanceof Boolean ) {
275
+ metadataBuilder .addBoolean (key , (Boolean ) value );
276
+ return ;
277
+ } else if (value instanceof String ) {
278
+ metadataBuilder .addString (key , (String ) value );
279
+ return ;
280
+ }
281
+ throw new IllegalArgumentException (
282
+ "The type of the Metadata entry with key " + key + " and value " + value + " is not supported" );
283
+ }
274
284
}
0 commit comments