1
1
package dev .openfeature .javasdk ;
2
2
3
- import lombok .EqualsAndHashCode ;
4
- import lombok .Getter ;
5
- import lombok .Setter ;
6
- import lombok .ToString ;
3
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
+ import lombok .*;
7
5
8
6
import java .time .ZonedDateTime ;
9
7
import java .time .format .DateTimeFormatter ;
13
11
@ ToString @ EqualsAndHashCode
14
12
@ SuppressWarnings ("PMD.BeanMembersShouldSerialize" )
15
13
public class EvaluationContext {
14
+ @ EqualsAndHashCode .Exclude private final ObjectMapper objMapper ;
16
15
@ Setter @ Getter private String targetingKey ;
17
16
private final Map <String , Integer > integerAttributes ;
18
17
private final Map <String , String > stringAttributes ;
18
+ private final Map <String , Boolean > booleanAttributes ;
19
+ final Map <String , String > jsonAttributes ;
19
20
20
21
EvaluationContext () {
22
+ objMapper = new ObjectMapper ();
21
23
this .targetingKey = "" ;
22
24
this .integerAttributes = new HashMap <>();
23
25
this .stringAttributes = new HashMap <>();
26
+ booleanAttributes = new HashMap <>();
27
+ jsonAttributes = new HashMap <>();
28
+ }
29
+
30
+ // TODO Not sure if I should have sneakythrows or checked exceptions here..
31
+ @ SneakyThrows
32
+ public <T > void addStructureAttribute (String key , T value ) {
33
+ jsonAttributes .put (key , objMapper .writeValueAsString (value ));
34
+ }
35
+
36
+ @ SneakyThrows
37
+ public <T > T getStructureAttribute (String key , Class <T > klass ) {
38
+ String val = jsonAttributes .get (key );
39
+ return objMapper .readValue (val , klass );
24
40
}
25
41
26
42
public void addStringAttribute (String key , String value ) {
@@ -40,19 +56,17 @@ public Integer getIntegerAttribute(String key) {
40
56
}
41
57
42
58
public Boolean getBooleanAttribute (String key ) {
43
- return Boolean . valueOf ( stringAttributes . get (key ) );
59
+ return booleanAttributes . get (key );
44
60
}
45
61
46
62
public void addBooleanAttribute (String key , Boolean b ) {
47
- stringAttributes .put (key , b . toString () );
63
+ booleanAttributes .put (key , b );
48
64
}
49
65
50
66
public void addDatetimeAttribute (String key , ZonedDateTime value ) {
51
67
this .stringAttributes .put (key , value .format (DateTimeFormatter .ISO_ZONED_DATE_TIME ));
52
68
}
53
69
54
- // TODO: addStructure or similar.
55
-
56
70
public ZonedDateTime getDatetimeAttribute (String key ) {
57
71
String attr = this .stringAttributes .get (key );
58
72
if (attr == null ) {
@@ -66,21 +80,19 @@ public ZonedDateTime getDatetimeAttribute(String key) {
66
80
*/
67
81
public static EvaluationContext merge (EvaluationContext ctx1 , EvaluationContext ctx2 ) {
68
82
EvaluationContext ec = new EvaluationContext ();
69
- for (Map .Entry <String , Integer > e : ctx1 .integerAttributes .entrySet ()) {
70
- ec .addIntegerAttribute (e .getKey (), e .getValue ());
71
- }
72
83
73
- for (Map .Entry <String , Integer > e : ctx2 .integerAttributes .entrySet ()) {
74
- ec .addIntegerAttribute (e .getKey (), e .getValue ());
75
- }
84
+ ec .stringAttributes .putAll (ctx1 .stringAttributes );
85
+ ec .stringAttributes .putAll (ctx2 .stringAttributes );
76
86
77
- for (Map .Entry <String , String > e : ctx1 .stringAttributes .entrySet ()) {
78
- ec .addStringAttribute (e .getKey (), e .getValue ());
79
- }
87
+ ec .integerAttributes .putAll (ctx1 .integerAttributes );
88
+ ec .integerAttributes .putAll (ctx2 .integerAttributes );
89
+
90
+ ec .booleanAttributes .putAll (ctx1 .booleanAttributes );
91
+ ec .booleanAttributes .putAll (ctx2 .booleanAttributes );
92
+
93
+ ec .jsonAttributes .putAll (ctx1 .jsonAttributes );
94
+ ec .jsonAttributes .putAll (ctx2 .jsonAttributes );
80
95
81
- for (Map .Entry <String , String > e : ctx2 .stringAttributes .entrySet ()) {
82
- ec .addStringAttribute (e .getKey (), e .getValue ());
83
- }
84
96
if (ctx1 .getTargetingKey () != null ) {
85
97
ec .setTargetingKey (ctx1 .getTargetingKey ());
86
98
}
0 commit comments