Skip to content

Commit 1b202be

Browse files
committed
refactor: rename toggles to flags
1 parent d8125c7 commit 1b202be

File tree

6 files changed

+30
-37
lines changed

6 files changed

+30
-37
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
types: [python]
2525
- id: isort
2626
name: formatting::isort
27-
entry: poetry run isort -rc
27+
entry: poetry run isort
2828
language: system
2929
types: [python]
3030
- repo: local

aws_lambda_powertools/utilities/feature_flags/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""Advanced feature toggles utility
2-
"""
1+
"""Advanced feature flags utility"""
32
from .appconfig import AppConfigStore
43
from .base import StoreProvider
54
from .exceptions import ConfigurationStoreError

aws_lambda_powertools/utilities/feature_flags/appconfig.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,20 @@ def get_configuration(self) -> Dict[str, Any]:
6969
"""
7070
try:
7171
# parse result conf as JSON, keep in cache for self.max_age seconds
72-
config = self._conf_store.get(
73-
name=self.name,
74-
transform=TRANSFORM_TYPE,
75-
max_age=self.cache_seconds,
72+
config = cast(
73+
dict,
74+
self._conf_store.get(
75+
name=self.name,
76+
transform=TRANSFORM_TYPE,
77+
max_age=self.cache_seconds,
78+
),
7679
)
7780

7881
if self.envelope:
7982
config = jmespath_utils.unwrap_event_from_envelope(
8083
data=config, envelope=self.envelope, jmespath_options=self.jmespath_options
8184
)
8285

83-
return cast(dict, config)
86+
return config
8487
except (GetParameterError, TransformParameterError) as exc:
8588
raise ConfigurationStoreError("Unable to get AWS AppConfig configuration file") from exc

tests/functional/feature_toggles/test_feature_toggles.py renamed to tests/functional/feature_flags/test_feature_flags.py

+20-29
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
from aws_lambda_powertools.utilities.feature_flags import ConfigurationStoreError, schema
77
from aws_lambda_powertools.utilities.feature_flags.appconfig import AppConfigStore
88
from aws_lambda_powertools.utilities.feature_flags.feature_flags import FeatureFlags
9-
from aws_lambda_powertools.utilities.feature_flags.schema import (
10-
CONDITION_ACTION,
11-
CONDITION_KEY,
12-
CONDITION_VALUE,
13-
CONDITIONS_KEY,
14-
FEATURE_DEFAULT_VAL_KEY,
15-
RULE_MATCH_VALUE,
16-
RULES_KEY,
17-
RuleAction,
18-
)
9+
from aws_lambda_powertools.utilities.feature_flags.schema import RuleAction
1910
from aws_lambda_powertools.utilities.parameters import GetParameterError
2011

2112

@@ -57,7 +48,7 @@ def init_fetcher_side_effect(mocker, config: Config, side_effect) -> AppConfigSt
5748

5849
# this test checks that we get correct value of feature that exists in the schema.
5950
# we also don't send an empty context dict in this case
60-
def test_toggles_rule_does_not_match(mocker, config):
51+
def test_flags_rule_does_not_match(mocker, config):
6152
expected_value = True
6253
mocked_app_config_schema = {
6354
"my_feature": {
@@ -84,7 +75,7 @@ def test_toggles_rule_does_not_match(mocker, config):
8475

8576
# this test checks that if you try to get a feature that doesn't exist in the schema,
8677
# you get the default value of False that was sent to the evaluate API
87-
def test_toggles_no_conditions_feature_does_not_exist(mocker, config):
78+
def test_flags_no_conditions_feature_does_not_exist(mocker, config):
8879
expected_value = False
8980
mocked_app_config_schema = {"my_fake_feature": {"default": True}}
9081

@@ -95,7 +86,7 @@ def test_toggles_no_conditions_feature_does_not_exist(mocker, config):
9586

9687
# check that feature match works when they are no rules and we send context.
9788
# default value is False but the feature has a True default_value.
98-
def test_toggles_no_rules(mocker, config):
89+
def test_flags_no_rules(mocker, config):
9990
expected_value = True
10091
mocked_app_config_schema = {"my_feature": {"default": expected_value}}
10192
feature_flags = init_feature_flags(mocker, mocked_app_config_schema, config)
@@ -104,7 +95,7 @@ def test_toggles_no_rules(mocker, config):
10495

10596

10697
# check a case where the feature exists but the rule doesn't match so we revert to the default value of the feature
107-
def test_toggles_conditions_no_match(mocker, config):
98+
def test_flags_conditions_no_match(mocker, config):
10899
expected_value = True
109100
mocked_app_config_schema = {
110101
"my_feature": {
@@ -129,7 +120,7 @@ def test_toggles_conditions_no_match(mocker, config):
129120

130121

131122
# check that a rule can match when it has multiple conditions, see rule name for further explanation
132-
def test_toggles_conditions_rule_match_equal_multiple_conditions(mocker, config):
123+
def test_flags_conditions_rule_match_equal_multiple_conditions(mocker, config):
133124
expected_value = False
134125
tenant_id_val = "6"
135126
username_val = "a"
@@ -170,7 +161,7 @@ def test_toggles_conditions_rule_match_equal_multiple_conditions(mocker, config)
170161
# check a case when rule doesn't match and it has multiple conditions,
171162
# different tenant id causes the rule to not match.
172163
# default value of the feature in this case is True
173-
def test_toggles_conditions_no_rule_match_equal_multiple_conditions(mocker, config):
164+
def test_flags_conditions_no_rule_match_equal_multiple_conditions(mocker, config):
174165
expected_val = True
175166
mocked_app_config_schema = {
176167
"my_feature": {
@@ -201,7 +192,7 @@ def test_toggles_conditions_no_rule_match_equal_multiple_conditions(mocker, conf
201192

202193

203194
# check rule match for multiple of action types
204-
def test_toggles_conditions_rule_match_multiple_actions_multiple_rules_multiple_conditions(mocker, config):
195+
def test_flags_conditions_rule_match_multiple_actions_multiple_rules_multiple_conditions(mocker, config):
205196
expected_value_first_check = True
206197
expected_value_second_check = False
207198
expected_value_third_check = False
@@ -271,7 +262,7 @@ def test_toggles_conditions_rule_match_multiple_actions_multiple_rules_multiple_
271262

272263

273264
# check a case where the feature exists but the rule doesn't match so we revert to the default value of the feature
274-
def test_toggles_match_rule_with_contains_action(mocker, config):
265+
def test_flags_match_rule_with_contains_action(mocker, config):
275266
expected_value = True
276267
mocked_app_config_schema = {
277268
"my_feature": {
@@ -295,7 +286,7 @@ def test_toggles_match_rule_with_contains_action(mocker, config):
295286
assert toggle == expected_value
296287

297288

298-
def test_toggles_no_match_rule_with_contains_action(mocker, config):
289+
def test_flags_no_match_rule_with_contains_action(mocker, config):
299290
expected_value = False
300291
mocked_app_config_schema = {
301292
"my_feature": {
@@ -407,16 +398,16 @@ def test_get_feature_toggle_handles_error(mocker, config):
407398
assert toggle is False
408399

409400

410-
def test_get_all_enabled_feature_toggles_handles_error(mocker, config):
401+
def test_get_all_enabled_feature_flags_handles_error(mocker, config):
411402
# GIVEN a schema fetch that raises a ConfigurationStoreError
412403
schema_fetcher = init_fetcher_side_effect(mocker, config, GetParameterError())
413404
feature_flags = FeatureFlags(schema_fetcher)
414405

415406
# WHEN calling get_enabled_features
416-
toggles = feature_flags.get_enabled_features(context=None)
407+
flags = feature_flags.get_enabled_features(context=None)
417408

418409
# THEN handle the error and return an empty list
419-
assert toggles == []
410+
assert flags == []
420411

421412

422413
def test_app_config_get_parameter_err(mocker, config):
@@ -477,15 +468,15 @@ def test_match_condition_with_dict_value(mocker, config):
477468
expected_value = True
478469
mocked_app_config_schema = {
479470
"my_feature": {
480-
FEATURE_DEFAULT_VAL_KEY: False,
481-
RULES_KEY: {
471+
"default": False,
472+
"rules": {
482473
"tenant id is 6 and username is lessa": {
483-
RULE_MATCH_VALUE: expected_value,
484-
CONDITIONS_KEY: [
474+
"when_match": expected_value,
475+
"conditions": [
485476
{
486-
CONDITION_ACTION: RuleAction.EQUALS.value,
487-
CONDITION_KEY: "tenant",
488-
CONDITION_VALUE: {"tenant_id": "6", "username": "lessa"},
477+
"action": RuleAction.EQUALS.value,
478+
"key": "tenant",
479+
"value": {"tenant_id": "6", "username": "lessa"},
489480
}
490481
],
491482
}

0 commit comments

Comments
 (0)