Skip to content

Commit 0fb68c6

Browse files
PerfectSlayerclaude
andcommitted
refactor(ffe): Delegate evaluation to the OpenFeature-typed evaluator
The evaluator now takes the OpenFeature context and returns the ProviderEvaluation, so the provider no longer adapts the context and converts the result on every evaluation. Exposures are dispatched through the evaluator ExposureListener. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2375b4c commit 0fb68c6

1 file changed

Lines changed: 6 additions & 55 deletions

File tree

‎dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java‎

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
import com.datadog.openfeature.internal.config.Settings;
77
import com.datadog.openfeature.internal.connector.Connector;
88
import com.datadog.openfeature.internal.evaluation.ConfigurationStore;
9-
import com.datadog.openfeature.internal.evaluation.EvaluationResult;
109
import com.datadog.openfeature.internal.evaluation.FlagEvaluator;
1110
import com.datadog.openfeature.internal.exposure.ExposureEvent;
1211
import com.datadog.openfeature.internal.exposure.Subject;
1312
import com.datadog.openfeature.internal.ufc.Allocation;
1413
import com.datadog.openfeature.internal.ufc.ServerConfiguration;
1514
import com.datadog.openfeature.internal.ufc.ValueType;
16-
import dev.openfeature.sdk.ErrorCode;
1715
import dev.openfeature.sdk.EvaluationContext;
1816
import dev.openfeature.sdk.ImmutableMetadata;
1917
import dev.openfeature.sdk.ImmutableStructure;
@@ -106,7 +104,7 @@ class DDEvaluator implements Evaluator, Consumer<ServerConfiguration> {
106104
this.settings = settings;
107105
// When span enrichment is off, the __dd_* span-enrichment metadata is not attached to
108106
// evaluations, so an enabled provider pays nothing extra.
109-
this.core = new FlagEvaluator(settings.isSpanEnrichmentEnabled());
107+
this.core = new FlagEvaluator(settings.isSpanEnrichmentEnabled(), this::dispatchExposure);
110108
}
111109

112110
@Override
@@ -155,76 +153,29 @@ public void accept(final ServerConfiguration config) {
155153
}
156154

157155
@Override
158-
@SuppressWarnings("unchecked")
159156
public <T> ProviderEvaluation<T> evaluate(
160157
final Class<T> target,
161158
final String key,
162159
final T defaultValue,
163160
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);
207162
}
208163

209164
static boolean isAllocationActive(final Allocation allocation, final Instant now) {
210165
return FlagEvaluator.isAllocationActive(allocation, now);
211166
}
212167

213168
static boolean isTypeCompatible(final Class<?> target, final ValueType type) {
214-
return FlagEvaluator.isTypeCompatible(coreType(target), type);
169+
return FlagEvaluator.isTypeCompatible(target, type);
215170
}
216171

217-
@SuppressWarnings("unchecked")
218172
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-
}
222173
return FlagEvaluator.mapValue(target, value);
223174
}
224175

225-
private <T> void dispatchExposure(
176+
private void dispatchExposure(
226177
final String flag,
227-
final ProviderEvaluation<T> evaluation,
178+
final ProviderEvaluation<?> evaluation,
228179
final Integer serialId,
229180
final EvaluationContext context) {
230181
final String allocationKey = allocationKey(evaluation);
@@ -255,7 +206,7 @@ void recordExposure(final ExposureEvent event) {
255206
}
256207
}
257208

258-
private static <T> String allocationKey(final ProviderEvaluation<T> resolution) {
209+
private static String allocationKey(final ProviderEvaluation<?> resolution) {
259210
final ImmutableMetadata meta = resolution.getFlagMetadata();
260211
return meta == null ? null : meta.getString("allocationKey");
261212
}

0 commit comments

Comments
 (0)