Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO TASK] Fix oneOf comparisons for both Booleans and Numbers (#14) #54

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
21 changes: 20 additions & 1 deletion src/main/java/com/eppo/sdk/dto/EppoValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,32 @@ public boolean isNull() {
return type == EppoValueType.NULL;
}

/**
* Converts the EppoValue into a string representation.
* NOTE: Take care when updating this method as it's currently used
* by the IN and NOT IN target rule evaluations.
*
* @return String the string representation of the EppoValue
*/
@Override
public String toString() {
switch(this.type) {
case STRING:
return this.stringValue;
case NUMBER:
return this.doubleValue.toString();
// By default, `String.valueOf(<double>)` will include at least one decimal place.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Appreciate the comment including the documentation link!

// Though numeric flags can either be integers or floating-point types. And target
// rule logic will cast a number type to a String before evaluating `oneOf` or `notOneOf`
// rules.
// The logic below ensures the cast to string better represents the intended numeric
// field type.
//
// @see https://docs.geteppo.com/feature-flagging/flag-variations#numeric-flags
// @see https://docs.geteppo.com/feature-flagging/targeting#supported-rule-operators
if (this.doubleValue.intValue() == this.doubleValue) {
return String.valueOf(this.doubleValue.intValue());
}
return String.valueOf(this.doubleValue);
case BOOLEAN:
return this.boolValue.toString();
case ARRAY_OF_STRING:
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/eppo/sdk/helpers/RuleValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ private static boolean evaluateCondition(
return Compare.compareRegex(value.stringValue(),
Pattern.compile(condition.value.stringValue()));
case ONE_OF:
return Compare.isOneOf(value.stringValue(), condition.value.arrayValue());
return Compare.isOneOf(value.toString(), condition.value.arrayValue());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

case NOT_ONE_OF:
return !Compare.isOneOf(value.stringValue(), condition.value.arrayValue());
return !Compare.isOneOf(value.toString(), condition.value.arrayValue());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

}
} catch (Exception e) {
return false;
Expand Down
94 changes: 94 additions & 0 deletions src/test/java/com/eppo/sdk/helpers/RuleValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ public void addOneOfCondition(Rule rule) {
addConditionToRule(rule, condition);
}

public void addOneOfConditionWithIntegers(Rule rule) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Condition condition = new Condition();
List<String> values = new ArrayList<>();
values.add("1");
values.add("2");

condition.value = EppoValue.valueOf(values);
condition.attribute = "oneOf";
condition.operator = OperatorType.ONE_OF;

addConditionToRule(rule, condition);
}

public void addOneOfConditionWithDoubles(Rule rule) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Condition condition = new Condition();
List<String> values = new ArrayList<>();
values.add("1.5");
values.add("2.7");

condition.value = EppoValue.valueOf(values);
condition.attribute = "oneOf";
condition.operator = OperatorType.ONE_OF;

addConditionToRule(rule, condition);
}

public void addOneOfConditionWithBoolean(Rule rule) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

Condition condition = new Condition();
List<String> values = new ArrayList<>();
values.add("true");

condition.value = EppoValue.valueOf(values);
condition.attribute = "oneOf";
condition.operator = OperatorType.ONE_OF;

addConditionToRule(rule, condition);
}

public void addNotOneOfCondition(Rule rule) {
Condition condition = new Condition();
List<String> values = new ArrayList<>();
Expand Down Expand Up @@ -226,4 +264,60 @@ void testMatchesAnyRuleWithNotOneOfRuleNotPassed() {
Assertions.assertFalse(RuleValidator.findMatchingRule(subjectAttributes, rules).isPresent());
}

@DisplayName("findMatchingRule() with oneOf rule on a string")
@Test
void testMatchesAnyRuleWithOneOfRuleOnString() {
List<Rule> rules = new ArrayList<>();
Rule rule = createRule(new ArrayList<>());
addOneOfCondition(rule);
rules.add(rule);

EppoAttributes subjectAttributes = new EppoAttributes();
subjectAttributes.put("oneOf", EppoValue.valueOf("value1"));

Assertions.assertTrue(RuleValidator.findMatchingRule(subjectAttributes, rules).isPresent());
}

@DisplayName("findMatchingRule() with oneOf rule on an integer")
@Test
void testMatchesAnyRuleWithOneOfRuleOnInteger() {
List<Rule> rules = new ArrayList<>();
Rule rule = createRule(new ArrayList<>());
addOneOfConditionWithIntegers(rule);
rules.add(rule);

EppoAttributes subjectAttributes = new EppoAttributes();
subjectAttributes.put("oneOf", EppoValue.valueOf(2));

Assertions.assertTrue(RuleValidator.findMatchingRule(subjectAttributes, rules).isPresent());
}

@DisplayName("findMatchingRule() with oneOf rule on a double")
@Test
void testMatchesAnyRuleWithOneOfRuleOnDouble() {
List<Rule> rules = new ArrayList<>();
Rule rule = createRule(new ArrayList<>());
addOneOfConditionWithDoubles(rule);
rules.add(rule);

EppoAttributes subjectAttributes = new EppoAttributes();
subjectAttributes.put("oneOf", EppoValue.valueOf(1.5));

Assertions.assertTrue(RuleValidator.findMatchingRule(subjectAttributes, rules).isPresent());
}

@DisplayName("findMatchingRule() with oneOf rule on a boolean")
@Test
void testMatchesAnyRuleWithOneOfRuleOnBoolean() {
List<Rule> rules = new ArrayList<>();
Rule rule = createRule(new ArrayList<>());
addOneOfConditionWithBoolean(rule);
rules.add(rule);

EppoAttributes subjectAttributes = new EppoAttributes();
subjectAttributes.put("oneOf", EppoValue.valueOf(true));

Assertions.assertTrue(RuleValidator.findMatchingRule(subjectAttributes, rules).isPresent());
}

}