Skip to content

Commit 6719c96

Browse files
authored
[ENUM] Default handler to use enum.name (#1252)
1 parent 6fa955b commit 6719c96

File tree

4 files changed

+96
-13
lines changed

4 files changed

+96
-13
lines changed

jac-cloud/jaclang_jaseci/core/architype.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Core constructs for Jac Language."""
22

3-
from dataclasses import asdict, dataclass, field, fields, is_dataclass
3+
from dataclasses import asdict as _asdict, dataclass, field, fields, is_dataclass
4+
from enum import Enum
45
from os import getenv
56
from pickle import dumps as pdumps
67
from re import IGNORECASE, compile
78
from typing import (
89
Any,
910
Callable,
1011
ClassVar,
12+
Iterable,
1113
Mapping,
1214
TypeVar,
1315
cast,
@@ -53,6 +55,24 @@
5355
TBA = TypeVar("TBA", bound="BaseArchitype")
5456

5557

58+
def asdict_factory(data: Iterable[tuple]) -> dict[str, Any]:
59+
"""Parse dataclass to dict."""
60+
_data = {}
61+
for key, value in data:
62+
if isinstance(value, Enum):
63+
_data[key] = value.name
64+
else:
65+
_data[key] = value
66+
return _data
67+
68+
69+
def asdict(obj: object) -> dict[str, Any]:
70+
"""Override dataclass asdict."""
71+
if is_dataclass(obj) and not isinstance(obj, type):
72+
return _asdict(obj, dict_factory=asdict_factory)
73+
raise ValueError("Object is not a dataclass!")
74+
75+
5676
def architype_to_dataclass(cls: type[T], data: dict[str, Any], **kwargs: object) -> T:
5777
"""Parse dict to architype."""
5878
_to_dataclass(cls, data)
@@ -89,6 +109,12 @@ def _to_dataclass(cls: type[T], data: dict[str, Any]) -> None:
89109
):
90110
for key, value in enumerate(target):
91111
target[key] = to_dataclass(inner_cls, value)
112+
elif (
113+
issubclass(hint, Enum)
114+
and isinstance(target, str)
115+
and (enum := hint.__members__.get(target))
116+
):
117+
data[attr.name] = enum
92118

93119

94120
@dataclass

jac-cloud/jaclang_jaseci/core/context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Core constructs for Jac Language."""
22

33
from contextvars import ContextVar
4-
from dataclasses import asdict, is_dataclass
4+
from dataclasses import is_dataclass
55
from os import getenv
66
from typing import Any, cast
77

@@ -19,6 +19,7 @@
1919
NodeAnchor,
2020
Permission,
2121
Root,
22+
asdict,
2223
)
2324
from .memory import MongoDB
2425

jac-cloud/jaclang_jaseci/tests/simple_graph.jac

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class User:BaseUser: {
3838
}
3939
}
4040

41+
enum Enum {
42+
A = "a",
43+
B = "b",
44+
C = "c"
45+
}
46+
4147
node A {
4248
has val: int;
4349
}
@@ -51,15 +57,15 @@ node C {
5157
}
5258

5359
obj Child {
54-
has val: int, arr: list, json: dict;
60+
has val: int, arr: list, json: dict, enum_field: Enum;
5561
}
5662

5763
obj Parent:Child: {
5864
has child: Child;
5965
}
6066

6167
node Nested {
62-
has val: int, arr: list, json: dict, parent: Parent;
68+
has val: int, arr: list, json: dict, parent: Parent, enum_field: Enum;
6369
}
6470

6571
walker create_graph {
@@ -145,9 +151,13 @@ walker create_nested_node {
145151
child=Child(
146152
val=2,
147153
arr=[1, 2],
148-
json={"a": 1, "b": 2}
149-
)
150-
));
154+
json={"a": 1, "b": 2},
155+
enum_field = Enum.C
156+
),
157+
enum_field = Enum.B
158+
),
159+
enum_field = Enum.A
160+
);
151161
here ++> n;
152162
return n;
153163
}
@@ -159,25 +169,31 @@ walker update_nested_node {
159169
nested.parent.child.json["c"] = 3;
160170
nested.parent.child.arr.append(3);
161171
nested.parent.child.val = 3;
172+
nested.parent.child.enum_field = Enum.A;
162173
nested.parent.json["b"] = 2;
163174
nested.parent.arr.append(2);
164175
nested.parent.val = 2;
176+
nested.parent.enum_field = Enum.C;
165177
nested.json["a"] = 1;
166178
nested.arr.append(1);
167179
nested.val = 1;
180+
nested.enum_field = Enum.B;
168181
return nested;
169182
}
170183

171184
can enter_nested with Nested entry {
172185
here.parent.child.json["c"] = 3;
173186
here.parent.child.arr.append(3);
174187
here.parent.child.val = 3;
188+
here.parent.child.enum_field = Enum.A;
175189
here.parent.json["b"] = 2;
176190
here.parent.arr.append(2);
177191
here.parent.val = 2;
192+
here.parent.enum_field = Enum.C;
178193
here.json["a"] = 1;
179194
here.arr.append(1);
180195
here.val = 1;
196+
here.enum_field = Enum.B;
181197
return here;
182198
}
183199
}
@@ -216,9 +232,13 @@ walker manual_create_nested_node {
216232
child=Child(
217233
val=2,
218234
arr=[1, 2],
219-
json={"a": 1, "b": 2}
220-
)
221-
));
235+
json={"a": 1, "b": 2},
236+
enum_field = Enum.C
237+
),
238+
enum_field = Enum.B
239+
),
240+
enum_field = Enum.A
241+
);
222242
here ++> n;
223243
here.__jac__.apply();
224244

@@ -237,12 +257,15 @@ walker manual_update_nested_node {
237257
nested.parent.child.json["c"] = 3;
238258
nested.parent.child.arr.append(3);
239259
nested.parent.child.val = 3;
260+
nested.parent.child.enum_field = Enum.A;
240261
nested.parent.json["b"] = 2;
241262
nested.parent.arr.append(2);
242263
nested.parent.val = 2;
264+
nested.parent.enum_field = Enum.C;
243265
nested.json["a"] = 1;
244266
nested.arr.append(1);
245267
nested.val = 1;
268+
nested.enum_field = Enum.B;
246269
nested.__jac__.apply();
247270

248271
# simulate no auto save

jac-cloud/jaclang_jaseci/tests/test_simple_graph.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,15 @@ def trigger_create_nested_node_test(self, manual: bool = False) -> None:
223223
"val": 1,
224224
"arr": [1],
225225
"json": {"a": 1},
226-
"child": {"val": 2, "arr": [1, 2], "json": {"a": 1, "b": 2}},
226+
"child": {
227+
"val": 2,
228+
"arr": [1, 2],
229+
"json": {"a": 1, "b": 2},
230+
"enum_field": "C",
231+
},
232+
"enum_field": "B",
227233
},
234+
"enum_field": "A",
228235
},
229236
res["returns"][0]["context"],
230237
)
@@ -252,8 +259,11 @@ def trigger_update_nested_node_test(
252259
"val": 3,
253260
"arr": [1, 2, 3],
254261
"json": {"a": 1, "b": 2, "c": 3},
262+
"enum_field": "A",
255263
},
264+
"enum_field": "C",
256265
},
266+
"enum_field": "B",
257267
},
258268
res["returns"][0]["context"],
259269
)
@@ -310,8 +320,15 @@ def trigger_access_validation_test(
310320
"val": 1,
311321
"arr": [1],
312322
"json": {"a": 1},
313-
"child": {"val": 2, "arr": [1, 2], "json": {"a": 1, "b": 2}},
323+
"child": {
324+
"val": 2,
325+
"arr": [1, 2],
326+
"json": {"a": 1, "b": 2},
327+
"enum_field": "C",
328+
},
329+
"enum_field": "B",
314330
},
331+
"enum_field": "A",
315332
},
316333
nested_node["context"],
317334
)
@@ -354,8 +371,11 @@ def trigger_access_validation_test(
354371
"val": 3,
355372
"arr": [1, 2, 3],
356373
"json": {"a": 1, "b": 2, "c": 3},
374+
"enum_field": "A",
357375
},
376+
"enum_field": "C",
358377
},
378+
"enum_field": "B",
359379
},
360380
res["returns"][0]["context"],
361381
)
@@ -373,8 +393,15 @@ def trigger_access_validation_test(
373393
"val": 1,
374394
"arr": [1],
375395
"json": {"a": 1},
376-
"child": {"val": 2, "arr": [1, 2], "json": {"a": 1, "b": 2}},
396+
"child": {
397+
"val": 2,
398+
"arr": [1, 2],
399+
"json": {"a": 1, "b": 2},
400+
"enum_field": "C",
401+
},
402+
"enum_field": "B",
377403
},
404+
"enum_field": "A",
378405
},
379406
res["returns"][0]["context"],
380407
)
@@ -409,8 +436,11 @@ def trigger_access_validation_test(
409436
"val": 3,
410437
"arr": [1, 2, 3],
411438
"json": {"a": 1, "b": 2, "c": 3},
439+
"enum_field": "A",
412440
},
441+
"enum_field": "C",
413442
},
443+
"enum_field": "B",
414444
},
415445
res["returns"][0]["context"],
416446
)
@@ -432,8 +462,11 @@ def trigger_access_validation_test(
432462
"val": 3,
433463
"arr": [1, 2, 3],
434464
"json": {"a": 1, "b": 2, "c": 3},
465+
"enum_field": "A",
435466
},
467+
"enum_field": "C",
436468
},
469+
"enum_field": "B",
437470
},
438471
res["returns"][0]["context"],
439472
)

0 commit comments

Comments
 (0)