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

Add semver support (FF-1573) #37

Merged
merged 4 commits into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions eppo/build.gradle
Expand Up @@ -71,6 +71,7 @@ dependencies {
androidTestImplementation "commons-io:commons-io:${versions.commonsio}"
implementation("com.google.code.gson:gson:${versions.gson}")
implementation("com.squareup.okhttp3:okhttp:${versions.okhttp}")
implementation("com.github.zafarkhaja:java-semver:0.10.2")
}

publishing {
Expand Down
53 changes: 43 additions & 10 deletions eppo/src/main/java/cloud/eppo/android/RuleEvaluator.java
@@ -1,9 +1,10 @@
package cloud.eppo.android;

import com.github.zafarkhaja.semver.Version;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import cloud.eppo.android.dto.EppoValue;
import cloud.eppo.android.dto.SubjectAttributes;
Expand All @@ -15,10 +16,6 @@ interface IConditionFunc<T> {
}

class Compare {
public static boolean compareNumber(double a, double b, IConditionFunc<Double> conditionFunc) {
return conditionFunc.check(a, b);
}
Comment on lines -18 to -20
Copy link
Contributor

Choose a reason for hiding this comment

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

🪓


public static boolean compareRegex(String a, Pattern pattern) {
return pattern.matcher(a).matches();
}
Expand Down Expand Up @@ -48,21 +45,57 @@ private static boolean matchesRule(SubjectAttributes subjectAttributes, Targetin
return !conditionEvaluations.contains(false);
}



private static boolean evaluateCondition(SubjectAttributes subjectAttributes, TargetingCondition condition
) {
if (subjectAttributes.containsKey(condition.getAttribute())) {
EppoValue value = subjectAttributes.get(condition.getAttribute());
Boolean isValueSemVer = Version.isValid(value.stringValue());
Boolean isConditionSemVer = Version.isValid(condition.getValue().stringValue());

try {
switch (condition.getOperator()) {
case GreaterThanEqualTo:
return Compare.compareNumber(value.doubleValue(), condition.getValue().doubleValue()
Copy link
Member Author

Choose a reason for hiding this comment

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

deleted compareNumber helper

Copy link
Contributor

Choose a reason for hiding this comment

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

🙌

, (a, b) -> a >= b);
if (value.isNumeric() && condition.getValue().isNumeric()) {
leoromanovsky marked this conversation as resolved.
Show resolved Hide resolved
return value.doubleValue() >= condition.getValue().doubleValue();
}

if (isValueSemVer && isConditionSemVer) {
return Version.parse(value.stringValue()).isHigherThanOrEquivalentTo(Version.parse(condition.getValue().stringValue()));
Copy link
Member Author

Choose a reason for hiding this comment

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

since we passed isValid we should be confident that parse will succeed without an exception

}

return false;
case GreaterThan:
return Compare.compareNumber(value.doubleValue(), condition.getValue().doubleValue(), (a, b) -> a > b);
if (value.isNumeric() && condition.getValue().isNumeric()) {
return value.doubleValue() > condition.getValue().doubleValue();
}

if (isValueSemVer && isConditionSemVer) {
return Version.parse(value.stringValue()).isHigherThan(Version.parse(condition.getValue().stringValue()));
}

return false;
case LessThanEqualTo:
return Compare.compareNumber(value.doubleValue(), condition.getValue().doubleValue(), (a, b) -> a <= b);
if (value.isNumeric() && condition.getValue().isNumeric()) {
return value.doubleValue() <= condition.getValue().doubleValue();
}

if (isValueSemVer && isConditionSemVer) {
return Version.parse(value.stringValue()).isLowerThanOrEquivalentTo(Version.parse(condition.getValue().stringValue()));
}

return false;
case LessThan:
return Compare.compareNumber(value.doubleValue(), condition.getValue().doubleValue(), (a, b) -> a < b);
if (value.isNumeric() && condition.getValue().isNumeric()) {
return value.doubleValue() < condition.getValue().doubleValue();
}

if (isValueSemVer && isConditionSemVer) {
return Version.parse(value.stringValue()).isLowerThan(Version.parse(condition.getValue().stringValue()));
}

return false;
case Matches:
return Compare.compareRegex(value.stringValue(), Pattern.compile(condition.getValue().stringValue()));
case OneOf:
Expand Down
27 changes: 27 additions & 0 deletions eppo/src/test/java/cloud/eppo/android/RuleEvaluatorTest.java
Expand Up @@ -43,6 +43,21 @@ public void addNumericConditionToRule(TargetingRule TargetingRule) {
addConditionToRule(TargetingRule, condition2);
}

public void addSemVerConditionToRule(TargetingRule TargetingRule) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

TargetingCondition condition1 = new TargetingCondition();
condition1.setValue(EppoValue.valueOf("1.5.0"));
condition1.setAttribute("appVersion");
condition1.setOperator(OperatorType.GreaterThanEqualTo);

TargetingCondition condition2 = new TargetingCondition();
condition2.setValue(EppoValue.valueOf("2.2.0"));
condition2.setAttribute("appVersion");
condition2.setOperator(OperatorType.LessThan);

addConditionToRule(TargetingRule, condition1);
addConditionToRule(TargetingRule, condition2);
}

public void addRegexConditionToRule(TargetingRule TargetingRule) {
TargetingCondition condition = new TargetingCondition();
condition.setValue(EppoValue.valueOf("[a-z]+"));
Expand Down Expand Up @@ -131,6 +146,18 @@ public void testMatchesAnyRuleWhenRuleMatches() {
assertEquals(targetingRule, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}

@Test
public void testMatchesAnyRuleWhenRuleMatchesWithSemVer() {
List<TargetingRule> targetingRules = new ArrayList<>();
TargetingRule targetingRule = createRule(new ArrayList<>());
addSemVerConditionToRule(targetingRule);
targetingRules.add(targetingRule);

SubjectAttributes subjectAttributes = new SubjectAttributes();
subjectAttributes.put("appVersion", "1.15.5");
Copy link
Member Author

Choose a reason for hiding this comment

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

15 > 5


assertEquals(targetingRule, RuleEvaluator.findMatchingRule(subjectAttributes, targetingRules));
}

@Test
public void testMatchesAnyRuleWhenThrowInvalidSubjectAttribute() {
Expand Down