1
1
import numbers
2
2
import re
3
+ import semver
3
4
from enum import Enum
4
5
from typing import Any , List
5
6
@@ -55,9 +56,13 @@ def evaluate_condition(subject_attributes: dict, condition: Condition) -> bool:
55
56
value .lower () for value in condition .value
56
57
]
57
58
else :
58
- return isinstance (
59
- subject_value , numbers .Number
60
- ) and evaluate_numeric_condition (subject_value , condition )
59
+ # Numeric operator: value could be numeric or semver.
60
+ if isinstance (subject_value , numbers .Number ):
61
+ return evaluate_numeric_condition (subject_value , condition )
62
+ elif is_valid_semver (subject_value ):
63
+ return compare_semver (
64
+ subject_value , condition .value , condition .operator
65
+ )
61
66
return False
62
67
63
68
@@ -70,4 +75,31 @@ def evaluate_numeric_condition(subject_value: numbers.Number, condition: Conditi
70
75
return subject_value < condition .value
71
76
elif condition .operator == OperatorType .LTE :
72
77
return subject_value <= condition .value
78
+
79
+ return False
80
+
81
+
82
+ def is_valid_semver (value : str ):
83
+ try :
84
+ # Parse the string. If it's a valid semver, it will return without errors.
85
+ semver .VersionInfo .parse (value )
86
+ return True
87
+ except ValueError :
88
+ # If a ValueError is raised, the string is not a valid semver.
89
+ return False
90
+
91
+
92
+ def compare_semver (attribute_value : Any , condition_value : Any , operator : OperatorType ):
93
+ if not is_valid_semver (attribute_value ) or not is_valid_semver (condition_value ):
94
+ return False
95
+
96
+ if operator == OperatorType .GT :
97
+ return semver .compare (attribute_value , condition_value ) > 0
98
+ elif operator == OperatorType .GTE :
99
+ return semver .compare (attribute_value , condition_value ) >= 0
100
+ elif operator == OperatorType .LT :
101
+ return semver .compare (attribute_value , condition_value ) < 0
102
+ elif operator == OperatorType .LTE :
103
+ return semver .compare (attribute_value , condition_value ) <= 0
104
+
73
105
return False
0 commit comments