Skip to content

Commit cef2c92

Browse files
committed
CLI: flat structure generation
1 parent bfd865b commit cef2c92

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

json_to_models/cli.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import json_to_models
1313
from json_to_models.dynamic_typing import ModelMeta, register_datetime_classes
1414
from json_to_models.generator import MetadataGenerator
15-
from json_to_models.models import ModelsStructureType, compose_models
15+
from json_to_models.models import ModelsStructureType, compose_models, compose_models_flat
1616
from json_to_models.models.attr import AttrsModelCodeGenerator
1717
from json_to_models.models.base import GenericModelCodeGenerator, generate_code
1818
from json_to_models.models.dataclasses import DataclassModelCodeGenerator
@@ -34,8 +34,7 @@ class Cli:
3434

3535
STRUCTURE_FN_MAPPING: Dict[str, STRUCTURE_FN_TYPE] = {
3636
"nested": compose_models,
37-
# TODO: vvvvvvvvvvvv
38-
"flat": lambda *args, **kwargs: None
37+
"flat": compose_models_flat
3938
}
4039

4140
MODEL_GENERATOR_MAPPING: Dict[str, Type[GenericModelCodeGenerator]] = {
@@ -187,8 +186,8 @@ def set_args(self, merge_policy: List[Union[List[str], str]],
187186

188187
self.initialize = True
189188

190-
@staticmethod
191-
def _create_argparser() -> argparse.ArgumentParser:
189+
@classmethod
190+
def _create_argparser(cls) -> argparse.ArgumentParser:
192191
"""
193192
ArgParser factory
194193
"""
@@ -255,13 +254,13 @@ def _create_argparser() -> argparse.ArgumentParser:
255254
parser.add_argument(
256255
"-s", "--structure",
257256
default="nested",
258-
choices=["nested", "flat"],
257+
choices=list(cls.STRUCTURE_FN_MAPPING.keys()),
259258
help="Models composition style. By default nested models become nested Python classes.\n\n"
260259
)
261260
parser.add_argument(
262261
"-f", "--framework",
263262
default="base",
264-
choices=["base", "attrs", "dataclasses", "custom"],
263+
choices=list(cls.MODEL_GENERATOR_MAPPING.keys()) + ["custom"],
265264
help="Model framework for which python code is generated.\n"
266265
"'base' (default) mean no framework so code will be generated without any decorators\n"
267266
"and additional meta-data.\n"

test/test_cli/test_script.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ def test_script(command):
9292
print(stdout)
9393

9494

95+
@pytest.mark.parametrize("command", test_commands)
96+
def test_script_flat(command):
97+
command += " -s flat"
98+
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
99+
stdout, stderr = _validate_result(proc)
100+
print(stdout)
101+
102+
95103
@pytest.mark.parametrize("command", test_commands)
96104
def test_script_attrs(command):
97105
command += " -f attrs"

0 commit comments

Comments
 (0)