|
7 | 7 | """Contains Instance class.""" |
8 | 8 |
|
9 | 9 | # standard libraries |
10 | | -import uuid |
| 10 | +import copy |
11 | 11 | from numbers import Number |
12 | 12 | from typing import Dict, Union |
13 | 13 |
|
|
16 | 16 |
|
17 | 17 | # local sources |
18 | 18 | ## PFDL base sources |
| 19 | +from pfdl_scheduler.model.array import Array |
| 20 | +from pfdl_scheduler.pfdl_base_classes import PFDLBaseClasses |
19 | 21 | from pfdl_scheduler.validation.error_handler import ErrorHandler |
20 | 22 |
|
21 | 23 |
|
@@ -56,39 +58,61 @@ def __init__( |
56 | 58 | self.context: ParserRuleContext = context |
57 | 59 | self.attribute_contexts: Dict = {} |
58 | 60 |
|
| 61 | + def __deepcopy__(self, memo): |
| 62 | + cls = self.__class__ |
| 63 | + result = cls.__new__(cls) |
| 64 | + memo[id(self)] = result |
| 65 | + for attr, value in self.__dict__.items(): |
| 66 | + try: |
| 67 | + setattr(result, attr, copy.deepcopy(value, memo)) |
| 68 | + except Exception: |
| 69 | + setattr(result, attr, value) |
| 70 | + return result |
| 71 | + |
59 | 72 | @classmethod |
60 | 73 | def from_json( |
61 | 74 | cls, |
62 | 75 | json_object: Dict, |
63 | 76 | error_handler: ErrorHandler, |
64 | 77 | struct_context: ParserRuleContext, |
65 | | - instance_class="Instance", |
| 78 | + pfdl_base_classes=PFDLBaseClasses, |
66 | 79 | ): |
67 | | - return parse_json(json_object, error_handler, struct_context, instance_class) |
| 80 | + return parse_json(json_object, error_handler, struct_context, pfdl_base_classes) |
68 | 81 |
|
69 | 82 |
|
70 | 83 | def parse_json( |
71 | 84 | json_object: Dict, |
72 | 85 | error_handler: ErrorHandler, |
73 | 86 | instance_context: ParserRuleContext, |
74 | | - instance_class=Instance, |
| 87 | + pfdl_base_classes=PFDLBaseClasses, |
75 | 88 | ) -> Instance: |
76 | 89 | """Parses the JSON Struct initialization. |
77 | 90 |
|
78 | 91 | Returns: |
79 | 92 | An Instance object representing the initialized instance. |
80 | 93 | """ |
81 | | - instance = instance_class() |
| 94 | + instance = pfdl_base_classes.get_class("Instance")() |
82 | 95 | instance.context = instance_context |
83 | 96 | for identifier, value in json_object.items(): |
84 | 97 | if isinstance(value, (int, str, bool)): |
85 | 98 | instance.attributes[identifier] = value |
86 | 99 | elif isinstance(value, list): |
87 | | - if error_handler and instance_context: |
88 | | - error_msg = "Array definition in JSON are not supported in the PFDL." |
89 | | - error_handler.print_error(error_msg, context=instance_context) |
| 100 | + array = pfdl_base_classes.get_class("Array")() |
| 101 | + instance.attributes[identifier] = array |
| 102 | + for element in value: |
| 103 | + if isinstance(element, (int, float, str, bool)): |
| 104 | + if isinstance(element, bool): |
| 105 | + array.type_of_elements = "boolean" |
| 106 | + elif isinstance(element, (int, float)): |
| 107 | + array.type_of_elements = "number" |
| 108 | + else: |
| 109 | + array.type_of_elements = "string" |
| 110 | + array.append_value(element) |
| 111 | + elif isinstance(element, dict): |
| 112 | + inner_struct = parse_json(element, error_handler) |
| 113 | + array.append_value(inner_struct) |
90 | 114 | elif isinstance(value, dict): |
91 | | - inner_struct = parse_json(value, error_handler, instance_context, instance_class) |
| 115 | + inner_struct = parse_json(value, error_handler, instance_context, pfdl_base_classes) |
92 | 116 | instance.attributes[identifier] = inner_struct |
93 | 117 |
|
94 | 118 | return instance |
0 commit comments