-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path_feature_conditions.py
More file actions
73 lines (62 loc) · 2.6 KB
/
_feature_conditions.py
File metadata and controls
73 lines (62 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
"""Feature flag condition models."""
from collections.abc import Mapping
from typing import Any, Dict, List
from ._constants import (
FEATURE_FLAG_CLIENT_FILTERS,
FEATURE_FILTER_NAME,
FEATURE_FILTER_REQUIREMENT_TYPE,
REQUIREMENT_TYPE_ALL,
REQUIREMENT_TYPE_ANY,
)
class FeatureConditions:
"""
Represents the conditions for a feature flag.
"""
def __init__(self) -> None:
self._requirement_type = REQUIREMENT_TYPE_ANY
self._client_filters: List[Dict[str, Any]] = []
@classmethod
def convert_from_json(cls, feature_name: str, json_value: str) -> "FeatureConditions":
"""
Convert a JSON object to FeatureConditions.
:param dict json: JSON object.
:return: FeatureConditions.
:rtype: FeatureConditions
"""
conditions = cls()
if json_value is not None and not isinstance(json_value, Mapping):
raise AttributeError("Feature flag conditions must be a dictionary")
conditions._requirement_type = json_value.get(FEATURE_FILTER_REQUIREMENT_TYPE, REQUIREMENT_TYPE_ANY)
conditions._client_filters = json_value.get(FEATURE_FLAG_CLIENT_FILTERS, [])
if not isinstance(conditions._client_filters, list):
conditions._client_filters = []
for feature_filter in conditions._client_filters:
feature_filter["feature_name"] = feature_name
return conditions
@property
def requirement_type(self) -> str:
"""
Get the requirement type for the feature flag.
:return: Requirement type.
:rtype: str
"""
return self._requirement_type
@property
def client_filters(self) -> List[Dict[str, Any]]:
"""
Get the client filters for the feature flag.
:return: Client filters.
:rtype: list[dict]
"""
return self._client_filters
def _validate(self, feature_flag_id: str) -> None:
if self._requirement_type not in [REQUIREMENT_TYPE_ALL, REQUIREMENT_TYPE_ANY]:
raise ValueError(f"Feature flag {feature_flag_id} has invalid requirement type.")
for feature_filter in self._client_filters:
if feature_filter.get(FEATURE_FILTER_NAME) is None:
raise ValueError(f"Feature flag {feature_flag_id} is missing filter name.")