This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
json_parse_node.py
316 lines (274 loc) · 11.8 KB
/
json_parse_node.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from __future__ import annotations
import json
import warnings
from datetime import date, datetime, time, timedelta
from enum import Enum
from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union
from uuid import UUID
import pendulum
from kiota_abstractions.serialization import Parsable, ParsableFactory, ParseNode
T = TypeVar("T")
U = TypeVar("U", bound=Parsable)
K = TypeVar("K", bound=Enum)
class JsonParseNode(ParseNode, Generic[T, U]):
def __init__(self, node: Any) -> None:
"""
Args:
node (Any): The JsonElement\
to initialize the node with
"""
self._json_node = node
self._on_before_assign_field_values: Optional[Callable[[Parsable], None]] = None
self._on_after_assign_field_values: Optional[Callable[[Parsable], None]] = None
def get_child_node(self, identifier: str) -> Optional[ParseNode]:
"""Gets a new parse node for the given identifier
Args:
identifier (str): The identifier of the current node property
Returns:
Optional[ParseNode]: A new parse node for the given identifier
"""
if not identifier:
raise ValueError("identifier cannot be None or empty.")
if isinstance(node := self._json_node, dict) and identifier in node:
return self._create_new_node(node[identifier])
return None
def get_str_value(self) -> Optional[str]:
"""Gets the string value from the json node
Returns:
str: The string value of the node
"""
return self._json_node if isinstance(self._json_node, str) else None
def get_bool_value(self) -> Optional[bool]:
"""Gets the boolean value of the json node
Returns:
bool: The boolean value of the node
"""
return self._json_node if isinstance(self._json_node, bool) else None
def get_int_value(self) -> Optional[int]:
"""Gets the integer value of the json node
Returns:
int: The integer value of the node
"""
return self._json_node if isinstance(self._json_node, int) else None
def get_float_value(self) -> Optional[float]:
"""Gets the float value of the json node
Returns:
float: The integer value of the node
"""
return self._json_node if isinstance(self._json_node, float) else None
def get_uuid_value(self) -> Optional[UUID]:
"""Gets the UUID value of the json node
Returns:
UUID: The GUID value of the node
"""
if isinstance(self._json_node, UUID):
return self._json_node
if isinstance(self._json_node, str):
return UUID(self._json_node)
return None
def get_datetime_value(self) -> Optional[datetime]:
"""Gets the datetime value of the json node
Returns:
datetime: The datetime value of the node
"""
if isinstance(self._json_node, datetime):
return self._json_node
if isinstance(self._json_node, str):
datetime_obj = pendulum.parse(self._json_node, exact=True)
if isinstance(datetime_obj, pendulum.DateTime):
return datetime_obj
return None
def get_timedelta_value(self) -> Optional[timedelta]:
"""Gets the timedelta value of the node
Returns:
timedelta: The timedelta value of the node
"""
if isinstance(self._json_node, timedelta):
return self._json_node
if isinstance(self._json_node, str):
datetime_obj = pendulum.parse(self._json_node, exact=True)
if isinstance(datetime_obj, pendulum.Duration):
return datetime_obj.as_timedelta()
return None
def get_date_value(self) -> Optional[date]:
"""Gets the date value of the node
Returns:
date: The datevalue of the node in terms on year, month, and day.
"""
if isinstance(self._json_node, date):
return self._json_node
if isinstance(self._json_node, str):
datetime_obj = pendulum.parse(self._json_node, exact=True)
if isinstance(datetime_obj, pendulum.Date):
return datetime_obj
return None
def get_time_value(self) -> Optional[time]:
"""Gets the time value of the node
Returns:
time: The time value of the node in terms of hour, minute, and second.
"""
if isinstance(self._json_node, time):
return self._json_node
if isinstance(self._json_node, str):
datetime_obj = pendulum.parse(self._json_node, exact=True)
if isinstance(datetime_obj, pendulum.Time):
return datetime_obj
return None
def get_collection_of_primitive_values(self, primitive_type: Any) -> Optional[List[T]]:
"""Gets the collection of primitive values of the node
Returns:
List[T]: The collection of primitive values
"""
primitive_types = {bool, str, int, float, UUID, datetime, timedelta, date, time, bytes}
def func(item):
generic_type = primitive_type if primitive_type else type(item)
current_parse_node = self._create_new_node(item)
if generic_type in primitive_types:
method = getattr(current_parse_node, f'get_{generic_type.__name__.lower()}_value')
return method()
raise Exception(f"Encountered an unknown type during deserialization {generic_type}")
if isinstance(self._json_node, str):
return list(map(func, json.loads(self._json_node)))
return list(map(func, list(self._json_node)))
def get_collection_of_object_values(self, factory: ParsableFactory) -> List[U]:
"""Gets the collection of type U values from the json node
Returns:
List[U]: The collection of model object values of the node
"""
if isinstance(self._json_node, list):
return list(
map(
lambda x: self._create_new_node(x).get_object_value(factory), # type: ignore
self._json_node,
)
)
return []
def get_collection_of_enum_values(self, enum_class: K) -> List[Optional[K]]:
"""Gets the collection of enum values of the json node
Returns:
List[K]: The collection of enum values
"""
if isinstance(self._json_node, list):
return list(
map(lambda x: self._create_new_node(x).get_enum_value(enum_class), self._json_node)
)
return []
def get_enum_value(self, enum_class: K) -> Optional[K]:
"""Gets the enum value of the node
Returns:
Optional[K]: The enum value of the node
"""
raw_key = str(self._json_node)
if not raw_key:
return None
camel_case_key = None
if raw_key.lower() == "none":
# None is a reserved keyword in python
camel_case_key = "None_"
else:
camel_case_key = raw_key[0].upper() + raw_key[1:]
try:
return enum_class[camel_case_key] # type: ignore
except KeyError:
raise Exception(f'Invalid key: {camel_case_key} for enum {enum_class}.')
def get_object_value(self, factory: ParsableFactory) -> U:
"""Gets the model object value of the node
Returns:
Parsable: The model object value of the node
"""
result = factory.create_from_discriminator_value(self)
if on_before := self.on_before_assign_field_values:
on_before(result)
self._assign_field_values(result)
if on_after := self.on_after_assign_field_values:
on_after(result)
return result
def get_bytes_value(self) -> Optional[bytes]:
"""Get a bytearray value from the nodes
Returns:
bytearray: The bytearray value from the nodes
"""
base64_string = str(self._json_node)
if not base64_string:
return None
return base64_string.encode("utf-8")
@property
def on_before_assign_field_values(self) -> Optional[Callable[[Parsable], None]]:
"""Gets the callback called before the node is deserialized.
Returns:
Callable[[Parsable], None]: the callback called before the node is deserialized.
"""
return self._on_before_assign_field_values
@on_before_assign_field_values.setter
def on_before_assign_field_values(self, value: Callable[[Parsable], None]) -> None:
"""Sets the callback called before the node is deserialized.
Args:
value (Callable[[Parsable], None]): the callback called before the node is
deserialized.
"""
self._on_before_assign_field_values = value
@property
def on_after_assign_field_values(self) -> Optional[Callable[[Parsable], None]]:
"""Gets the callback called before the node is deserialized.
Returns:
Callable[[Parsable], None]: the callback called before the node is deserialized.
"""
return self._on_after_assign_field_values
@on_after_assign_field_values.setter
def on_after_assign_field_values(self, value: Callable[[Parsable], None]) -> None:
"""Sets the callback called after the node is deserialized.
Args:
value (Callable[[Parsable], None]): the callback called after the node is
deserialized.
"""
self._on_after_assign_field_values = value
def _assign_field_values(self, item: U) -> None:
"""Assigns the field values to the model object"""
# if object is null
if not isinstance(self._json_node, dict):
return
item_additional_data = None
if not hasattr(item, "additional_data") or item.additional_data is None:
item_additional_data = {}
else:
item_additional_data = item.additional_data
field_deserializers = item.get_field_deserializers()
for field_name, field_value in self._json_node.items():
if field_name in field_deserializers:
if field_value is None:
continue
field_deserializer = field_deserializers[field_name]
field_deserializer(JsonParseNode(field_value))
elif item_additional_data is not None:
item_additional_data[field_name] = self.try_get_anything(field_value)
else:
warnings.warn(
f"Found additional property {field_name} to \
deserialize but the model doesn't support additional data"
)
def try_get_anything(self, value: Any) -> Any:
if isinstance(value, (int, float, bool)) or value is None:
return value
if isinstance(value, list):
return list(map(self.try_get_anything, value))
if isinstance(value, dict):
return dict(map(lambda x: (x[0], self.try_get_anything(x[1])), value.items()))
if isinstance(value, str):
try:
datetime_obj = pendulum.parse(value)
if isinstance(datetime_obj, pendulum.Duration):
return datetime_obj.as_timedelta()
return datetime_obj
except:
pass
try:
return UUID(value)
except:
pass
return value
raise ValueError(f"Unexpected additional value type {type(value)} during deserialization.")
def _create_new_node(self, node: Any) -> JsonParseNode:
new_node: JsonParseNode = JsonParseNode(node)
new_node.on_before_assign_field_values = self.on_before_assign_field_values
new_node.on_after_assign_field_values = self.on_after_assign_field_values
return new_node