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

Feature: Write a macro to convert a jinja dictionary to yaml #8

Closed
clrcrl opened this issue Jul 11, 2019 · 3 comments
Closed

Feature: Write a macro to convert a jinja dictionary to yaml #8

clrcrl opened this issue Jul 11, 2019 · 3 comments
Labels

Comments

@clrcrl
Copy link
Contributor

clrcrl commented Jul 11, 2019

This will make generating yaml so much cleaner!

This is almost it, but it doesn't unnest things that are lists in the way we would expect:

{% macro dict_to_yaml(my_dict, indent=0, result="") %}
{% set ns=namespace(result=result) %}
{% for key, value in my_dict.items() %}
    {% set ns.result= ns.result ~ " " * indent ~  key %}

    {% if value is mapping %} {# if it's a dictionary, recurse #}
    {% set ns.result=dict_to_yaml(value, indent + 2, ns.result ~ ":\n") %}
    {% elif value is iterable and value is not string %} {# janky way to check for a list #}
    {% set ns.result = ns.result ~ ":\n" %}
    {% for item in value %}
        {% set ns.result = ns.result ~ " " * indent ~ "- " ~ item ~ "\n" %}
    {% endfor %}
    {% else %}
    {% set ns.result = ns.result ~ ": " ~ value ~ "\n" %}
    {% endif %}

{% endfor %}

{{ return(ns.result) }}
{% endmacro %}
@github-actions
Copy link

This issue has been marked as Stale because it has been open for 180 days with no activity. If you would like the issue to remain open, please comment on the issue or else it will be closed in 7 days.

@github-actions github-actions bot added the Stale label Aug 10, 2023
@github-actions
Copy link

Although we are closing this issue as stale, it's not gone forever. Issues can be reopened if there is renewed community interest. Just add a comment to notify the maintainers.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2023
@tema-popov
Copy link

For anyone who could stumble on it from a Google search: there is a toyaml function in DBT right now to generate yaml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants