How can I fix linter error `line too long` when using dbt_utils.generate_surrogate_key macro?

115 views Asked by At

I am a novice in DBT. I used the dbt_utils.geenrate_surrogate_key() macro and it compiles correctly, but generates all the outpu sql in one line. That in turn causes the linter (sqlfluff) to complain about long line.

I could edit the macro by hand to add some newlines, but that would be a non-maintainable hack. Is there an established way to deal with this problem?

1

There are 1 answers

0
Ilia Gilmijarow On

I ended up writing my own macro to split lines of code into indented blocks.

{#
    Split a single line of code into multiple lines indented by nesting depth according to opening and closing characters (round parens by default)
    Optionally split into lines the parts separated by a `separator` (usually a comma)
#}
{%- macro line_to_code_block(the_line, open_char = '(', close_char = ')', sep = '') -%}
    {%- set x = the_line.replace(open_char, open_char + '\n') -%}
    {%- if sep != '' -%}
        {%- set x = x.replace(sep, sep + '\n') -%}
    {%- endif -%}
    {%- set x = x.replace(close_char, '\n' + close_char) -%}
    {%- set lines = x.splitlines() -%}
    {%- set indent = '    ' -%} {# indentation string #}
    {%- set indentation = [''] -%} 
    {%- for line in lines -%}
        {%- if indentation|length > 1 and line.startswith(close_char) == True -%}
            {%- do indentation.pop() -%}
        {%- endif -%}
        {{ indentation|join(indent) }}{{ line|trim }}{{ '\n' }}
        {%- if line.endswith(open_char) == True  -%}
            {%- do indentation.append('') -%}
        {%- endif -%}
    {%- endfor -%}
{%- endmacro -%}