Skip to content

Commit

Permalink
chore: recommit changes without submodule updates
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Phelps <[email protected]>
  • Loading branch information
markphelps committed Sep 17, 2024
1 parent cd7a61a commit d0c6222
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa

@Override
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
ProviderEvaluation<Value> valueProviderEvaluation = getObjectEvaluation(key, new Value(defaultValue), ctx);
ProviderEvaluation<Value> valueProviderEvaluation =
evaluateVariant(String.class, key, new Value(defaultValue), ctx);
return ProviderEvaluation.<String>builder()
.value(valueProviderEvaluation.getValue().asString())
.variant(valueProviderEvaluation.getVariant())
Expand All @@ -135,7 +136,8 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default

@Override
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
ProviderEvaluation<Value> valueProviderEvaluation = getObjectEvaluation(key, new Value(defaultValue), ctx);
ProviderEvaluation<Value> valueProviderEvaluation =
evaluateVariant(Integer.class, key, new Value(defaultValue), ctx);
Integer value = getIntegerValue(valueProviderEvaluation, defaultValue);
return ProviderEvaluation.<Integer>builder()
.value(value)
Expand All @@ -157,7 +159,8 @@ private static Integer getIntegerValue(ProviderEvaluation<Value> valueProviderEv

@Override
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) {
ProviderEvaluation<Value> valueProviderEvaluation = getObjectEvaluation(key, new Value(defaultValue), ctx);
ProviderEvaluation<Value> valueProviderEvaluation =
evaluateVariant(Double.class, key, new Value(defaultValue), ctx);
Double value = getDoubleValue(valueProviderEvaluation, defaultValue);
return ProviderEvaluation.<Double>builder()
.value(value)
Expand All @@ -179,12 +182,18 @@ private static Double getDoubleValue(ProviderEvaluation<Value> valueProviderEval

@Override
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) {
return evaluateVariant(Value.class, key, defaultValue, ctx);
}

private <T> ProviderEvaluation<Value> evaluateVariant(Class<T> clazz, String key, Value defaultValue,
EvaluationContext ctx) {
if (!ProviderState.READY.equals(state)) {
if (ProviderState.NOT_READY.equals(state)) {
throw new ProviderNotReadyError(PROVIDER_NOT_YET_INITIALIZED);
}
throw new GeneralError(UNKNOWN_ERROR);
}

Map<String, String> contextMap = ContextTransformer.transform(ctx);
EvaluationRequest request = EvaluationRequest.builder().namespaceKey(fliptProviderConfig.getNamespace())
.flagKey(key).entityId(ctx.getTargetingKey()).context(contextMap).build();
Expand All @@ -209,7 +218,10 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
ImmutableMetadata.ImmutableMetadataBuilder flagMetadataBuilder = ImmutableMetadata.builder();
if (response.getVariantAttachment() != null && !response.getVariantAttachment().isEmpty()) {
flagMetadataBuilder.addString("variant-attachment", response.getVariantAttachment());
value = new Value(response.getVariantAttachment());

if (clazz.isAssignableFrom(Value.class)) {
value = new Value(response.getVariantAttachment());
}
}

return ProviderEvaluation.<Value>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.FlagEvaluationDetails;
import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.ImmutableMetadata;
import dev.openfeature.sdk.MutableContext;
import dev.openfeature.sdk.OpenFeatureAPI;
import dev.openfeature.sdk.ProviderEvaluation;
import dev.openfeature.sdk.ProviderEventDetails;
import dev.openfeature.sdk.ProviderState;
import dev.openfeature.sdk.Value;
import dev.openfeature.sdk.exceptions.GeneralError;
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
import lombok.SneakyThrows;
Expand All @@ -16,8 +26,6 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
Expand Down Expand Up @@ -46,7 +54,6 @@ class FliptProviderTest {

private static FliptProvider fliptProvider;
private static Client client;

private String apiUrl;

@BeforeAll
Expand Down Expand Up @@ -167,6 +174,24 @@ void getEvaluationMetadataTest() {
assertNull(nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment"));
}

@SneakyThrows
@Test
void getObjectEvaluationTest() {
mockFliptAPI("/evaluate/v1/variant", "variant-object.json", OBJECT_FLAG_NAME);
MutableContext evaluationContext = new MutableContext();
evaluationContext.setTargetingKey(TARGETING_KEY);
evaluationContext.add("userId", "object");

Value expectedValue = new Value("{\"key1\":\"value1\",\"key2\":42,\"key3\":true}");
Value emptyValue = new Value();

assertEquals(expectedValue, client.getObjectValue(OBJECT_FLAG_NAME, emptyValue, evaluationContext));
assertEquals(emptyValue, client.getObjectValue("non-existing", emptyValue, evaluationContext));

// non-object flag value
assertEquals(emptyValue, client.getObjectValue(VARIANT_FLAG_NAME, emptyValue, evaluationContext));
}

@SneakyThrows
@Test
void shouldThrowIfNotInitialized() {
Expand Down

0 comments on commit d0c6222

Please sign in to comment.