From 1534bdf481cb8c8165e126193cc17f4c909f31aa Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Fri, 4 Aug 2023 12:23:45 +0300 Subject: [PATCH] method for setting first primary key field --- tpl/macros_struct.twig | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 03ced6f..252a626 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -10,6 +10,7 @@ {% endif %} {% if has_token(s, 'data_root') %} + {{ _self.root_set_pk(s) }} {{ _self.root_save(s) }} {{ _self.root_delete(s) }} {{ _self.root_save_diff(s) }} @@ -187,6 +188,28 @@ func Save{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ s.name {% endmacro struct_table %} +{% macro root_set_pk(s) %} +{% set set_field = meta_field(token(s, 'data_root')) %} +func (s *{{ s.name }}) Set{{ set_field|fname }}({{ set_field|varname }} {{ set_field.type|go_type }}) { + {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip')) %} + {%~ if f.type is array %} + {% set pkey = table_pkey(f.type.value) %} + {% set field_name = pkey|first|fname %} + + for i := range s.{{ f|fname }} { + s.{{ f|fname }}[i].{{ field_name }} = {{ set_field|varname }} + } + {%~ else %} + {% set pkey = table_pkey(f.type) %} + {% set field_name = pkey|first|fname %} + + s.{{ f|fname }}.{{ field_name }} = {{ set_field|varname }} + {%~ endif %} + {%~ endfor %} +} +{% endmacro root_set_pk %} + + {% macro root_save(s) %} func Save{{ s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ s.name }}) error { {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip')) %}