forked from DevCycleHQ/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
129 lines (106 loc) · 4.57 KB
/
Copy pathutils.py
File metadata and controls
129 lines (106 loc) · 4.57 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import json
import logging
import math
from typing import Any, Optional
from devcycle_python_sdk.models.variable import TypeEnum, Variable
from devcycle_python_sdk.models.user import DevCycleUser
import devcycle_python_sdk.protobuf.variableForUserParams_pb2 as pb2
logger = logging.getLogger(__name__)
def create_nullable_double(val: Optional[float]) -> pb2.NullableDouble: # type: ignore
if val and not math.isnan(val):
return pb2.NullableDouble(value=val, isNull=False) # type: ignore
else:
return pb2.NullableDouble(isNull=True) # type: ignore
def create_nullable_string(val: Optional[str]) -> pb2.NullableString: # type: ignore
if val is None:
return pb2.NullableString(isNull=True) # type: ignore
else:
return pb2.NullableString(value=val, isNull=False) # type: ignore
def create_nullable_custom_data(val: Optional[dict]) -> pb2.NullableCustomData: # type: ignore
if val:
values = dict()
for key, value in val.items():
if value is None:
values[key] = pb2.CustomDataValue(type=pb2.CustomDataType.Null) # type: ignore
elif isinstance(value, bool):
values[key] = pb2.CustomDataValue(type=pb2.CustomDataType.Bool, boolValue=value) # type: ignore
elif isinstance(value, str):
values[key] = pb2.CustomDataValue(type=pb2.CustomDataType.Str, stringValue=value) # type: ignore
elif isinstance(value, (int, float)):
values[key] = pb2.CustomDataValue(type=pb2.CustomDataType.Num, doubleValue=value) # type: ignore
else:
logger.warning(
f"Custom Data contains data type that can't be written, will be ignored. Key: {key}, Type: {str(type(value))}"
)
return pb2.NullableCustomData(value=values, isNull=False) # type: ignore
else:
return pb2.NullableCustomData(isNull=True) # type: ignore
def convert_type_enum_to_variable_type(var_type: str) -> pb2.VariableType_PB:
if var_type == TypeEnum.BOOLEAN:
return pb2.VariableType_PB.Boolean
elif var_type == TypeEnum.STRING:
return pb2.VariableType_PB.String
elif var_type == TypeEnum.NUMBER:
return pb2.VariableType_PB.Number
elif var_type == TypeEnum.JSON:
return pb2.VariableType_PB.JSON
else:
raise ValueError("Unknown type: " + str(var_type))
def create_dvcuser_pb(user: DevCycleUser) -> pb2.DVCUser_PB: # type: ignore
app_build = float("nan")
if user.appBuild:
try:
app_build = float(user.appBuild)
except ValueError:
pass
return pb2.DVCUser_PB( # type: ignore
user_id=user.user_id,
email=create_nullable_string(user.email),
name=create_nullable_string(user.name),
language=create_nullable_string(user.language),
country=create_nullable_string(user.country),
appVersion=create_nullable_string(user.appVersion),
appBuild=create_nullable_double(app_build),
customData=create_nullable_custom_data(user.customData),
privateCustomData=create_nullable_custom_data(user.privateCustomData),
)
def create_variable(sdk_variable: pb2.SDKVariable_PB, default_value: Any) -> Variable: # type: ignore
if sdk_variable.type == pb2.VariableType_PB.Boolean: # type: ignore
return Variable(
_id=None,
value=sdk_variable.boolValue,
key=sdk_variable.key,
type=TypeEnum.BOOLEAN,
isDefaulted=False,
defaultValue=default_value,
)
elif sdk_variable.type == pb2.VariableType_PB.String: # type: ignore
return Variable(
_id=None,
value=sdk_variable.stringValue,
key=sdk_variable.key,
type=TypeEnum.STRING,
isDefaulted=False,
defaultValue=default_value,
)
elif sdk_variable.type == pb2.VariableType_PB.Number: # type: ignore
return Variable(
_id=None,
value=sdk_variable.doubleValue,
key=sdk_variable.key,
type=TypeEnum.NUMBER,
isDefaulted=False,
defaultValue=default_value,
)
elif sdk_variable.type == pb2.VariableType_PB.JSON: # type: ignore
json_data = json.loads(sdk_variable.stringValue)
return Variable(
_id=None,
value=json_data,
key=sdk_variable.key,
type=TypeEnum.JSON,
isDefaulted=False,
defaultValue=default_value,
)
else:
raise ValueError("Unknown type: " + sdk_variable.type)