Skip to content

Commit

Permalink
improve naming
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert committed Oct 5, 2022
1 parent fae24d5 commit 5418242
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
24 changes: 12 additions & 12 deletions src/main/java/dev/openfeature/sdk/MutableContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
public class MutableContext implements EvaluationContext {

@Setter() @Getter private String targetingKey;
@Delegate(excludes = HideDelegateAddMethods.class) private final HashMapStructure structure;
@Delegate(excludes = HideDelegateAddMethods.class) private final MutableStructure structure;

public MutableContext() {
this.structure = new HashMapStructure();
this.structure = new MutableStructure();
this.targetingKey = "";
}

Expand All @@ -28,7 +28,7 @@ public MutableContext(String targetingKey) {
}

public MutableContext(Map<String, Value> attributes) {
this.structure = new HashMapStructure(attributes);
this.structure = new MutableStructure(attributes);
this.targetingKey = "";
}

Expand Down Expand Up @@ -63,7 +63,7 @@ public MutableContext add(String key, Instant value) {
return this;
}

public MutableContext add(String key, HashMapStructure value) {
public MutableContext add(String key, Structure value) {
this.structure.add(key, value);
return this;
}
Expand Down Expand Up @@ -106,35 +106,35 @@ public EvaluationContext merge(EvaluationContext overridingContext) {
* Hidden class to tell Lombok not to copy these methods over via delegation.
*/
private static class HideDelegateAddMethods {
public HashMapStructure add(String ignoredKey, Boolean ignoredValue) {
public MutableStructure add(String ignoredKey, Boolean ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, Double ignoredValue) {
public MutableStructure add(String ignoredKey, Double ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, String ignoredValue) {
public MutableStructure add(String ignoredKey, String ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, Value ignoredValue) {
public MutableStructure add(String ignoredKey, Value ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, Integer ignoredValue) {
public MutableStructure add(String ignoredKey, Integer ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, List<Value> ignoredValue) {
public MutableStructure add(String ignoredKey, List<Value> ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, HashMapStructure ignoredValue) {
public MutableStructure add(String ignoredKey, MutableStructure ignoredValue) {
return null;
}

public HashMapStructure add(String ignoredKey, Instant ignoredValue) {
public MutableStructure add(String ignoredKey, Instant ignoredValue) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
import lombok.ToString;

/**
* {@link HashMapStructure} represents a potentially nested object type which is used to pass complex objects via
* {@link MutableStructure} represents a potentially nested object type which is used to pass complex objects via
* {@link MutableContext}.
*/
@ToString
@EqualsAndHashCode
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
public class HashMapStructure {
public class MutableStructure implements Structure {

protected final Map<String, Value> attributes;

public HashMapStructure() {
public MutableStructure() {
this.attributes = new HashMap<>();
}

public HashMapStructure(Map<String, Value> attributes) {
public MutableStructure(Map<String, Value> attributes) {
this.attributes = new HashMap<>(attributes);
}

Expand All @@ -37,27 +37,27 @@ public Value getValue(String key) {
}

// adders
public HashMapStructure add(String key, Value value) {
public MutableStructure add(String key, Value value) {
attributes.put(key, value);
return this;
}

public HashMapStructure add(String key, Boolean value) {
public MutableStructure add(String key, Boolean value) {
attributes.put(key, new Value(value));
return this;
}

public HashMapStructure add(String key, String value) {
public MutableStructure add(String key, String value) {
attributes.put(key, new Value(value));
return this;
}

public HashMapStructure add(String key, Integer value) {
public MutableStructure add(String key, Integer value) {
attributes.put(key, new Value(value));
return this;
}

public HashMapStructure add(String key, Double value) {
public MutableStructure add(String key, Double value) {
attributes.put(key, new Value(value));
return this;
}
Expand All @@ -69,17 +69,17 @@ public HashMapStructure add(String key, Double value) {
* @param value date-time value
* @return Structure
*/
public HashMapStructure add(String key, Instant value) {
public MutableStructure add(String key, Instant value) {
attributes.put(key, new Value(value));
return this;
}

public HashMapStructure add(String key, HashMapStructure value) {
public MutableStructure add(String key, Structure value) {
attributes.put(key, new Value(value));
return this;
}

public <T> HashMapStructure add(String key, List<Value> value) {
public <T> MutableStructure add(String key, List<Value> value) {
attributes.put(key, new Value(value));
return this;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ private Object convertValue(Value value) {
}

if (value.isStructure()) {
HashMapStructure s = value.asStructure();
Structure s = value.asStructure();
return s.asMap()
.keySet()
.stream()
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/dev/openfeature/sdk/EvalContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class EvalContextTest {
assertEquals("targeting-key", ec.getTargetingKey());
}

@Specification(number="3.1.2", text="The evaluation context MUST support the inclusion of " +
@Specification(number="3.1.2", text= "The evaluation context MUST support the inclusion of " +
"custom fields, having keys of type `string`, and " +
"values of type `boolean | string | number | datetime | structure`.")
@Test void eval_context() {
Expand All @@ -45,13 +45,13 @@ public class EvalContextTest {
"values of type `boolean | string | number | datetime | structure`.")
@Test void eval_context_structure_array() {
MutableContext ec = new MutableContext();
ec.add("obj", new HashMapStructure().add("val1", 1).add("val2", "2"));
ec.add("obj", new MutableStructure().add("val1", 1).add("val2", "2"));
ec.add("arr", new ArrayList<Value>(){{
add(new Value("one"));
add(new Value("two"));
}});

HashMapStructure str = ec.getValue("obj").asStructure();
Structure str = ec.getValue("obj").asStructure();
assertEquals(1, str.getValue("val1").asInteger());
assertEquals("2", str.getValue("val2").asString());

Expand All @@ -76,7 +76,7 @@ public class EvalContextTest {
Instant dt = Instant.now();
ec.add("dt", dt);

ec.add("obj", new HashMapStructure().add("val1", 1).add("val2", "2"));
ec.add("obj", new MutableStructure().add("val1", 1).add("val2", "2"));

Map<String, Value> foundStr = ec.asMap();
assertEquals(ec.getValue("str").asString(), foundStr.get("str").asString());
Expand All @@ -90,7 +90,7 @@ public class EvalContextTest {
assertEquals(ec.getValue("int").asInteger(), foundInt.get("int").asInteger());
assertEquals(ec.getValue("int2").asInteger(), foundInt.get("int2").asInteger());

HashMapStructure foundObj = ec.getValue("obj").asStructure();
Structure foundObj = ec.getValue("obj").asStructure();
assertEquals(1, foundObj.getValue("val1").asInteger());
assertEquals("2", foundObj.getValue("val2").asString());
}
Expand All @@ -111,7 +111,7 @@ public class EvalContextTest {
MutableContext out = ec.add("str", "test")
.add("int", 4)
.add("bool", false)
.add("str", new HashMapStructure());
.add("str", new MutableStructure());
assertEquals(MutableContext.class, out.getClass());
}

Expand All @@ -120,7 +120,7 @@ public class EvalContextTest {
.add("Boolean", (Boolean)null)
.add("String", (String)null)
.add("Double", (Double)null)
.add("Structure", (HashMapStructure)null)
.add("Structure", (MutableStructure)null)
.add("List", (List<Value>)null)
.add("Instant", (Instant)null);
assertEquals(6, ec.asMap().size());
Expand Down Expand Up @@ -172,7 +172,7 @@ public class EvalContextTest {
structureValue.put("structIntegerItem", new Value(1));
structureValue.put("structDoubleItem", new Value(1.2));
structureValue.put("structInstantItem", new Value(Instant.ofEpochSecond(1663331342)));
HashMapStructure structure = new HashMapStructure(structureValue);
Structure structure = new MutableStructure(structureValue);
ctx.add("structureItem", structure);


Expand Down
8 changes: 4 additions & 4 deletions src/test/java/dev/openfeature/sdk/StructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class StructureTest {
@Test public void noArgShouldContainEmptyAttributes() {
HashMapStructure structure = new HashMapStructure();
MutableStructure structure = new MutableStructure();
assertEquals(0, structure.asMap().keySet().size());
}

Expand All @@ -25,7 +25,7 @@ public class StructureTest {
put(KEY, new Value(KEY));
}
};
HashMapStructure structure = new HashMapStructure(map);
MutableStructure structure = new MutableStructure(map);
assertEquals(KEY, structure.asMap().get(KEY).asString());
assertNotSame(structure.asMap(), map); // should be a copy
}
Expand All @@ -45,11 +45,11 @@ public class StructureTest {
int INT_VAL = 13;
double DOUBLE_VAL = .5;
Instant DATE_VAL = Instant.now();
HashMapStructure STRUCT_VAL = new HashMapStructure();
MutableStructure STRUCT_VAL = new MutableStructure();
List<Value> LIST_VAL = new ArrayList<Value>();
Value VALUE_VAL = new Value();

HashMapStructure structure = new HashMapStructure();
MutableStructure structure = new MutableStructure();
structure.add(BOOL_KEY, BOOL_VAL);
structure.add(STRING_KEY, STRING_VAL);
structure.add(INT_KEY, INT_VAL);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/dev/openfeature/sdk/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ValueTest {
list.add(true);
list.add("val");
list.add(.5);
list.add(new HashMapStructure());
list.add(new MutableStructure());
list.add(new ArrayList<Value>());
list.add(Instant.now());

Expand Down Expand Up @@ -96,7 +96,7 @@ class Something {}
@Test public void structureShouldContainStructure() {
String INNER_KEY = "key";
String INNER_VALUE = "val";
HashMapStructure innerValue = new HashMapStructure().add(INNER_KEY, INNER_VALUE);
MutableStructure innerValue = new MutableStructure().add(INNER_KEY, INNER_VALUE);
Value value = new Value(innerValue);
assertTrue(value.isStructure());
assertEquals(INNER_VALUE, value.asStructure().getValue(INNER_KEY).asString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
// import dev.openfeature.contrib.providers.flagd.FlagdProvider;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.FlagEvaluationDetails;
import dev.openfeature.sdk.HashMapStructure;
import dev.openfeature.sdk.MutableStructure;
import dev.openfeature.sdk.MutableContext;
import dev.openfeature.sdk.OpenFeatureAPI;
import dev.openfeature.sdk.Reason;
import dev.openfeature.sdk.Structure;
import dev.openfeature.sdk.Value;
import io.cucumber.java.BeforeAll;
import io.cucumber.java.en.Then;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void an_object_flag_with_key_is_evaluated_with_a_null_default_value(Strin
@Then("the resolved object value should be contain fields {string}, {string}, and {string}, with values {string}, {string} and {int}, respectively")
public void the_resolved_object_value_should_be_contain_fields_and_with_values_and_respectively(String boolField,
String stringField, String numberField, String boolValue, String stringValue, int numberValue) {
HashMapStructure structure = this.objectFlagValue.asStructure();
Structure structure = this.objectFlagValue.asStructure();

assertEquals(Boolean.valueOf(boolValue), structure.asMap().get(boolField).asBoolean());
assertEquals(stringValue, structure.asMap().get(stringField).asString());
Expand Down Expand Up @@ -186,7 +187,7 @@ public void an_object_flag_with_key_is_evaluated_with_details_and_a_null_default
public void the_resolved_object_value_should_be_contain_fields_and_with_values_and_respectively_again(
String boolField,
String stringField, String numberField, String boolValue, String stringValue, int numberValue) {
HashMapStructure structure = this.objectFlagDetails.getValue().asStructure();
Structure structure = this.objectFlagDetails.getValue().asStructure();

assertEquals(Boolean.valueOf(boolValue), structure.asMap().get(boolField).asBoolean());
assertEquals(stringValue, structure.asMap().get(stringField).asString());
Expand Down

0 comments on commit 5418242

Please sign in to comment.