1414import io .split .client .SplitClient ;
1515import io .split .openfeature .utils .Serialization ;
1616
17- import java .util .HashMap ;
17+ import java .time .ZonedDateTime ;
18+ import java .time .format .DateTimeParseException ;
1819import java .util .List ;
1920import java .util .Map ;
2021import java .util .stream .Collectors ;
@@ -127,11 +128,8 @@ public ProviderEvaluation<Structure> getObjectEvaluation(String key, Structure d
127128 if (noTreatment (evaluated )) {
128129 return constructProviderEvaluation (defaultTreatment , evaluated , Reason .DEFAULT , ErrorCode .FLAG_NOT_FOUND .name ());
129130 }
130- Structure structure = new Structure ();
131131 Map <String , Object > rawMap = Serialization .stringToMap (evaluated );
132- for (Map .Entry <String , Object > rawEntry : rawMap .entrySet ()) {
133- structure .add (rawEntry .getKey (),(String ) rawEntry .getValue ());
134- }
132+ Structure structure = mapToStructure (rawMap );
135133 return constructProviderEvaluation (structure , evaluated );
136134 } catch (OpenFeatureError e ) {
137135 throw e ;
@@ -173,17 +171,12 @@ private <T> ProviderEvaluation<T> constructProviderEvaluation(T value, String va
173171 }
174172
175173 private Map <String , Object > getMapFromStructMap (Map <String , Value > structMap ) {
176- Map <String , Object > toReturn = new HashMap <>();
177- for (Map .Entry <String , Value > entry : structMap .entrySet ()) {
178- String key = entry .getKey ();
179- Value value = entry .getValue ();
180- toReturn .put (key , getInnerValue (value ));
181- }
182- return toReturn ;
174+ return structMap .entrySet ().stream ().collect (Collectors .toMap (Map .Entry ::getKey , e -> getInnerValue (e .getValue ())));
183175 }
184176
185- private Map <String , Object > getMapFromStructure (Structure structure ) {
186- return getMapFromStructMap (structure .asMap ());
177+ private Structure mapToStructure (Map <String , Object > map ) {
178+ return new Structure (
179+ map .entrySet ().stream ().collect (Collectors .toMap (Map .Entry ::getKey , e -> objectToValue (e .getValue ()))));
187180 }
188181
189182 private Object getInnerValue (Value value ) {
@@ -205,19 +198,49 @@ private Object getInnerValue(Value value) {
205198 }
206199 object = value .asZonedDateTime ();
207200 if (object != null ) {
208- return object . toString () ;
201+ return object ;
209202 }
210203 object = value .asStructure ();
211204 if (object != null ) {
212205 // must return a map
213- return getMapFromStructure (( Structure ) object );
206+ return getMapFromStructMap ((( Structure ) object ). asMap () );
214207 }
215208 object = value .asList ();
216209 if (object != null ) {
217210 // must return a list of inner objects
218211 List <Value > values = (List <Value >) object ;
219212 return values .stream ().map (this ::getInnerValue ).collect (Collectors .toList ());
220213 }
221- return null ;
214+ throw new ClassCastException ("Could not get inner value from Value object." );
215+ }
216+
217+ private Value objectToValue (Object object ) {
218+ if (object instanceof Value ) {
219+ return (Value ) object ;
220+ } else if (object instanceof String ) {
221+ // try to parse to zoned date time, otherwise use as string
222+ try {
223+ return new Value (ZonedDateTime .parse ((String ) object ));
224+ } catch (DateTimeParseException e ) {
225+ return new Value ((String ) object );
226+ }
227+ } else if (object instanceof Boolean ) {
228+ return new Value ((Boolean ) object );
229+ } else if (object instanceof Integer ) {
230+ return new Value ((Integer ) object );
231+ } else if (object instanceof Double ) {
232+ return new Value ((Double ) object );
233+ } else if (object instanceof Structure ) {
234+ return new Value ((Structure ) object );
235+ } else if (object instanceof List ) {
236+ // need to translate each elem in list to a value
237+ return new Value (((List <Object >) object ).stream ().map (this ::objectToValue ).collect (Collectors .toList ()));
238+ } else if (object instanceof ZonedDateTime ) {
239+ return new Value ((ZonedDateTime ) object );
240+ } else if (object instanceof Map ) {
241+ return new Value (mapToStructure ((Map <String , Object >) object ));
242+ } else {
243+ throw new ClassCastException ("Could not cast Object to Value" );
244+ }
222245 }
223246}
0 commit comments