Skip to content

Commit 8ed3a66

Browse files
committed
docs: new section for non boolean flags
1 parent 9a335f0 commit 8ed3a66

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

aws_lambda_powertools/utilities/feature_flags/schema.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SchemaValidator(BaseValidator):
5555
5656
`JSONType` being any JSON primitive value: `Union[str, int, float, bool, None, Dict[str, Any], List[Any]]`
5757
58-
```python
58+
```json
5959
{
6060
"my_feature": {
6161
"default": True,
@@ -77,10 +77,10 @@ class SchemaValidator(BaseValidator):
7777
* **when_match**: `Union[bool, JSONType]`. Defines value to return when context matches conditions
7878
* **conditions**: `List[Dict]`. Conditions object. This MUST be present
7979
80-
```python
80+
```json
8181
{
8282
"my_feature": {
83-
"default": True,
83+
"default": true,
8484
"rules": {
8585
"tenant id equals 345345435": {
8686
"when_match": False,
@@ -90,7 +90,7 @@ class SchemaValidator(BaseValidator):
9090
},
9191
"my_non_boolean_feature": {
9292
"default": {"group": "read-only"},
93-
"boolean_type": False,
93+
"boolean_type": false,
9494
"rules": {
9595
"tenant id equals 345345435": {
9696
"when_match": {"group": "admin"},
@@ -113,13 +113,13 @@ class SchemaValidator(BaseValidator):
113113
* **key**: `str`. Key in given context to perform operation
114114
* **value**: `Any`. Value in given context that should match action operation.
115115
116-
```python
116+
```json
117117
{
118118
"my_feature": {
119-
"default": True,
119+
"default": true,
120120
"rules": {
121121
"tenant id equals 345345435": {
122-
"when_match": False,
122+
"when_match": false,
123123
"conditions": [
124124
{
125125
"action": "EQUALS",

docs/utilities/feature_flags.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Feature flags are used to modify behaviour without changing the application's co
1414

1515
**Static flags**. Indicates something is simply `on` or `off`, for example `TRACER_ENABLED=True`.
1616

17-
**Dynamic flags**. Indicates something can have varying states, for example enable a premium feature for customer X not Y.
17+
**Dynamic flags**. Indicates something can have varying states, for example enable a list of premium features for customer X not Y.
1818

1919
???+ tip
2020
You can use [Parameters utility](parameters.md) for static flags while this utility can do both static and dynamic feature flags.
@@ -380,6 +380,71 @@ You can use `get_enabled_features` method for scenarios where you need a list of
380380
}
381381
```
382382

383+
### Beyond boolean feature flags
384+
385+
???+ info "When is this useful?"
386+
You might have a list of features to unlock for premium customers, unlock a specific set of features for admin users, etc.
387+
388+
Feature flags can return any JSON values when `boolean_type` parameter is set to `False`. These can be dictionaries, list, string, integers, etc.
389+
390+
391+
=== "app.py"
392+
393+
```python hl_lines="3 9 13 16 18"
394+
from aws_lambda_powertools.utilities.feature_flags import FeatureFlags, AppConfigStore
395+
396+
app_config = AppConfigStore(
397+
environment="dev",
398+
application="product-catalogue",
399+
name="features"
400+
)
401+
402+
feature_flags = FeatureFlags(store=app_config)
403+
404+
def lambda_handler(event, context):
405+
# Get customer's tier from incoming request
406+
ctx = { "tier": event.get("tier", "standard") }
407+
408+
# Evaluate `has_premium_features` base don customer's tier
409+
premium_features: list[str] = feature_flags.evaluate(name="premium_features",
410+
context=ctx, default=False)
411+
for feature in premium_features:
412+
# enable premium features
413+
...
414+
```
415+
416+
=== "event.json"
417+
418+
```json hl_lines="3"
419+
{
420+
"username": "lessa",
421+
"tier": "premium",
422+
"basked_id": "random_id"
423+
}
424+
```
425+
=== "features.json"
426+
427+
```json hl_lines="3-4 7"
428+
{
429+
"premium_features": {
430+
"boolean_type": false,
431+
"default": [],
432+
"rules": {
433+
"customer tier equals premium": {
434+
"when_match": ["no_ads", "no_limits", "chat"],
435+
"conditions": [
436+
{
437+
"action": "EQUALS",
438+
"key": "tier",
439+
"value": "premium"
440+
}
441+
]
442+
}
443+
}
444+
}
445+
}
446+
```
447+
383448
## Advanced
384449

385450
### Adjusting in-memory cache
@@ -436,11 +501,11 @@ A feature can simply have its name and a `default` value. This is either on or o
436501
```json hl_lines="2-3 5-7" title="minimal_schema.json"
437502
{
438503
"global_feature": {
439-
"default": True
504+
"default": true
440505
},
441506
"non_boolean_global_feature": {
442507
"default": {"group": "read-only"},
443-
"boolean_type": False
508+
"boolean_type": false
444509
},
445510
}
446511
```

0 commit comments

Comments
 (0)