|
2 | 2 |
|
3 | 3 | import com.optimizely.ab.Optimizely; |
4 | 4 | import com.optimizely.ab.OptimizelyUserContext; |
5 | | -import com.optimizely.ab.optimizelydecision.OptimizelyDecision; |
6 | 5 | import com.optimizely.ab.optimizelyjson.OptimizelyJSON; |
7 | 6 | import dev.openfeature.sdk.EvaluationContext; |
8 | 7 | import dev.openfeature.sdk.EventProvider; |
9 | 8 | import dev.openfeature.sdk.Metadata; |
10 | 9 | import dev.openfeature.sdk.ProviderEvaluation; |
| 10 | +import dev.openfeature.sdk.Reason; |
11 | 11 | import dev.openfeature.sdk.Structure; |
12 | 12 | import dev.openfeature.sdk.Value; |
13 | | -import java.util.List; |
14 | 13 | import java.util.Map; |
15 | 14 | import lombok.Getter; |
16 | 15 | import lombok.SneakyThrows; |
@@ -69,68 +68,135 @@ public Metadata getMetadata() { |
69 | 68 | @Override |
70 | 69 | public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
71 | 70 | OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
72 | | - OptimizelyDecision decision = userContext.decide(key); |
73 | | - String variationKey = decision.getVariationKey(); |
74 | | - String reasonsString = null; |
75 | | - if (variationKey == null) { |
76 | | - List<String> reasons = decision.getReasons(); |
77 | | - reasonsString = String.join(", ", reasons); |
| 71 | + |
| 72 | + String variableKey = getVariableKey(ctx); |
| 73 | + Boolean enabled = optimizely.getFeatureVariableBoolean( |
| 74 | + key, variableKey, userContext.getUserId(), userContext.getAttributes()); |
| 75 | + |
| 76 | + String variant = variableKey; |
| 77 | + String reason = Reason.TARGETING_MATCH.name(); |
| 78 | + if (enabled == null) { |
| 79 | + enabled = false; |
| 80 | + variant = null; |
| 81 | + reason = Reason.DEFAULT.name(); |
78 | 82 | } |
79 | 83 |
|
80 | | - boolean enabled = decision.getEnabled(); |
81 | 84 | return ProviderEvaluation.<Boolean>builder() |
82 | 85 | .value(enabled) |
83 | | - .reason(reasonsString) |
| 86 | + .reason(reason) |
| 87 | + .variant(variant) |
84 | 88 | .build(); |
85 | 89 | } |
86 | 90 |
|
| 91 | + private static String getVariableKey(EvaluationContext ctx) { |
| 92 | + String variableKey = "value"; |
| 93 | + Value varKey = ctx.getValue("variableKey"); |
| 94 | + if (varKey != null && varKey.isString() && !varKey.asString().isBlank()) { |
| 95 | + variableKey = varKey.asString(); |
| 96 | + } |
| 97 | + return variableKey; |
| 98 | + } |
| 99 | + |
87 | 100 | @SneakyThrows |
88 | 101 | @Override |
89 | 102 | public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
90 | | - throw new UnsupportedOperationException("String evaluation is not directly supported by Optimizely provider," |
91 | | - + "use getObjectEvaluation instead."); |
| 103 | + OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
| 104 | + |
| 105 | + String variableKey = getVariableKey(ctx); |
| 106 | + String value = optimizely.getFeatureVariableString( |
| 107 | + key, variableKey, userContext.getUserId(), userContext.getAttributes()); |
| 108 | + |
| 109 | + String variant = variableKey; |
| 110 | + String reason = Reason.TARGETING_MATCH.name(); |
| 111 | + if (value == null) { |
| 112 | + value = defaultValue; |
| 113 | + variant = null; |
| 114 | + reason = Reason.DEFAULT.name(); |
| 115 | + } |
| 116 | + |
| 117 | + return ProviderEvaluation.<String>builder() |
| 118 | + .value(value) |
| 119 | + .reason(reason) |
| 120 | + .variant(variant) |
| 121 | + .build(); |
92 | 122 | } |
93 | 123 |
|
94 | 124 | @Override |
95 | 125 | public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
96 | | - throw new UnsupportedOperationException("Integer evaluation is not directly supported by Optimizely provider," |
97 | | - + "use getObjectEvaluation instead."); |
| 126 | + OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
| 127 | + |
| 128 | + String variableKey = getVariableKey(ctx); |
| 129 | + Integer value = optimizely.getFeatureVariableInteger( |
| 130 | + key, variableKey, userContext.getUserId(), userContext.getAttributes()); |
| 131 | + |
| 132 | + String variant = variableKey; |
| 133 | + String reason = Reason.TARGETING_MATCH.name(); |
| 134 | + if (value == null) { |
| 135 | + value = defaultValue; |
| 136 | + variant = null; |
| 137 | + reason = Reason.DEFAULT.name(); |
| 138 | + } |
| 139 | + |
| 140 | + return ProviderEvaluation.<Integer>builder() |
| 141 | + .value(value) |
| 142 | + .reason(reason) |
| 143 | + .variant(variant) |
| 144 | + .build(); |
98 | 145 | } |
99 | 146 |
|
100 | 147 | @Override |
101 | 148 | public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
102 | | - throw new UnsupportedOperationException("Double evaluation is not directly supported by Optimizely provider," |
103 | | - + "use getObjectEvaluation instead."); |
| 149 | + OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
| 150 | + |
| 151 | + String variableKey = getVariableKey(ctx); |
| 152 | + Double value = optimizely.getFeatureVariableDouble( |
| 153 | + key, variableKey, userContext.getUserId(), userContext.getAttributes()); |
| 154 | + |
| 155 | + String variant = variableKey; |
| 156 | + String reason = Reason.TARGETING_MATCH.name(); |
| 157 | + if (value == null) { |
| 158 | + value = defaultValue; |
| 159 | + variant = null; |
| 160 | + reason = Reason.DEFAULT.name(); |
| 161 | + } |
| 162 | + |
| 163 | + return ProviderEvaluation.<Double>builder() |
| 164 | + .value(value) |
| 165 | + .reason(reason) |
| 166 | + .variant(variant) |
| 167 | + .build(); |
104 | 168 | } |
105 | 169 |
|
106 | 170 | @SneakyThrows |
107 | 171 | @Override |
108 | 172 | public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
109 | 173 | OptimizelyUserContext userContext = contextTransformer.transform(ctx); |
110 | | - OptimizelyDecision decision = userContext.decide(key); |
111 | | - String variationKey = decision.getVariationKey(); |
112 | | - String reasonsString = null; |
113 | | - if (variationKey == null) { |
114 | | - List<String> reasons = decision.getReasons(); |
115 | | - reasonsString = String.join(", ", reasons); |
116 | | - } |
117 | 174 |
|
118 | | - Value evaluatedValue = defaultValue; |
119 | | - boolean enabled = decision.getEnabled(); |
120 | | - if (enabled) { |
121 | | - OptimizelyJSON variables = decision.getVariables(); |
122 | | - evaluatedValue = toValue(variables); |
| 175 | + String variableKey = getVariableKey(ctx); |
| 176 | + OptimizelyJSON value = optimizely.getFeatureVariableJSON( |
| 177 | + key, variableKey, userContext.getUserId(), userContext.getAttributes()); |
| 178 | + Value evaluatedValue = toValue(value); |
| 179 | + |
| 180 | + String variant = variableKey; |
| 181 | + String reason = Reason.TARGETING_MATCH.name(); |
| 182 | + if (value == null) { |
| 183 | + evaluatedValue = defaultValue; |
| 184 | + variant = null; |
| 185 | + reason = Reason.DEFAULT.name(); |
123 | 186 | } |
124 | 187 |
|
125 | 188 | return ProviderEvaluation.<Value>builder() |
126 | 189 | .value(evaluatedValue) |
127 | | - .reason(reasonsString) |
128 | | - .variant(variationKey) |
| 190 | + .reason(reason) |
| 191 | + .variant(variant) |
129 | 192 | .build(); |
130 | 193 | } |
131 | 194 |
|
132 | 195 | @SneakyThrows |
133 | 196 | private Value toValue(OptimizelyJSON optimizelyJson) { |
| 197 | + if (optimizelyJson == null) { |
| 198 | + return new Value(); |
| 199 | + } |
134 | 200 | Map<String, Object> map = optimizelyJson.toMap(); |
135 | 201 | Structure structure = Structure.mapToStructure(map); |
136 | 202 | return new Value(structure); |
|
0 commit comments