Skip to content

Commit 55d2b6b

Browse files
committed
Fix fields sorting with unicode model ref;
1 parent f26d218 commit 55d2b6b

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,17 @@ Arguments:
162162
* **Example**: `-l Car - cars.json -l Person fetch_results.items.persons result.json`
163163
* **Note**: Models names under this arguments should be unique.
164164

165+
* `-o`, `--output` - Output file
166+
* **Format**: `-o <FILE>`
167+
* **Example**: `-o car_model.py`
168+
165169
* `-f`, `--framework` - Model framework for which python code is generated.
166170
`base` (default) mean no framework so code will be generated without any decorators and additional meta-data.
167171
* **Format**: `-f {base,attrs,dataclasses,custom}`
168172
* **Example**: `-f attrs`
169173
* **Default**: `-f base`
170174

171-
* `-s , --structure` - Models composition style.
175+
* `-s`, `--structure` - Models composition style.
172176
* **Format**: `-s {nested, flat}`
173177
* **Example**: `-s flat`
174178
* **Default**: `-s nested`
@@ -177,6 +181,9 @@ Arguments:
177181
* **Default**: disabled
178182
* **Warning**: This can lead to 6-7 times slowdown on large datasets. Be sure that you really need this option.
179183

184+
* `--disable-unicode-conversion`, `--no-unidecode` - Disable unicode conversion in field labels and class names
185+
* **Default**: enabled
186+
180187
* `--strings-converters` - Enable generation of string types converters (i.e. `IsoDatetimeString` or `BooleanString`).
181188
* **Default**: disabled
182189

@@ -196,7 +203,7 @@ Arguments:
196203
* **Format**: `--dkr RegEx [RegEx ...]`
197204
* **Example**: `--dkr node_\d+ \d+_\d+_\d+`
198205
* **Note**: `^` and `$` (string borders) tokens will be added automatically but you
199-
have escape to other special characters manually.
206+
have to escape other special characters manually.
200207
* **Optional**
201208

202209
* `--dict-keys-fields`, `--dkf` - List of model fields names that will be marked as dict fields

json_to_models/dynamic_typing/base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from inspect import isclass
2-
from typing import Iterable, List, Tuple, Union
2+
from typing import Any, Generator, Iterable, List, Tuple, Union
33

44
ImportPathList = List[Tuple[str, Union[Iterable[str], str, None]]]
55

@@ -50,6 +50,14 @@ def _to_hash_string(self) -> str:
5050
"""
5151
raise NotImplementedError()
5252

53+
def iter_child(self) -> Generator['MetaData', Any, None]:
54+
yield self
55+
for child in self:
56+
if isinstance(child, BaseType):
57+
yield from child.iter_child()
58+
else:
59+
yield child
60+
5361

5462
class UnknownType(BaseType):
5563
__slots__ = []

json_to_models/models/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def fields(self) -> Tuple[ImportPathList, List[str]]:
143143
144144
:return: imports, list of fields as string
145145
"""
146-
required, optional = sort_fields(self.model)
146+
required, optional = sort_fields(self.model, unicode_fix=not self.convert_unicode)
147147
imports: ImportPathList = []
148148
strings: List[str] = []
149149
for is_optional, fields in enumerate((required, optional)):

json_to_models/models/structure.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from . import Index, ModelsStructureType
44
from .utils import ListEx, PositionsDict
5-
from ..dynamic_typing import DOptional, ModelMeta, ModelPtr
5+
from ..dynamic_typing import BaseType, DOptional, ModelMeta, ModelPtr
66

77

88
def compose_models(models_map: Dict[str, ModelMeta]) -> ModelsStructureType:
@@ -137,18 +137,24 @@ def extract_root(model: ModelMeta) -> Set[Index]:
137137
return roots
138138

139139

140-
def sort_fields(model_meta: ModelMeta) -> Tuple[List[str], List[str]]:
140+
def sort_fields(model_meta: ModelMeta, unicode_fix=False) -> Tuple[List[str], List[str]]:
141141
"""
142142
Split fields into required and optional groups
143143
144144
:return: two list of fields names: required fields, optional fields
145145
"""
146146
fields = model_meta.type
147147
required = []
148+
required_2 = []
148149
optional = []
149150
for key, meta in fields.items():
150151
if isinstance(meta, DOptional):
151152
optional.append(key)
153+
elif unicode_fix and isinstance(meta, BaseType) and any(
154+
isinstance(node, ModelMeta)
155+
for node in meta.iter_child()
156+
):
157+
required_2.append(key)
152158
else:
153159
required.append(key)
154-
return required, optional
160+
return required + required_2, optional

0 commit comments

Comments
 (0)