Skip to content

Commit

Permalink
feat: better default str dumping for multiline strings in dbt yamls
Browse files Browse the repository at this point in the history
  • Loading branch information
z3z1ma committed Jan 4, 2025
1 parent 80a3663 commit 0acb96a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/dbt_osmosis/core/osmosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def create_yaml_instance(
indent_mapping: int = 2,
indent_sequence: int = 4,
indent_offset: int = 2,
width: int = 800,
width: int = 100,
preserve_quotes: bool = True,
default_flow_style: bool = False,
encoding: str = "utf-8",
Expand All @@ -330,6 +330,14 @@ def create_yaml_instance(
y.preserve_quotes = preserve_quotes
y.default_flow_style = default_flow_style
y.encoding = encoding

def str_representer(dumper: t.Any, data: t.Any) -> t.Any:
if len(data.splitlines()) > 1: # check for multiline string
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
return dumper.represent_scalar("tag:yaml.org,2002:str", data)

y.representer.add_representer(str, str_representer)

logger.debug(":notebook: YAML instance created => %s", y)
return y

Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,5 +587,5 @@ def test_create_yaml_instance_settings():
assert y.map_indent == 4
assert y.sequence_indent == 2
assert y.sequence_dash_offset == 0
assert y.width == 800 # default
assert y.width == 100 # default
assert y.preserve_quotes is True

0 comments on commit 0acb96a

Please sign in to comment.