Skip to content

Commit

Permalink
Add config spec option to use a compact YAML representations of neste…
Browse files Browse the repository at this point in the history
…d arrays (#6082)
  • Loading branch information
ofek authored Mar 18, 2020
1 parent 40dc7ae commit 5386ee9
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def __exit__(self, exc_type, exc_value, traceback):
self.writer.close()


def construct_yaml(obj):
return yaml.safe_dump(obj, default_flow_style=False, sort_keys=False)
def construct_yaml(obj, **kwargs):
kwargs.setdefault('default_flow_style', False)
return yaml.safe_dump(obj, sort_keys=False, **kwargs)


def value_type_string(value):
Expand Down Expand Up @@ -120,7 +121,20 @@ def write_option(option, writer, indent='', start_list=False):
option_yaml = construct_yaml([{option_name: example}])
indent = indent[:-2]
else:
option_yaml = construct_yaml({option_name: example})
if value.get('compact_example') and example_type is list:
option_yaml_lines = [f'{option_name}:']
for item in example:
# Solitary strings are given an ellipsis after, prevent that
if isinstance(item, str):
compacted_item = construct_yaml(item, default_flow_style=True, default_style='"')
else:
compacted_item = construct_yaml(item, default_flow_style=True)

option_yaml_lines.append(f'- {compacted_item.strip()}')

option_yaml = '\n'.join(option_yaml_lines)
else:
option_yaml = construct_yaml({option_name: example})

example_indent = ' ' if example_type is list and example else ''
for i, line in enumerate(option_yaml.splitlines()):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,102 @@ def test_no_options():
- {}
"""
)


def test_compact_example():
consumer = get_example_consumer(
"""
name: foo
version: 0.0.0
files:
- name: test.yaml
example_name: test.yaml.example
options:
- name: foo
description: words
value:
type: array
compact_example: true
example:
- - 0
- 1
- foo
- foo: bar
bar: baz
- - 2
- 3
items:
type: array
items:
type: integer
"""
)

files = consumer.render()
contents, errors = files['test.yaml.example']
assert not errors
assert contents == normalize_yaml(
"""
## @param foo - list of lists - optional
## words
#
# foo:
# - [0, 1]
# - "foo"
# - {foo: bar, bar: baz}
# - [2, 3]
"""
)


def test_compact_example_nested():
consumer = get_example_consumer(
"""
name: foo
version: 0.0.0
files:
- name: test.yaml
example_name: test.yaml.example
options:
- template: instances
options:
- name: foo
description: words
value:
type: array
compact_example: true
example:
- - 0
- 1
- foo
- foo: bar
bar: baz
- - 2
- 3
items:
type: array
items:
type: integer
"""
)

files = consumer.render()
contents, errors = files['test.yaml.example']
assert not errors
assert contents == normalize_yaml(
"""
## Every instance is scheduled independent of the others.
#
instances:
-
## @param foo - list of lists - optional
## words
#
# foo:
# - [0, 1]
# - "foo"
# - {foo: bar, bar: baz}
# - [2, 3]
"""
)

0 comments on commit 5386ee9

Please sign in to comment.