Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public EvaluationContextConverter(LDLogger logger) {
* @return An LDContext containing information from the evaluation context.
*/
public LDContext toLdContext(EvaluationContext evaluationContext) {
// Using the kind as a map here because getting a value from an immutable context that doesn't exist
// throws. https://github.com/open-feature/java-sdk/pull/300
Map<String, Value> attributes = evaluationContext.asMap();
Map<String, Value> attributes = evaluationContext.asUnmodifiableMap();

Value kindAsValue = attributes.get("kind");

Expand All @@ -45,7 +43,7 @@ public LDContext toLdContext(EvaluationContext evaluationContext) {
String kindString = kindAsValue.asString();
if (Objects.equals(kindString, "multi")) {
// A multi-context.
return BuildMultiContext(evaluationContext);
return BuildMultiContext(attributes);
} else {
// Single context with specified kind.
finalKind = kindString;
Expand All @@ -60,7 +58,7 @@ public LDContext toLdContext(EvaluationContext evaluationContext) {

targetingKey = getTargetingKey(targetingKey, keyAsValue);

return BuildSingleContext(evaluationContext.asMap(), finalKind, targetingKey);
return BuildSingleContext(attributes, finalKind, targetingKey);
}

private boolean isNullOrEmpty(String value) {
Expand Down Expand Up @@ -103,13 +101,13 @@ private String getTargetingKey(String targetingKey, Value keyAsValue) {
/**
* Build a multi-context from an evaluation context.
*
* @param evaluationContext The evaluation context containing multi-context information.
* @param contextAttributes The attributes of the evaluation context containing multi-context information.
* @return The built context.
*/
private LDContext BuildMultiContext(EvaluationContext evaluationContext) {
private LDContext BuildMultiContext(Map<String, Value> contextAttributes) {
ContextMultiBuilder multiBuilder = LDContext.multiBuilder();

evaluationContext.asMap().forEach((kind, attributes) -> {
contextAttributes.forEach((kind, attributes) -> {
// Do not need to do anything for the kind key.
if (Objects.equals(kind, "kind")) return;

Expand All @@ -119,7 +117,7 @@ private LDContext BuildMultiContext(EvaluationContext evaluationContext) {
return;
}

Map<String, Value> attributesMap = attributes.asStructure().asMap();
Map<String, Value> attributesMap = attributes.asStructure().asUnmodifiableMap();
Value keyAsValue = attributesMap.get("key");
Value targetingKeyAsValue = attributesMap.get("targetingKey");
String targetingKey = targetingKeyAsValue != null ? targetingKeyAsValue.asString() : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public LDValue toLdValue(Value value) {
if (value.isStructure()) {
ObjectBuilder objectBuilder = LDValue.buildObject();
Structure structure = value.asStructure();
structure.asMap().forEach((itemKey, itemValue) -> {
structure.asUnmodifiableMap().forEach((itemKey, itemValue) -> {
LDValue itemLdValue = toLdValue(itemValue);
objectBuilder.put(itemKey, itemLdValue);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import com.launchdarkly.sdk.LDValue;
import com.launchdarkly.sdk.LDValueType;
import dev.openfeature.sdk.ImmutableStructure;
import dev.openfeature.sdk.Structure;
import dev.openfeature.sdk.Value;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ValueConverterTest {
private final ValueConverter valueConverter = new ValueConverter(LDLogger.none());
Expand Down Expand Up @@ -107,4 +111,14 @@ public void itCanConvertStructures() {
assertEquals("bKey", valueList.get(1).keys().iterator().next());
assertEquals("bValue", valueList.get(1).values().iterator().next().stringValue());
}

@Test
public void itUsesTheReadOnlyStructureMap() {
Structure structure = mock(Structure.class);
when(structure.asUnmodifiableMap()).thenReturn(Collections.singletonMap("key", new Value("value")));

LDValue converted = valueConverter.toLdValue(new Value(structure));

assertEquals("value", converted.get("key").stringValue());
}
}