Skip to content

Commit

Permalink
yaml renderer: handle nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
glorpen committed May 3, 2022
1 parent 625f9bb commit 3e92ba9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/glorpen/config/translators/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def list_indent(items: typing.Iterable[str], prefix):

class YamlRenderer:

_indent_size = 2

def __init__(self):
super(YamlRenderer, self).__init__()

Expand All @@ -32,9 +34,15 @@ def _render_dict(self, fields: typing.Dict[str, Field]):
value = list(self._render(field))
key = f"{name}: "
prefix = "# " if field.default_factory else ""
yield prefix + key + value[0]
for line in list_indent(value[1:], " " * len(key)):
yield prefix + line

if isinstance(field.args, dict):
yield prefix + key.rstrip()
for line in list_indent(value, " " * self._indent_size):
yield prefix + line
else:
yield prefix + key + value[0]
for line in list_indent(value[1:], " " * len(key)):
yield prefix + line

def _render_value(self, model: Field):
if model.is_nullable():
Expand Down
12 changes: 12 additions & 0 deletions tests/translators/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ class Dummy:
line3"""

assert render(Dummy) == "# field: |-\n# line1\n# line2\n# line3\n"


def test_nested_fields():
@dataclasses.dataclass
class Dummy2:
field: str

@dataclasses.dataclass
class Dummy1:
field: Dummy2

assert render(Dummy1) == "field:\n field: # required str\n"

0 comments on commit 3e92ba9

Please sign in to comment.