From 5929ad0f5e8860b66134915a7ab9d3b724e4d04c Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Mon, 13 Jan 2025 22:35:16 +0000 Subject: [PATCH 1/3] Add indent parameter to tojson and toyaml jinja context methods, plus tests --- core/dbt/context/base.py | 21 ++++++++++++++----- ...nctions.py => test_json_yaml_functions.py} | 2 ++ 2 files changed, 18 insertions(+), 5 deletions(-) rename tests/functional/context_methods/{test_yaml_functions.py => test_json_yaml_functions.py} (83%) diff --git a/core/dbt/context/base.py b/core/dbt/context/base.py index 5b8fd45e350..bbdb8648cdb 100644 --- a/core/dbt/context/base.py +++ b/core/dbt/context/base.py @@ -393,7 +393,9 @@ 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. @@ -401,6 +403,9 @@ def tojson(value: Any, default: Any = None, sort_keys: bool = False) -> Any: :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: @@ -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 @@ -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: @@ -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 diff --git a/tests/functional/context_methods/test_yaml_functions.py b/tests/functional/context_methods/test_json_yaml_functions.py similarity index 83% rename from tests/functional/context_methods/test_yaml_functions.py rename to tests/functional/context_methods/test_json_yaml_functions.py index e90da5f7254..2cf2175352d 100644 --- a/tests/functional/context_methods/test_yaml_functions.py +++ b/tests/functional/context_methods/test_json_yaml_functions.py @@ -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 %}) From f2e04e0e0abdadf2d2ee7d9f7e278319294ca574 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Mon, 13 Jan 2025 22:38:56 +0000 Subject: [PATCH 2/3] Add indent parameter to tojson and toyaml jinja context methods, plus tests --- .changes/unreleased/Features-20250113-223828.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Features-20250113-223828.yaml diff --git a/.changes/unreleased/Features-20250113-223828.yaml b/.changes/unreleased/Features-20250113-223828.yaml new file mode 100644 index 00000000000..4afe8073414 --- /dev/null +++ b/.changes/unreleased/Features-20250113-223828.yaml @@ -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: None From 3dd81200fe63116b2c8ce7a4b46de1c3577b6736 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Mon, 13 Jan 2025 22:49:49 +0000 Subject: [PATCH 3/3] Add issue number --- .changes/unreleased/Features-20250113-223828.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/unreleased/Features-20250113-223828.yaml b/.changes/unreleased/Features-20250113-223828.yaml index 4afe8073414..6044c353213 100644 --- a/.changes/unreleased/Features-20250113-223828.yaml +++ b/.changes/unreleased/Features-20250113-223828.yaml @@ -3,4 +3,4 @@ body: Add indent parameter to tojson and toyaml context methods time: 2025-01-13T22:38:28.480988715Z custom: Author: mjsqu - Issue: None + Issue: 11210