-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodels.py
More file actions
42 lines (31 loc) · 1.32 KB
/
models.py
File metadata and controls
42 lines (31 loc) · 1.32 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
import typing
from pydantic import BaseModel, BeforeValidator, Field
from typing_extensions import Annotated
from flag_engine.features.models import FeatureStateModel
from flag_engine.segments import constants
from flag_engine.segments.types import ConditionOperator, RuleType
LaxStr = Annotated[str, BeforeValidator(lambda x: str(x))]
class SegmentConditionModel(BaseModel):
operator: ConditionOperator
value: typing.Optional[LaxStr] = None
property_: typing.Optional[str] = None
class SegmentRuleModel(BaseModel):
type: RuleType
rules: typing.List["SegmentRuleModel"] = Field(default_factory=list)
conditions: typing.List[SegmentConditionModel] = Field(default_factory=list)
@staticmethod
def none(iterable: typing.Iterable[object]) -> bool:
return not any(iterable)
@property
def matching_function(self) -> typing.Callable[[typing.Iterable[object]], bool]:
return {
constants.ANY_RULE: any,
constants.ALL_RULE: all,
constants.NONE_RULE: SegmentRuleModel.none,
}[self.type]
class SegmentModel(BaseModel):
id: int
name: str
rules: typing.List[SegmentRuleModel] = Field(default_factory=list)
feature_states: typing.List[FeatureStateModel] = Field(default_factory=list)
meta: typing.Optional[typing.Dict[str, str]] = None