Skip to content

Commit f4f9021

Browse files
gwlesterleandrodamascenasthulb
authored
feat(feature_flags): add intersect actions for conditions (#3692)
Code and docs Co-authored-by: Leandro Damascena <[email protected]> Co-authored-by: Simon Thulbourn <[email protected]>
1 parent 93b4ac6 commit f4f9021

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

aws_lambda_powertools/utilities/feature_flags/comparators.py

+41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from dateutil.tz import gettz
55

66
from .schema import HOUR_MIN_SEPARATOR, ModuloRangeValues, TimeValues
7+
from .exceptions import SchemaValidationError
78

89

910
def _get_now_from_timezone(timezone: Optional[tzinfo]) -> datetime:
@@ -82,3 +83,43 @@ def compare_modulo_range(context_value: int, condition_value: Dict) -> bool:
8283
end = condition_value.get(ModuloRangeValues.END.value, 1)
8384

8485
return start <= context_value % base <= end
86+
87+
88+
def compare_any_in_list(key_list, value_list):
89+
if not (isinstance(key_list, list) and isinstance(value_list, list)):
90+
raise SchemaValidationError()
91+
92+
results = False
93+
for key in key_list:
94+
if key in value_list:
95+
results = True
96+
break
97+
98+
return results
99+
100+
101+
def compare_all_in_list(key_list, value_list):
102+
if not (isinstance(key_list, list) and isinstance(value_list, list)):
103+
raise SchemaValidationError()
104+
105+
results = True
106+
for key in key_list:
107+
if key not in value_list:
108+
results = False
109+
break
110+
111+
return results
112+
113+
114+
def compare_none_in_list(key_list, value_list):
115+
if not (isinstance(key_list, list) and isinstance(value_list, list)):
116+
raise SchemaValidationError()
117+
118+
results = True
119+
for key in key_list:
120+
if key in value_list:
121+
results = False
122+
break
123+
124+
return results
125+

aws_lambda_powertools/utilities/feature_flags/feature_flags.py

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
compare_days_of_week,
1111
compare_modulo_range,
1212
compare_time_range,
13+
compare_all_in_list,
14+
compare_any_in_list,
15+
compare_none_in_list
1316
)
1417
from .exceptions import ConfigurationStoreError
1518

@@ -63,6 +66,9 @@ def _match_by_action(self, action: str, condition_value: Any, context_value: Any
6366
schema.RuleAction.KEY_NOT_IN_VALUE.value: lambda a, b: a not in b,
6467
schema.RuleAction.VALUE_IN_KEY.value: lambda a, b: b in a,
6568
schema.RuleAction.VALUE_NOT_IN_KEY.value: lambda a, b: b not in a,
69+
schema.RuleAction.ALL_IN_VALUE.value: lambda a, b: compare_all_in_list(a, b),
70+
schema.RuleAction.ANY_IN_VALUE.value: lambda a, b: compare_any_in_list(a, b),
71+
schema.RuleAction.NONE_IN_VALUE.value: lambda a, b: compare_none_in_list(a, b),
6672
schema.RuleAction.SCHEDULE_BETWEEN_TIME_RANGE.value: lambda a, b: compare_time_range(a, b),
6773
schema.RuleAction.SCHEDULE_BETWEEN_DATETIME_RANGE.value: lambda a, b: compare_datetime_range(a, b),
6874
schema.RuleAction.SCHEDULE_BETWEEN_DAYS_OF_WEEK.value: lambda a, b: compare_days_of_week(a, b),

aws_lambda_powertools/utilities/feature_flags/schema.py

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class RuleAction(str, Enum):
3838
KEY_NOT_IN_VALUE = "KEY_NOT_IN_VALUE"
3939
VALUE_IN_KEY = "VALUE_IN_KEY"
4040
VALUE_NOT_IN_KEY = "VALUE_NOT_IN_KEY"
41+
ALL_IN_VALUE = "ALL_IN_VALUE"
42+
ANY_IN_VALUE = "ANY_IN_VALUE"
43+
NONE_IN_VALUE = "NONE_IN_VALUE"
4144
SCHEDULE_BETWEEN_TIME_RANGE = "SCHEDULE_BETWEEN_TIME_RANGE" # hour:min 24 hours clock
4245
SCHEDULE_BETWEEN_DATETIME_RANGE = "SCHEDULE_BETWEEN_DATETIME_RANGE" # full datetime format, excluding timezone
4346
SCHEDULE_BETWEEN_DAYS_OF_WEEK = "SCHEDULE_BETWEEN_DAYS_OF_WEEK" # MONDAY, TUESDAY, .... see TimeValues enum

docs/utilities/feature_flags.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,13 @@ The `action` configuration can have the following values, where the expressions
431431
| **ENDSWITH** | `lambda a, b: a.endswith(b)` |
432432
| **KEY_IN_VALUE** | `lambda a, b: a in b` |
433433
| **KEY_NOT_IN_VALUE** | `lambda a, b: a not in b` |
434+
| **ANY_IN_VALUE** | `lambda a, b: any of a is in b` |
435+
| **ALL_IN_VALUE** | `lambda a, b: all of a is in b` |
436+
| **NONE_IN_VALUE** | `lambda a, b: none of a is in b` |
434437
| **VALUE_IN_KEY** | `lambda a, b: b in a` |
435438
| **VALUE_NOT_IN_KEY** | `lambda a, b: b not in a` |
436-
| **SCHEDULE_BETWEEN_TIME_RANGE** | `lambda a, b: b.start <= time(a) <= b.end` |
437-
| **SCHEDULE_BETWEEN_DATETIME_RANGE** | `lambda a, b: b.start <= datetime(a) <= b.end` |
439+
| **SCHEDULE_BETWEEN_TIME_RANGE** | `lambda a, b: b.start <= time(a) <= b.end` |
440+
| **SCHEDULE_BETWEEN_DATETIME_RANGE** | `lambda a, b: b.start <= datetime(a) <= b.end` |
438441
| **SCHEDULE_BETWEEN_DAYS_OF_WEEK** | `lambda a, b: day_of_week(a) in b` |
439442
| **MODULO_RANGE** | `lambda a, b: b.start <= a % b.base <= b.end` |
440443

0 commit comments

Comments
 (0)