@@ -40,7 +40,11 @@ public class FliptProvider extends EventProvider {
4040 @ Getter
4141 private FliptClient fliptClient ;
4242
43- private AtomicBoolean isInitialized = new AtomicBoolean (false );
43+ @ Setter (AccessLevel .PROTECTED )
44+ @ Getter
45+ private ProviderState state = ProviderState .NOT_READY ;
46+
47+ private final AtomicBoolean isInitialized = new AtomicBoolean (false );
4448
4549 /**
4650 * Constructor.
@@ -96,7 +100,8 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
96100
97101 @ Override
98102 public ProviderEvaluation <String > getStringEvaluation (String key , String defaultValue , EvaluationContext ctx ) {
99- ProviderEvaluation <Value > valueProviderEvaluation = getObjectEvaluation (key , new Value (defaultValue ), ctx );
103+ ProviderEvaluation <Value > valueProviderEvaluation =
104+ evaluateVariant (String .class , key , new Value (defaultValue ), ctx );
100105 return ProviderEvaluation .<String >builder ()
101106 .value (valueProviderEvaluation .getValue ().asString ())
102107 .variant (valueProviderEvaluation .getVariant ())
@@ -108,7 +113,8 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default
108113
109114 @ Override
110115 public ProviderEvaluation <Integer > getIntegerEvaluation (String key , Integer defaultValue , EvaluationContext ctx ) {
111- ProviderEvaluation <Value > valueProviderEvaluation = getObjectEvaluation (key , new Value (defaultValue ), ctx );
116+ ProviderEvaluation <Value > valueProviderEvaluation =
117+ evaluateVariant (Integer .class , key , new Value (defaultValue ), ctx );
112118 Integer value = getIntegerValue (valueProviderEvaluation , defaultValue );
113119 return ProviderEvaluation .<Integer >builder ()
114120 .value (value )
@@ -130,7 +136,8 @@ private static Integer getIntegerValue(ProviderEvaluation<Value> valueProviderEv
130136
131137 @ Override
132138 public ProviderEvaluation <Double > getDoubleEvaluation (String key , Double defaultValue , EvaluationContext ctx ) {
133- ProviderEvaluation <Value > valueProviderEvaluation = getObjectEvaluation (key , new Value (defaultValue ), ctx );
139+ ProviderEvaluation <Value > valueProviderEvaluation =
140+ evaluateVariant (Double .class , key , new Value (defaultValue ), ctx );
134141 Double value = getDoubleValue (valueProviderEvaluation , defaultValue );
135142 return ProviderEvaluation .<Double >builder ()
136143 .value (value )
@@ -152,6 +159,18 @@ private static Double getDoubleValue(ProviderEvaluation<Value> valueProviderEval
152159
153160 @ Override
154161 public ProviderEvaluation <Value > getObjectEvaluation (String key , Value defaultValue , EvaluationContext ctx ) {
162+ return evaluateVariant (Value .class , key , defaultValue , ctx );
163+ }
164+
165+ private <T > ProviderEvaluation <Value > evaluateVariant (Class <T > clazz , String key , Value defaultValue ,
166+ EvaluationContext ctx ) {
167+ if (!ProviderState .READY .equals (state )) {
168+ if (ProviderState .NOT_READY .equals (state )) {
169+ throw new ProviderNotReadyError (PROVIDER_NOT_YET_INITIALIZED );
170+ }
171+ throw new GeneralError (UNKNOWN_ERROR );
172+ }
173+
155174 Map <String , String > contextMap = ContextTransformer .transform (ctx );
156175 EvaluationRequest request = EvaluationRequest .builder ().namespaceKey (fliptProviderConfig .getNamespace ())
157176 .flagKey (key ).entityId (ctx .getTargetingKey ()).context (contextMap ).build ();
@@ -172,17 +191,22 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
172191 .build ();
173192 }
174193
194+ Value value = new Value (response .getVariantKey ());
175195 ImmutableMetadata .ImmutableMetadataBuilder flagMetadataBuilder = ImmutableMetadata .builder ();
176- if (response .getVariantAttachment () != null ) {
196+ if (response .getVariantAttachment () != null && ! response . getVariantAttachment (). isEmpty () ) {
177197 flagMetadataBuilder .addString ("variant-attachment" , response .getVariantAttachment ());
198+
199+ if (clazz .isAssignableFrom (Value .class )) {
200+ value = new Value (response .getVariantAttachment ());
201+ }
178202 }
179203
180204 return ProviderEvaluation .<Value >builder ()
181- .value (new Value ( response . getVariantKey ()) )
182- .variant (response .getVariantKey ())
183- .reason (TARGETING_MATCH .name ())
184- .flagMetadata (flagMetadataBuilder .build ())
185- .build ();
205+ .value (value )
206+ .variant (response .getVariantKey ())
207+ .reason (TARGETING_MATCH .name ())
208+ .flagMetadata (flagMetadataBuilder .build ())
209+ .build ();
186210 }
187211
188212 @ Override
0 commit comments