diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 7a485c8..5fd73dc 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -14,6 +14,7 @@ {{ _self.root_delete(s) }} {{ _self.root_save_diff(s) }} {{ _self.root_delete_diff(s) }} + {{ _self.root_load(s) }} {% endif %} {% endmacro struct %} @@ -259,6 +260,26 @@ func Delete{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, ids {{ s.nam {% endmacro root_delete_diff %} +{% macro root_load(s) %} +{% set load_by = meta_field(token(s, 'data_root')) %} +func Load{{ s.name }}(ctx context.Context, dbq metadb.Querier, {{ load_by|varname }} {{ load_by.type|go_type }}) ({{ s.name }}, error) { + var s {{ s.name }} + {%~ for f in s.fields %} + {%~ if f.type is array %} + {{ f|varname }}, err := Load{{ f.type.value.name }}Collection(ctx, dbq, {{ load_by|varname }}) + {%~ else %} + {{ f|varname }}, err := Load{{ f.type.name }}(ctx, dbq, {{ load_by|varname }}) + {%~ endif %} + if err != nil { + return {{ s.name }}{}, err + } + s.{{ f|fname }} = {{ f|varname }} + {%~ endfor %} + return s, nil +} +{% endmacro root_load %} + + {% macro table_save(ctx) %} func Save{{ ctx.s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ ctx.s.name }}) error { query := "INSERT INTO `{{ ctx.table_name }}` ({{ ctx.insert_column_expr }}) VALUES ({{ ctx.insert_values_expr }}) ON DUPLICATE KEY UPDATE {{ ctx.insert_update_expr }}"