Skip to content

Commit d8bf5e0

Browse files
committed
add proposed user-mapping-by-action to feature-flags
1 parent 79414c8 commit d8bf5e0

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

aws_lambda_powertools/utilities/feature_flags/feature_flags.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Dict, List, Optional, Union, cast
2+
from typing import Any, Callable, Dict, List, Optional, Union, cast
33

44
from . import schema
55
from .base import StoreProvider
@@ -9,7 +9,7 @@
99

1010

1111
class FeatureFlags:
12-
def __init__(self, store: StoreProvider):
12+
def __init__(self, store: StoreProvider, user_mapping_by_action: Optional[Dict[str, Callable]] = None):
1313
"""Evaluates whether feature flags should be enabled based on a given context.
1414
1515
It uses the provided store to fetch feature flag rules before evaluating them.
@@ -35,20 +35,32 @@ def __init__(self, store: StoreProvider):
3535
----------
3636
store: StoreProvider
3737
Store to use to fetch feature flag schema configuration.
38+
user_mapping_by_action: Dict[str, Callable]
39+
User mapping of actions to a callable (context_value, condition_value)
3840
"""
3941
self._store = store
42+
self._user_mapping_by_action = user_mapping_by_action
4043

4144
@staticmethod
42-
def _match_by_action(action: str, condition_value: Any, context_value: Any) -> bool:
45+
def _match_by_action(
46+
action: str,
47+
condition_value: Any,
48+
context_value: Any,
49+
user_mapping_by_action: Optional[Dict[str, Callable]] = None,
50+
) -> bool:
4351
if not context_value:
4452
return False
45-
mapping_by_action = {
53+
_mapping_by_action = {
4654
schema.RuleAction.EQUALS.value: lambda a, b: a == b,
4755
schema.RuleAction.STARTSWITH.value: lambda a, b: a.startswith(b),
4856
schema.RuleAction.ENDSWITH.value: lambda a, b: a.endswith(b),
4957
schema.RuleAction.IN.value: lambda a, b: a in b,
5058
schema.RuleAction.NOT_IN.value: lambda a, b: a not in b,
5159
}
60+
mapping_by_action = {}
61+
if user_mapping_by_action:
62+
mapping_by_action.update(user_mapping_by_action)
63+
mapping_by_action.update(_mapping_by_action)
5264

5365
try:
5466
func = mapping_by_action.get(action, lambda a, b: False)
@@ -76,7 +88,12 @@ def _evaluate_conditions(
7688
cond_action = condition.get(schema.CONDITION_ACTION, "")
7789
cond_value = condition.get(schema.CONDITION_VALUE)
7890

79-
if not self._match_by_action(action=cond_action, condition_value=cond_value, context_value=context_value):
91+
if not self._match_by_action(
92+
action=cond_action,
93+
condition_value=cond_value,
94+
context_value=context_value,
95+
user_mapping_by_action=self._user_mapping_by_action,
96+
):
8097
logger.debug(
8198
f"rule did not match action, rule_name={rule_name}, rule_value={rule_match_value}, "
8299
f"name={feature_name}, context_value={str(context_value)} "

0 commit comments

Comments
 (0)