Skip to content

Instantly share code, notes, and snippets.

@bcrisp4
Last active June 21, 2022 21:59
Show Gist options
  • Save bcrisp4/0b480683f0729847af4cd6a9bcdb92b5 to your computer and use it in GitHub Desktop.
Save bcrisp4/0b480683f0729847af4cd6a9bcdb92b5 to your computer and use it in GitHub Desktop.
Jinja2 template for textproto config format used by Bazel Buildfarm
{#-
This template attempts to produce an output that conforms to the
notoriously poorly documented "textproto" (human-readable protobuf)
format, as used by Bazel Buildfarm for config files.
#}
{%- macro write_boolean(key, value) %}
{%- if value is sameas true %}
{{ key }}: true
{%- else %}
{{ key }}: false
{%- endif %}
{%- endmacro %}
{%- macro write_number(key, value) %}
{{ key }}: {{ value }}
{%- endmacro %}
{%- macro write_string(key, value) %}
{{ key }}: "{{ value }}"
{%- endmacro %}
{%- macro write_default(key, value) %}
{{- write_string(key, value) }}
{%- endmacro %}
{%- macro write_value(key, value) %}
{%- if value is boolean %}
{{- write_boolean(key, value) }}
{%- elif value is number %}
{{- write_number(key, value) }}
{%- elif value is string %}
{{- write_string(key, value) }}
{%- else %}
{{- write_default(key, value) }}
{%- endif %}
{%- endmacro %}
{%- macro write_config(config) %}
{%- for key, value in config.items() %}
{%- if value is mapping %}
{{ key }}: {
{{- write_config(value) | indent(2) }}
}
{%- elif value is iterable and value is not string %}
{%- for item in value %}
{{- write_config({key: item}) }}
{%- endfor %}
{%- else %}
{{- write_value(key, value) }}
{%- endif %}
{%- endfor %}
{%- endmacro %}
{{- write_config(config) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment