|
1 | | -from pydantic import BaseModel, ConfigDict, Field, field_validator |
| 1 | +from pydantic import ConfigDict, Field, model_validator |
2 | 2 | from typing import Annotated, List, Literal, Union |
3 | 3 |
|
4 | 4 | from schema.BaseElement import BaseElement |
@@ -29,47 +29,70 @@ class Line(BaseElement): |
29 | 29 | ] |
30 | 30 | ] |
31 | 31 |
|
32 | | - @field_validator("line", mode="before") |
| 32 | + # @field_validator("line", mode="before") |
| 33 | + # @classmethod |
| 34 | + # def parse_list_of_dicts(cls, value): |
| 35 | + # """This method inserts the key of the one-key dictionary into |
| 36 | + # the name attribute of the elements""" |
| 37 | + # if not isinstance(value, list): |
| 38 | + # raise TypeError("line must be a list") |
| 39 | + |
| 40 | + # if value and isinstance(value[0], BaseModel): |
| 41 | + # # Already a list of models; nothing to do |
| 42 | + # return value |
| 43 | + |
| 44 | + # # we expect a list of dicts or strings |
| 45 | + # elements = [] |
| 46 | + # for item_dict in value: |
| 47 | + # # an element is either a reference string to another element or a dict |
| 48 | + # if isinstance(item_dict, str): |
| 49 | + # raise RuntimeError("Reference/alias elements not yet implemented") |
| 50 | + |
| 51 | + # elif isinstance(item_dict, dict): |
| 52 | + # if not (isinstance(item_dict, dict) and len(item_dict) == 1): |
| 53 | + # raise ValueError( |
| 54 | + # f"Each line element must be a dict with exactly one key, the name of the element, but we got: {item_dict!r}" |
| 55 | + # ) |
| 56 | + # [(name, fields)] = item_dict.items() |
| 57 | + |
| 58 | + # if not isinstance(fields, dict): |
| 59 | + # raise ValueError( |
| 60 | + # f"Value for element key '{name}' must be a dict (got {fields!r})" |
| 61 | + # ) |
| 62 | + |
| 63 | + # # Insert the name into the fields dict |
| 64 | + # fields["name"] = name |
| 65 | + # elements.append(fields) |
| 66 | + # return elements |
| 67 | + |
| 68 | + @model_validator(mode="before") |
33 | 69 | @classmethod |
34 | | - def parse_list_of_dicts(cls, value): |
35 | | - """This method inserts the key of the one-key dictionary into |
36 | | - the name attribute of the elements""" |
37 | | - if not isinstance(value, list): |
38 | | - raise TypeError("line must be a list") |
39 | | - |
40 | | - if value and isinstance(value[0], BaseModel): |
41 | | - # Already a list of models; nothing to do |
42 | | - return value |
43 | | - |
44 | | - # we expect a list of dicts or strings |
45 | | - elements = [] |
46 | | - for item_dict in value: |
47 | | - # an element is either a reference string to another element or a dict |
48 | | - if isinstance(item_dict, str): |
49 | | - raise RuntimeError("Reference/alias elements not yet implemented") |
50 | | - |
51 | | - elif isinstance(item_dict, dict): |
52 | | - if not (isinstance(item_dict, dict) and len(item_dict) == 1): |
53 | | - raise ValueError( |
54 | | - f"Each line element must be a dict with exactly one key, the name of the element, but we got: {item_dict!r}" |
55 | | - ) |
56 | | - [(name, fields)] = item_dict.items() |
57 | | - |
58 | | - if not isinstance(fields, dict): |
59 | | - raise ValueError( |
60 | | - f"Value for element key '{name}' must be a dict (got {fields!r})" |
61 | | - ) |
62 | | - |
63 | | - # Insert the name into the fields dict |
64 | | - fields["name"] = name |
65 | | - elements.append(fields) |
66 | | - return elements |
| 70 | + def unpack_yaml_structure(cls, data): |
| 71 | + # Handle the top-level dict |
| 72 | + if isinstance(data, dict) and len(data) == 1: |
| 73 | + name, value = list(data.items())[0] |
| 74 | + value = dict(value) |
| 75 | + value["name"] = name |
| 76 | + data = value |
| 77 | + # Now handle the 'line' field if present: unpack each element's name |
| 78 | + if "line" in data and isinstance(data["line"], list): |
| 79 | + new_line = [] |
| 80 | + for item in data["line"]: |
| 81 | + if isinstance(item, dict) and len(item) == 1: |
| 82 | + elem_name, elem_fields = list(item.items())[0] |
| 83 | + elem_fields = dict(elem_fields) |
| 84 | + elem_fields["name"] = elem_name |
| 85 | + new_line.append(elem_fields) |
| 86 | + else: |
| 87 | + new_line.append(item) |
| 88 | + data["line"] = new_line |
| 89 | + return data |
67 | 90 |
|
68 | 91 | def model_dump(self, *args, **kwargs): |
69 | 92 | """This makes sure the element name property is moved out and up to a one-key dictionary""" |
70 | 93 | # Use base element dump first and return a one-element list of the form |
71 | 94 | # [{key: value}], where 'key' is the name of the line and 'value' is a |
72 | | - # dictionary with all other properties |
| 95 | + # dict with all other properties |
73 | 96 | data = super().model_dump(*args, **kwargs) |
74 | 97 | # Reformat 'line' field as list of single-key dicts |
75 | 98 | new_line = [] |
|
0 commit comments