Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config spec option for compact YAML representations of nested arrays #6082

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
"""
)