|
6 | 6 | import com.datadog.openfeature.internal.config.Settings; |
7 | 7 | import com.datadog.openfeature.internal.connector.Connector; |
8 | 8 | import com.datadog.openfeature.internal.evaluation.ConfigurationStore; |
9 | | -import com.datadog.openfeature.internal.evaluation.EvaluationResult; |
10 | 9 | import com.datadog.openfeature.internal.evaluation.FlagEvaluator; |
11 | 10 | import com.datadog.openfeature.internal.exposure.ExposureEvent; |
12 | 11 | import com.datadog.openfeature.internal.exposure.Subject; |
13 | 12 | import com.datadog.openfeature.internal.ufc.Allocation; |
14 | 13 | import com.datadog.openfeature.internal.ufc.ServerConfiguration; |
15 | 14 | import com.datadog.openfeature.internal.ufc.ValueType; |
16 | | -import dev.openfeature.sdk.ErrorCode; |
17 | 15 | import dev.openfeature.sdk.EvaluationContext; |
18 | 16 | import dev.openfeature.sdk.ImmutableMetadata; |
19 | 17 | import dev.openfeature.sdk.ImmutableStructure; |
@@ -106,7 +104,7 @@ class DDEvaluator implements Evaluator, Consumer<ServerConfiguration> { |
106 | 104 | this.settings = settings; |
107 | 105 | // When span enrichment is off, the __dd_* span-enrichment metadata is not attached to |
108 | 106 | // evaluations, so an enabled provider pays nothing extra. |
109 | | - this.core = new FlagEvaluator(settings.isSpanEnrichmentEnabled()); |
| 107 | + this.core = new FlagEvaluator(settings.isSpanEnrichmentEnabled(), this::dispatchExposure); |
110 | 108 | } |
111 | 109 |
|
112 | 110 | @Override |
@@ -155,76 +153,29 @@ public void accept(final ServerConfiguration config) { |
155 | 153 | } |
156 | 154 |
|
157 | 155 | @Override |
158 | | - @SuppressWarnings("unchecked") |
159 | 156 | public <T> ProviderEvaluation<T> evaluate( |
160 | 157 | final Class<T> target, |
161 | 158 | final String key, |
162 | 159 | final T defaultValue, |
163 | 160 | final EvaluationContext context) { |
164 | | - final com.datadog.openfeature.internal.evaluation.EvaluationContext adapted = |
165 | | - context == null |
166 | | - ? null |
167 | | - : new com.datadog.openfeature.internal.evaluation.EvaluationContext() { |
168 | | - @Override |
169 | | - public String getTargetingKey() { |
170 | | - return context.getTargetingKey(); |
171 | | - } |
172 | | - |
173 | | - @Override |
174 | | - public boolean hasAttribute(final String name) { |
175 | | - return context.keySet().contains(name); |
176 | | - } |
177 | | - |
178 | | - @Override |
179 | | - public Object attribute(final String name) { |
180 | | - return context.convertValue(context.getValue(name)); |
181 | | - } |
182 | | - }; |
183 | | - final EvaluationResult<?> result = |
184 | | - core.evaluate(configuration.get(), (Class) coreType(target), key, defaultValue, adapted); |
185 | | - final T value = |
186 | | - result.getValue() == defaultValue ? defaultValue : mapValue(target, result.getValue()); |
187 | | - final ProviderEvaluation<T> evaluation = |
188 | | - ProviderEvaluation.<T>builder() |
189 | | - .value(value) |
190 | | - .reason(result.getReason()) |
191 | | - .variant(result.getVariant()) |
192 | | - .errorCode( |
193 | | - result.getErrorCode() == null |
194 | | - ? null |
195 | | - : ErrorCode.valueOf(result.getErrorCode().name())) |
196 | | - .errorMessage(result.getErrorMessage()) |
197 | | - .flagMetadata(result.getFlagMetadata()) |
198 | | - .build(); |
199 | | - if (result.isLogExposure()) { |
200 | | - dispatchExposure(key, evaluation, result.getSerialId(), context); |
201 | | - } |
202 | | - return evaluation; |
203 | | - } |
204 | | - |
205 | | - private static Class<?> coreType(final Class<?> target) { |
206 | | - return target == Value.class ? Object.class : target; |
| 161 | + return core.evaluate(configuration.get(), target, key, defaultValue, context); |
207 | 162 | } |
208 | 163 |
|
209 | 164 | static boolean isAllocationActive(final Allocation allocation, final Instant now) { |
210 | 165 | return FlagEvaluator.isAllocationActive(allocation, now); |
211 | 166 | } |
212 | 167 |
|
213 | 168 | static boolean isTypeCompatible(final Class<?> target, final ValueType type) { |
214 | | - return FlagEvaluator.isTypeCompatible(coreType(target), type); |
| 169 | + return FlagEvaluator.isTypeCompatible(target, type); |
215 | 170 | } |
216 | 171 |
|
217 | | - @SuppressWarnings("unchecked") |
218 | 172 | static <T> T mapValue(final Class<T> target, final Object value) { |
219 | | - if (target == Value.class) { |
220 | | - return value == null ? null : (T) Value.objectToValue(value); |
221 | | - } |
222 | 173 | return FlagEvaluator.mapValue(target, value); |
223 | 174 | } |
224 | 175 |
|
225 | | - private <T> void dispatchExposure( |
| 176 | + private void dispatchExposure( |
226 | 177 | final String flag, |
227 | | - final ProviderEvaluation<T> evaluation, |
| 178 | + final ProviderEvaluation<?> evaluation, |
228 | 179 | final Integer serialId, |
229 | 180 | final EvaluationContext context) { |
230 | 181 | final String allocationKey = allocationKey(evaluation); |
@@ -255,7 +206,7 @@ void recordExposure(final ExposureEvent event) { |
255 | 206 | } |
256 | 207 | } |
257 | 208 |
|
258 | | - private static <T> String allocationKey(final ProviderEvaluation<T> resolution) { |
| 209 | + private static String allocationKey(final ProviderEvaluation<?> resolution) { |
259 | 210 | final ImmutableMetadata meta = resolution.getFlagMetadata(); |
260 | 211 | return meta == null ? null : meta.getString("allocationKey"); |
261 | 212 | } |
|
0 commit comments