Skip to content

Commit 4a46a7b

Browse files
committed
agate_to_dicts: improve decimal serialization
1 parent 72d9576 commit 4a46a7b

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

macros/utils/run_queries/agate_to_dicts.sql

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,24 @@
2323
{% if val.year is defined %}
2424
{% do return(val.isoformat()) %}
2525
{% endif %}
26-
{% if val is number and '.' in val | string %}
27-
{# a bit of a hacky way to standardize Decimals which are not JSON-serializable #}
28-
{% do return(val | string | float) %}
26+
{% if elementary.edr_is_decimal(val) %}
27+
{% do return(elementary.edr_serialize_decimal(val)) %}
2928
{% endif %}
3029
{% do return(val) %}
3130
{% endmacro %}
31+
32+
{% macro edr_is_decimal(val) %}
33+
{# A hacky way to check if a value is of type Decimal, as there isn't a straightforward way to check that #}
34+
{% do return(val is number and val.normalize is defined and val.normalize is not none) %}
35+
{% endmacro %}
36+
37+
{% macro edr_serialize_decimal(val) %}
38+
{% set dec_tuple = val.normalize().as_tuple() %}
39+
40+
{# A hacky way to standardize Decimals which are not JSON-serializable #}
41+
{% if dec_tuple[2] == 0 %}
42+
{% do return(val | string | int) %}
43+
{% else %}
44+
{% do return(val | string | float) %}
45+
{% endif %}
46+
{% endmacro %}

0 commit comments

Comments
 (0)