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 indenting to toyaml & tojson context methods #11211

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20250113-223828.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add indent parameter to tojson and toyaml context methods
time: 2025-01-13T22:38:28.480988715Z
custom:
Author: mjsqu
Issue: 11210
21 changes: 16 additions & 5 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,19 @@ def fromjson(string: str, default: Any = None) -> Any:

@contextmember()
@staticmethod
def tojson(value: Any, default: Any = None, sort_keys: bool = False) -> Any:
def tojson(
value: Any, default: Any = None, sort_keys: bool = False, indent: int | str | None = None
) -> Any:
"""The `tojson` context method can be used to serialize a Python
object primitive, eg. a `dict` or `list` to a json string.

:param value: The value serialize to json
:param default: A default value to return if the `value` argument
cannot be serialized
:param sort_keys: If True, sort the keys.
:param indent: If indent is a non-negative integer, then nested
structures will be serialized indented by that value. An indent
value of 0 will insert newlines with no indenting.


Usage:
Expand All @@ -410,7 +415,7 @@ def tojson(value: Any, default: Any = None, sort_keys: bool = False) -> Any:
{% do log(my_json_string) %}
"""
try:
return json.dumps(value, sort_keys=sort_keys)
return json.dumps(value, sort_keys=sort_keys, indent=indent)
except ValueError:
return default

Expand Down Expand Up @@ -448,15 +453,21 @@ def fromyaml(value: str, default: Any = None) -> Any:
@contextmember()
@staticmethod
def toyaml(
value: Any, default: Optional[str] = None, sort_keys: bool = False
value: Any,
default: Optional[str] = None,
sort_keys: bool = False,
indent: int | None = None,
) -> Optional[str]:
"""The `tojson` context method can be used to serialize a Python
"""The `toyaml` context method can be used to serialize a Python
object primitive, eg. a `dict` or `list` to a yaml string.

:param value: The value serialize to yaml
:param default: A default value to return if the `value` argument
cannot be serialized
:param sort_keys: If True, sort the keys.
:param indent: If indent is a non-negative integer, the number of
spaces by which to indent nested structures (Default for YAML
is indenting by 2 spaces)


Usage:
Expand All @@ -466,7 +477,7 @@ def toyaml(
{% do log(my_yaml_string) %}
"""
try:
return yaml.safe_dump(data=value, sort_keys=sort_keys)
return yaml.safe_dump(data=value, sort_keys=sort_keys, indent=indent)
except (ValueError, yaml.YAMLError):
return default

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
{% set default_sort = (toyaml({'b': 2, 'a': 1}) == 'b: 2\\na: 1\\n') %}
{% set unsorted = (toyaml({'b': 2, 'a': 1}, sort_keys=False) == 'b: 2\\na: 1\\n') %}
{% set sorted = (toyaml({'b': 2, 'a': 1}, sort_keys=True) == 'a: 1\\nb: 2\\n') %}
{% set yaml_indented = (toyaml({'a': {'ab': 1}, 'b': {'ba':2}}, indent=8) == 'a:\\n ab: 1\\nb:\\n ba: 2\\n') %}
{% set json_indented = (tojson({'a': {'ab': 1}, 'b': {'ba':2}}, indent=4) == '{\\n "a": {\\n "ab": 1\\n },\\n "b": {\\n "ba": 2\\n }\\n}') %}
{% set default_results = (toyaml({'a': adapter}, 'failed') == 'failed') %}

(select 'simplest' as name {% if simplest %}limit 0{% endif %})
Expand Down
Loading