-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function decorator for feature flags.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Any, Callable | ||
|
||
from injector import Injector, inject | ||
from typing_extensions import overload | ||
|
||
from .feature_flag_router import FeatureFlag, FeatureFlagRouter | ||
|
||
|
||
@overload | ||
def feature_flag( | ||
feature_flag_name: str, *, enabled_callback: Callable[..., Any] | ||
) -> Callable[..., Callable[..., Any]]: ... | ||
@overload | ||
def feature_flag( | ||
feature_flag_name: str, *, disabled_callback: Callable[..., Any] | ||
) -> Callable[..., Callable[..., Any]]: ... | ||
|
||
|
||
def feature_flag( | ||
feature_flag_name: str, | ||
*, | ||
enabled_callback: Callable[..., None] = lambda: None, | ||
disabled_callback: Callable[..., None] = lambda: None, | ||
) -> Callable[..., Callable[..., Any]]: | ||
def decorator(fn: Callable[..., Any]): | ||
@inject | ||
def wrapper( | ||
feature_flag_router: FeatureFlagRouter[FeatureFlag], | ||
injector: Injector, | ||
): | ||
if feature_flag_router.feature_is_enabled(feature_flag_name): | ||
enabled_callback() | ||
else: | ||
disabled_callback() | ||
|
||
return injector.call_with_injection(fn) | ||
|
||
return wrapper | ||
|
||
return decorator |