method for setting first primary key field

This commit is contained in:
Pavel Merzlyakov 2023-08-04 12:23:45 +03:00
parent 6fd0628de7
commit 1534bdf481
1 changed files with 23 additions and 0 deletions

View File

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