@@ -40,7 +40,11 @@ public class FliptProvider extends EventProvider {
40
40
@ Getter
41
41
private FliptClient fliptClient ;
42
42
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 );
44
48
45
49
/**
46
50
* Constructor.
@@ -96,7 +100,8 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
96
100
97
101
@ Override
98
102
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 );
100
105
return ProviderEvaluation .<String >builder ()
101
106
.value (valueProviderEvaluation .getValue ().asString ())
102
107
.variant (valueProviderEvaluation .getVariant ())
@@ -108,7 +113,8 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default
108
113
109
114
@ Override
110
115
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 );
112
118
Integer value = getIntegerValue (valueProviderEvaluation , defaultValue );
113
119
return ProviderEvaluation .<Integer >builder ()
114
120
.value (value )
@@ -130,7 +136,8 @@ private static Integer getIntegerValue(ProviderEvaluation<Value> valueProviderEv
130
136
131
137
@ Override
132
138
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 );
134
141
Double value = getDoubleValue (valueProviderEvaluation , defaultValue );
135
142
return ProviderEvaluation .<Double >builder ()
136
143
.value (value )
@@ -152,6 +159,18 @@ private static Double getDoubleValue(ProviderEvaluation<Value> valueProviderEval
152
159
153
160
@ Override
154
161
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
+
155
174
Map <String , String > contextMap = ContextTransformer .transform (ctx );
156
175
EvaluationRequest request = EvaluationRequest .builder ().namespaceKey (fliptProviderConfig .getNamespace ())
157
176
.flagKey (key ).entityId (ctx .getTargetingKey ()).context (contextMap ).build ();
@@ -172,17 +191,22 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
172
191
.build ();
173
192
}
174
193
194
+ Value value = new Value (response .getVariantKey ());
175
195
ImmutableMetadata .ImmutableMetadataBuilder flagMetadataBuilder = ImmutableMetadata .builder ();
176
- if (response .getVariantAttachment () != null ) {
196
+ if (response .getVariantAttachment () != null && ! response . getVariantAttachment (). isEmpty () ) {
177
197
flagMetadataBuilder .addString ("variant-attachment" , response .getVariantAttachment ());
198
+
199
+ if (clazz .isAssignableFrom (Value .class )) {
200
+ value = new Value (response .getVariantAttachment ());
201
+ }
178
202
}
179
203
180
204
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 ();
186
210
}
187
211
188
212
@ Override
0 commit comments