Skip to content

Commit

Permalink
Merge pull request #18 from emilyriederer/whitespace
Browse files Browse the repository at this point in the history
Fix Jinja templating to improve whitespace in macro results
  • Loading branch information
emilyriederer authored Oct 27, 2022
2 parents e5b75b5 + c7c336a commit 5545a2a
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 113 deletions.
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'dbtplyr'
version: '0.3.2'
version: '0.3.3'
config-version: 2
require-dbt-version: ">=1.0.0"

Expand Down
10 changes: 5 additions & 5 deletions integration_tests/models/as_values.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{% set cols = dbtplyr.get_column_names( ref('data') ) %}
{% set cast_to %}
{% if target.type == 'postgres' %}
{%- set cols = dbtplyr.get_column_names( ref('data') ) %}
{%- set cast_to %}
{%- if target.type == 'postgres' -%}
text
{% else %}
{%- else -%}
string
{% endif %}
{%- endif -%}
{% endset %}

select
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/models/contains.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.contains( 'c', ref('data') ) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/ends_with.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.ends_with( 'c', ref('data') ) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/everything.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.everything( ref('data')) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/matches.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.matches('.*_c', ref('data')) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/not_contains.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.not_contains('n', ref('data')) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/not_one_of.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.not_one_of(['n_a', 'ind_a', 'ind_b'], ref('data')) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/one_of.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.one_of(['amt_c'], ref('data')) %}
select {{ dbtplyr.across(cols_n, "{{var}}") }}
select
{{ dbtplyr.across(cols_n, "{{var}}") }}
from {{ ref('data') }}
3 changes: 2 additions & 1 deletion integration_tests/models/starts_with.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% set cols_n = dbtplyr.starts_with('amt', ref('data')) %}
select {{ dbtplyr.across(cols_n) }}
select
{{ dbtplyr.across(cols_n) }}
from {{ ref('data') }}
46 changes: 21 additions & 25 deletions macros/across.sql
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
{% macro across(var_list, script_string = '{{var}}', final_comma = false) %}
{{ adapter.dispatch('across', 'dbtplyr') (var_list, script_string, final_comma) }}
{% endmacro %}
{%- macro across(var_list, script_string = '{{var}}', final_comma = false) -%}
{{- adapter.dispatch('across', 'dbtplyr') (var_list, script_string, final_comma) -}}
{%- endmacro -%}

{% macro default__across(var_list, script_string, final_comma) %}
{%- macro default__across(var_list, script_string, final_comma) -%}
{%- for v in var_list %}
{{- script_string | replace('{{var}}', v) -}}
{%- if not loop.last -%},{%- endif -%}
{%- if loop.last and final_comma|default(false) -%},{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{% for v in var_list %}
{{ script_string | replace('{{var}}', v) }}
{%- if not loop.last %},{% endif %}
{%- if loop.last and final_comma|default(false) %},{% endif %}
{% endfor %}
{%- macro c_across(var_list, script_string) -%}
{{- adapter.dispatch('c_across', 'dbtplyr') (var_list, script_string) -}}
{%- endmacro -%}

{% endmacro %}

{% macro c_across(var_list, script_string) %}
{{ adapter.dispatch('c_across', 'dbtplyr') (var_list, script_string) }}
{% endmacro %}

{% macro default__c_across(var_list, script_string) %}

{% if script_string | length < 2 %}
{{ var_list | join(script_string) }}
{% else %}
{% set vars = var_list | join(",") %}
{{ script_string | replace('{{var}}', vars) }}
{% endif %}

{% endmacro %}
{%- macro default__c_across(var_list, script_string) -%}
{%- if script_string | length < 2 -%}
{{- var_list | join(script_string) -}}
{%- else -%}
{%- set vars = var_list | join(",") -%}
{{- script_string | replace('{{var}}', vars) -}}
{%- endif -%}
{%- endmacro -%}
20 changes: 10 additions & 10 deletions macros/get_column_names.sql
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{% macro get_column_names(relation) %}
{%- macro get_column_names(relation) -%}
{{ return( adapter.dispatch('get_column_names', 'dbtplyr')(relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__get_column_names(relation) %}
{%- macro default__get_column_names(relation) -%}

{# if relation is not actually a reference simply pass through #}
{# this is useful so downstream functions can accept either list or relation #}

{% if relation.database is not defined %}
{%- if relation.database is not defined -%}
{{ return(relation) }}
{% endif %}
{%- endif -%}

{%- set cols_list = adapter.get_columns_in_relation(relation) -%}
{% set results_list = [] %}
{% for c in cols_list %}
{{ results_list.append(c.name) }}
{% endfor %}
{%- set results_list = [] -%}
{%- for c in cols_list -%}
{{ results_list.append(c.name) }}
{%- endfor -%}
{{ return(results_list) }}

{% endmacro %}
{%- endmacro -%}
118 changes: 58 additions & 60 deletions macros/select_helpers.sql
Original file line number Diff line number Diff line change
@@ -1,109 +1,107 @@
{% macro starts_with(string, relation) %}
{%- macro starts_with(string, relation) %}
{{ return( adapter.dispatch('starts_with', 'dbtplyr') (string, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__starts_with(string, relation) %}
{%- macro default__starts_with(string, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set regex = "^" ~ string ~ ".*" %}
{%set results = dbtplyr.get_matches(cols, regex) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set regex = "^" ~ string ~ ".*" %}
{% set results = dbtplyr.get_matches(cols, regex) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro ends_with(string, relation) %}
{%- macro ends_with(string, relation) %}
{{ return( adapter.dispatch('ends_with', 'dbtplyr') (string, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__ends_with(string, relation) %}
{%- macro default__ends_with(string, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set regex = "^.*" ~ string ~ "$" %}
{%set results = dbtplyr.get_matches(cols, regex) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set regex = "^.*" ~ string ~ "$" %}
{% set results = dbtplyr.get_matches(cols, regex) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro contains(string, relation) %}
{%- macro contains(string, relation) %}
{{ return( adapter.dispatch('contains', 'dbtplyr') (string, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__contains(string, relation) %}
{%- macro default__contains(string, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set regex = "^.*" ~ string ~ ".*$" %}
{%set results = dbtplyr.get_matches(cols, regex) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set regex = "^.*" ~ string ~ ".*$" %}
{% set results = dbtplyr.get_matches(cols, regex) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro not_contains(string, relation) %}
{%- macro not_contains(string, relation) %}
{{ return( adapter.dispatch('not_contains', 'dbtplyr') (string, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__not_contains(string, relation) %}
{%- macro default__not_contains(string, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set regex = "^((?!" ~ string ~ ").)*$" %}
{%set results = dbtplyr.get_matches(cols, regex) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set regex = "^((?!" ~ string ~ ").)*$" %}
{% set results = dbtplyr.get_matches(cols, regex) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro one_of(strings, relation) %}
{%- macro one_of(strings, relation) %}
{{ return( adapter.dispatch('one_of', 'dbtplyr') (strings, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__one_of(strings, relation) %}
{%- macro default__one_of(strings, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set regex = "^("+ strings|join("|") +")$" %}
{%set results = dbtplyr.get_matches(cols, regex) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set regex = "^("+ strings|join("|") +")$" %}
{% set results = dbtplyr.get_matches(cols, regex) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro not_one_of(strings, relation) %}
{%- macro not_one_of(strings, relation) %}
{{ return( adapter.dispatch('not_one_of', 'dbtplyr') (strings, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__not_one_of(strings, relation) %}
{%- macro default__not_one_of(strings, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set results = cols | reject('in', strings) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set results = cols | reject('in', strings) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro matches(string, relation) %}
{%- macro matches(string, relation) %}
{{ return( adapter.dispatch('matches', 'dbtplyr') (string, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__matches(string, relation) %}
{%- macro default__matches(string, relation) %}

{%set cols = dbtplyr.get_column_names(relation) %}
{%set regex = string %}
{%set results = dbtplyr.get_matches(cols, regex) %}
{% set cols = dbtplyr.get_column_names(relation) %}
{% set regex = string %}
{% set results = dbtplyr.get_matches(cols, regex) %}
{{return(results)}}

{% endmacro %}
{%- endmacro -%}

{% macro everything(relation) %}
{%- macro everything(relation) -%}
{{ return( adapter.dispatch('everything', 'dbtplyr') (relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__everything(relation) %}
{%- macro default__everything(relation) -%}
{%- set cols = dbtplyr.get_column_names(relation) -%}
{{ return(cols) }}
{%- endmacro -%}

{%set cols = dbtplyr.get_column_names(relation) %}
{{return(cols)}}

{% endmacro %}

{% macro where(fn, relation) %}
{%- macro where(fn, relation) %}
{{ return( adapter.dispatch('where', 'dbtplyr') (fn, relation) ) }}
{% endmacro %}
{%- endmacro -%}

{% macro default__where(fn, relation) %}
{%- macro default__where(fn, relation) %}

{% set cols = adapter.get_columns_in_relation(relation) %}
{% set results_list = [] %}
Expand All @@ -122,4 +120,4 @@

{{return(results_list)}}

{% endmacro %}
{%- endmacro -%}
8 changes: 4 additions & 4 deletions macros/where_helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

{% macro default__if_any(var_list, script_string) %}

{% for v in var_list %}
{%- for v in var_list -%}
{{ "(" ~ script_string | replace('{{var}}', v) ~ ")" }}
{%- if not loop.last %} or {% endif %}
{% endfor %}
{%- endfor -%}

{% endmacro %}

Expand All @@ -17,9 +17,9 @@

{% macro default__if_all(var_list, script_string) %}

{% for v in var_list %}
{%- for v in var_list -%}
{{ "(" ~ script_string | replace('{{var}}', v) ~ ")" }}
{%- if not loop.last %} and {% endif %}
{% endfor %}
{%- endfor -%}

{% endmacro %}

0 comments on commit 5545a2a

Please sign in to comment.