From 1a2d4546cf70d660e6b3e12d59caa81238229f3c Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Thu, 7 Sep 2023 11:32:50 +0300 Subject: [PATCH] fix saving json values --- tpl/macros_struct.twig | 47 ++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 2367ba1..eb3ae2b 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -86,9 +86,9 @@ func Save{{ s.name }}Diff(ctx context.Context, dbe metadb.Execer, rec {{ s.name } var builder strings.Builder - builder.Grow({{ 50 + insert_column_expr|length }}) + builder.Grow({{ 56 + table_name|length + insert_column_expr|length + insert_values_expr|length }}) - builder.WriteString("INSERT INTO `{{ table_name }}` ({{ insert_column_expr }}) ON DUPLICATE KEY UPDATE `kv`=") + builder.WriteString("INSERT INTO `{{ table_name }}` ({{ insert_column_expr }}) VALUES({{ insert_values_expr }}) ON DUPLICATE KEY UPDATE `kv`=") if err := rec.WriteJsonSetExpr(&builder); err != nil { return err @@ -563,7 +563,7 @@ func (v {{ type_name }}) Value() (driver.Value, error) { return nil, errors.WithStack(err) } - return jsonBytes, nil + return string(jsonBytes), nil } {% endmacro json_sql_impl %} @@ -691,17 +691,11 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { contSize-- } if !use_mask || (use_mask && s.FieldChanged({{ loop.index0 }})) { - {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens) }} - {% if has_token(s, 'table') %} - s.changedFields.SetChanged("{{ f|alias }}") - {% endif %} + {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens, has_token(s, 'table')) }} } {% else %} contSize-- - {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens) }} - {% if has_token(s, 'table') %} - s.changedFields.SetChanged("{{ f|alias }}") - {% endif %} + {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, f.tokens, has_token(s, 'table')) }} {% endif %} {%- endfor -%} @@ -711,15 +705,23 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { {% endmacro meta_read %} -{% macro meta_read_field(type, fname, alias, tokens) %} +{% macro meta_read_field(type, fname, alias, tokens, has_table_token) %} {% if type is builtin %} - if err := reader.Read{{ type|builtin_type_suffix }}(&{{ fname }}, "{{ alias }}"); err != nil && err != meta.FieldNotFound { - return err - } + if err := reader.Read{{ type|builtin_type_suffix }}(&{{ fname }}, "{{ alias }}"); err != nil { + if err != meta.FieldNotFound { + return err + } + } {% if has_table_token %}else { + s.changedFields.SetChanged("{{ alias }}") + }{% endif ~%} {% elseif type is enum %} - if err := reader.ReadInt32((*int32)(&{{ fname }}), "{{ alias }}"); err != nil && err != meta.FieldNotFound { - return err - } + if err := reader.ReadInt32((*int32)(&{{ fname }}), "{{ alias }}"); err != nil { + if err != meta.FieldNotFound { + return err + } + } {% if has_table_token %}else { + s.changedFields.SetChanged("{{ alias }}") + }{% endif ~%} if !{{ fname }}.IsValid() { return errors.Errorf("bad enum value `%d` for `{{ alias }}`", {{ fname }}) } @@ -731,6 +733,9 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { } } else { {{ fname }} = v.(I{{ type.name }}) + {% if has_table_token %} + s.changedFields.SetChanged("{{ alias }}") + {% endif %} } {% else %} if err := reader.BeginContainer("{{ alias }}"); err != nil { @@ -744,6 +749,9 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { if err := reader.EndContainer(); err != nil { return err } + {% if has_table_token %} + s.changedFields.SetChanged("{{ alias }}") + {% endif %} } {% endif %} {% elseif type is array %} @@ -767,6 +775,9 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { if err := reader.EndContainer(); err != nil { return err } + {% if has_table_token %} + s.changedFields.SetChanged("{{ alias }}") + {% endif %} } {% else %} {{ Error('unkown type ' ~ type) }}