From e7fa182cb4bf77d2624d3b645a558a723bad3f1b Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Mon, 31 Jul 2023 17:48:54 +0300 Subject: [PATCH] db_skip token support --- src/codegen.inc.php | 1 + tpl/macros_struct.twig | 20 +++++++++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index ac39433..aad2a8f 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -39,6 +39,7 @@ function supported_tokens() 'table', 'table_pkey', 'table_json_kv', + 'db_skip', 'data_root', 'bitfields', diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 854c419..03ced6f 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -189,7 +189,7 @@ func Save{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ s.name {% macro root_save(s) %} func Save{{ s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ s.name }}) error { - {%~ for f in s.fields %} + {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip')) %} {%~ if f.type is array %} if err := Save{{ f.type.value.name }}Collection(ctx, dbe, rec.{{ f|fname }}); err != nil { return err @@ -207,7 +207,7 @@ func Save{{ s.name }}(ctx context.Context, dbe metadb.Execer, rec {{ s.name }}) {% macro root_save_diff(s) %} func Save{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ s.name }}) error { - {%~ for f in s.fields %} + {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip')) %} {%~ if f.type is array %} if err := Save{{ f.type.value.name }}CollectionDiff(ctx, dbe, rec.{{ f|fname }}); err != nil { return err @@ -226,11 +226,15 @@ func Save{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ s.name {% macro root_delete(s) %} {% set delete_by = meta_field(token(s, 'data_root')) %} func Delete{{ s.name }}(ctx context.Context, dbe metadb.Execer, {{ delete_by|varname }} {{ delete_by.type|go_type }}) error { - {%~ for f in s.fields %} + {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip')) %} {%~ if f.type is array %} if err := Delete{{ f.type.value.name }}Collection(ctx, dbe, {{ delete_by|varname }}); err != nil { return err } + {%~ elseif f.type is struct %} + if _, err := Delete{{ f.type.name }}(ctx, dbe, {{ delete_by|varname }}); err != nil { + return err + } {%~ else %} if err := Delete{{ f.type.name }}(ctx, dbe, {{ delete_by|varname }}); err != nil { return err @@ -244,16 +248,10 @@ func Delete{{ s.name }}(ctx context.Context, dbe metadb.Execer, {{ delete_by|var {% macro root_delete_diff(s) %} func Delete{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, ids {{ s.name }}RemovedIds) error { - {%~ for f in s.fields %} - {%~ if f.type is array %} + {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip') and f.type is array) %} if err := Delete{{ f.type.value.name }}CollectionById(ctx, dbe, ids.{{ f|fname }}); err != nil { return err } - {%~ else %} - if err := Delete{{ f.type.name }}ById(ctx, dbe, ids.{{ f|fname }}); err != nil { - return err - } - {%~ endif %} {%~ endfor %} return nil } @@ -264,7 +262,7 @@ func Delete{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, ids {{ s.nam {% 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 %} + {%~ for f in s.fields|filter(f => not has_token(f, 'db_skip')) %} {%~ if f.type is array %} {{ f|varname }}, err := Load{{ f.type.value.name }}Collection(ctx, dbq, {{ load_by|varname }}) {%~ else %}