From 49cc0bd8ae2c917961f55affce32f0413814ab15 Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Fri, 19 Jan 2024 01:09:14 +0300 Subject: [PATCH] generating set pk method for struct with diff_removed token --- tpl/macros_struct.twig | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index d30dd35..37385e7 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -41,6 +41,10 @@ type {{ s.name }} struct { {{ _self.data_root_methods(s) }} {% endif %} +{% if has_token(s, 'diff_removed') %} +{{ _self.diff_removed_methods(s) }} +{% endif %} + {% if json_fields|length > 0 %} type {{ json_fields_type }} struct{ {%~ for f in json_fields %} @@ -887,18 +891,24 @@ func (s *{{ name }}) FieldsMask() meta.FieldsMask { {% set root_field = meta_field(token(s, 'data_root')) %} {% set fields = s.fields|filter(f => not has_token(f, 'db_skip')) %} -{{ _self.root_set_pk(_context) }} +{{ _self.root_set_pk(s.name, fields, root_field) }} {{ _self.root_save(_context) }} {{ _self.root_load(_context) }} {{ _self.root_delete(_context) }} {% endmacro %} -{% macro root_set_pk(ctx) %} -{% set s = ctx.s %} -{% set set_field = ctx.root_field %} -func (s *{{ s.name }}) Set{{ set_field|fname }}({{ set_field|varname }} {{ set_field.type|go_type }}) { - {%~ for f in ctx.fields %} +{% macro diff_removed_methods(s) %} +{% set root_field = meta_field(token(s, 'data_root')) %} +{% set fields = s.fields|filter(f => not has_token(f, 'db_skip')) %} + +{{ _self.root_set_pk(s.name, fields, root_field) }} +{% endmacro %} + + +{% macro root_set_pk(struct_name, fields, set_field) %} +func (s *{{ struct_name }}) Set{{ set_field|fname }}({{ set_field|varname }} {{ set_field.type|go_type }}) { + {%~ for f in fields %} {%~ if f.type is array %} {% set pkey = table_pkey(f.type.value) %} {% set field_name = pkey|first|fname %}