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')) %}