3
3
import com .eppo .sdk .dto .EppoValue ;
4
4
import com .eppo .sdk .dto .SubjectAttributes ;
5
5
import com .eppo .sdk .exception .InvalidSubjectAttribute ;
6
+ import com .github .zafarkhaja .semver .Version ;
6
7
import com .eppo .sdk .dto .Condition ;
7
8
import com .eppo .sdk .dto .Rule ;
8
9
@@ -24,18 +25,6 @@ interface IConditionFunc<T> {
24
25
* Compare Class
25
26
*/
26
27
class Compare {
27
- /**
28
- * This function is used to compare number
29
- *
30
- * @param a
31
- * @param b
32
- * @param conditionFunc
33
- * @return
34
- */
35
- public static boolean compareNumber (double a , double b , IConditionFunc <Double > conditionFunc ) {
36
- return conditionFunc .check (a , b );
37
- }
38
-
39
28
/**
40
29
* This function is used to compare Regex
41
30
*
@@ -75,8 +64,7 @@ public class RuleValidator {
75
64
*/
76
65
public static Optional <Rule > findMatchingRule (
77
66
SubjectAttributes subjectAttributes ,
78
- List <Rule > rules
79
- ) {
67
+ List <Rule > rules ) {
80
68
for (Rule rule : rules ) {
81
69
if (RuleValidator .matchesRule (subjectAttributes , rule )) {
82
70
return Optional .of (rule );
@@ -95,9 +83,9 @@ public static Optional<Rule> findMatchingRule(
95
83
*/
96
84
private static boolean matchesRule (
97
85
SubjectAttributes subjectAttributes ,
98
- Rule rule
99
- ) throws InvalidSubjectAttribute {
100
- List < Boolean > conditionEvaluations = RuleValidator . evaluateRuleConditions ( subjectAttributes , rule .getConditions ());
86
+ Rule rule ) throws InvalidSubjectAttribute {
87
+ List < Boolean > conditionEvaluations = RuleValidator . evaluateRuleConditions ( subjectAttributes ,
88
+ rule .getConditions ());
101
89
return !conditionEvaluations .contains (false );
102
90
}
103
91
@@ -111,23 +99,57 @@ private static boolean matchesRule(
111
99
*/
112
100
private static boolean evaluateCondition (
113
101
SubjectAttributes subjectAttributes ,
114
- Condition condition
115
- ) throws InvalidSubjectAttribute {
102
+ Condition condition ) throws InvalidSubjectAttribute {
116
103
if (subjectAttributes .containsKey (condition .attribute )) {
117
104
EppoValue value = subjectAttributes .get (condition .attribute );
105
+ Optional <Version > valueSemVer = Version .tryParse (value .stringValue ());
106
+ Optional <Version > conditionSemVer = Version .tryParse (condition .value .stringValue ());
107
+
118
108
try {
119
109
switch (condition .operator ) {
120
110
case GTE :
121
- return Compare .compareNumber (value .doubleValue (), condition .value .doubleValue ()
122
- , (a , b ) -> a >= b );
111
+ if (value .isNumeric () && condition .value .isNumeric ()) {
112
+ return value .doubleValue () >= condition .value .doubleValue ();
113
+ }
114
+
115
+ if (valueSemVer .isPresent () && conditionSemVer .isPresent ()) {
116
+ return valueSemVer .get ().isHigherThanOrEquivalentTo (conditionSemVer .get ());
117
+ }
118
+
119
+ return false ;
123
120
case GT :
124
- return Compare .compareNumber (value .doubleValue (), condition .value .doubleValue (), (a , b ) -> a > b );
121
+ if (value .isNumeric () && condition .value .isNumeric ()) {
122
+ return value .doubleValue () > condition .value .doubleValue ();
123
+ }
124
+
125
+ if (valueSemVer .isPresent () && conditionSemVer .isPresent ()) {
126
+ return valueSemVer .get ().isHigherThan (conditionSemVer .get ());
127
+ }
128
+
129
+ return false ;
125
130
case LTE :
126
- return Compare .compareNumber (value .doubleValue (), condition .value .doubleValue (), (a , b ) -> a <= b );
131
+ if (value .isNumeric () && condition .value .isNumeric ()) {
132
+ return value .doubleValue () <= condition .value .doubleValue ();
133
+ }
134
+
135
+ if (valueSemVer .isPresent () && conditionSemVer .isPresent ()) {
136
+ return valueSemVer .get ().isLowerThanOrEquivalentTo (conditionSemVer .get ());
137
+ }
138
+
139
+ return false ;
127
140
case LT :
128
- return Compare .compareNumber (value .doubleValue (), condition .value .doubleValue (), (a , b ) -> a < b );
141
+ if (value .isNumeric () && condition .value .isNumeric ()) {
142
+ return value .doubleValue () < condition .value .doubleValue ();
143
+ }
144
+
145
+ if (valueSemVer .isPresent () && conditionSemVer .isPresent ()) {
146
+ return valueSemVer .get ().isLowerThan (conditionSemVer .get ());
147
+ }
148
+
149
+ return false ;
129
150
case MATCHES :
130
- return Compare .compareRegex (value .stringValue (), Pattern .compile (condition .value .stringValue ()));
151
+ return Compare .compareRegex (value .stringValue (),
152
+ Pattern .compile (condition .value .stringValue ()));
131
153
case ONE_OF :
132
154
return Compare .isOneOf (value .stringValue (), condition .value .arrayValue ());
133
155
case NOT_ONE_OF :
@@ -151,8 +173,7 @@ private static boolean evaluateCondition(
151
173
*/
152
174
private static List <Boolean > evaluateRuleConditions (
153
175
SubjectAttributes subjectAttributes ,
154
- List <Condition > conditions
155
- ) throws InvalidSubjectAttribute {
176
+ List <Condition > conditions ) throws InvalidSubjectAttribute {
156
177
return conditions .stream ()
157
178
.map ((condition ) -> {
158
179
try {
0 commit comments