From f8af876761452774047ccd1c66c066e68f70400e Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Fri, 16 Jun 2023 01:02:53 +0300 Subject: [PATCH] get rid of readFieldsAssoc method --- tpl/macros_rpc.twig | 1 - tpl/macros_struct.twig | 145 +++++++++++++++++++---------------------- 2 files changed, 67 insertions(+), 79 deletions(-) diff --git a/tpl/macros_rpc.twig b/tpl/macros_rpc.twig index ab7f017..619705f 100644 --- a/tpl/macros_rpc.twig +++ b/tpl/macros_rpc.twig @@ -66,7 +66,6 @@ func New{{ req_name }}() *{{ req_name }} { return req } -{{ struct_macros.struct_required_fields(req_name, r.req.fields)}} {{ struct_macros.reset_method(r.req, req_name) }} {{ struct_macros.meta_read(r.req, req_name) }} diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 557e41f..6d8e2af 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -60,11 +60,9 @@ type {{ json_fields_type }} struct{ {{ _self.json_sql_impl(json_fields_type) }} {{ _self.struct_methods(s.name, s.classid) }} -{{ _self.struct_required_fields(s.name, s.fields) }} {{ _self.reset_method(s) }} {{ _self.meta_read(s) }} {{ _self.meta_write(s) }} -{# {{ _self.struct_identifier(s.name, pkey) }} #} {% if has_token(s, 'bitfields') %} {{ _self.bitfields_methods(s.name) }} @@ -588,12 +586,11 @@ func (s *{{ name|default(o.name) }}) Reset() { {% endmacro reset_method %} -{% macro meta_read(o, name) %} -{% set fields_len = o.fields|length %} -{% set opt_fields_len = count_optional(o.fields) %} -{% set all_fields = get_all_fields(o) %} +{% macro meta_read(s, name) %} +{% set fields_len = s.fields|length %} +{% set opt_fields_len = count_optional(s.fields) %} -func (s *{{ name|default(o.name) }}) Read(reader meta.Reader) error { +func (s *{{ name|default(s.name) }}) Read(reader meta.Reader) error { if err := reader.BeginContainer(""); err != nil { return err } @@ -603,59 +600,10 @@ func (s *{{ name|default(o.name) }}) Read(reader meta.Reader) error { return reader.EndContainer() } -func (s *{{ name|default(o.name) }}) ReadFields(reader meta.Reader) error { +func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { s.Reset() - readAsMap, err := reader.IsContainerAssoc() - if err != nil { - return err - } - if readAsMap { - return s.readFieldsAssociative(reader) - } - return s.readFields(reader) -} - -func (s *{{ name|default(o.name) }}) readFieldsAssociative(reader meta.Reader) error { - size, err := reader.ContainerSize() - if err != nil { - return err - } - - readFields := make(map[string]struct{}, {{ all_fields|length }}) - for ; size > 0; size-- { - var field string - if err := reader.ReadString(&field, ""); err != nil { - return err - } - - switch field { - {% for f in all_fields %} - case "{{ f|alias }}": - {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }} - {% endfor %} - default: - {# continue // do not return an error for nested structs #} - return errors.Errorf("unexpected field `%s`", field) - } - - readFields[field] = struct{}{} - {% if has_token(o, 'table') %} - s.changedFields.SetChanged(field) - {% endif %} - } - - for field := range _{{ name|default(o.name) }}RequiredFields { - if _, ok := readFields[field]; !ok { - return errors.Errorf("field `%s` is not present", field) - } - } - - return nil -} - -func (s *{{ name|default(o.name) }}) readFields(reader meta.Reader) error { - {% if has_token(o, 'bitfields') %} + {% if has_token(s, 'bitfields') %} use_mask, mask, err := reader.TryReadMask() if err != nil { return err @@ -672,31 +620,31 @@ func (s *{{ name|default(o.name) }}) readFields(reader meta.Reader) error { contSize = {{ fields_len - opt_fields_len }} } - {% if o.parent %} - if err := s.{{ o.parent.name }}.ReadFields(reader); err != nil { + {% if s.parent %} + if err := s.{{ s.parent.name }}.ReadFields(reader); err != nil { return err } {% endif %} - {% for f in o.fields %} + {% for f in s.fields %} if contSize <= 0 { return nil } - {% if has_token(o, 'bitfields') %} + {% if has_token(s, 'bitfields') %} if !use_mask { contSize-- } if !use_mask || (use_mask && s.FieldChanged({{ loop.index0 }})) { {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }} - {% if has_token(o, 'table') %} + {% if has_token(s, 'table') %} s.changedFields.SetChanged("{{ f|alias }}") {% endif %} } {% else %} contSize-- {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }} - {% if has_token(o, 'table') %} + {% if has_token(s, 'table') %} s.changedFields.SetChanged("{{ f|alias }}") {% endif %} {% endif %} @@ -867,8 +815,6 @@ type {{ s.name }} struct { } {{ _self.struct_methods(s.name, s.classid) }} - -{{ _self.struct_required_fields(s.name, s.fields) }} {{ _self.reset_method(s) }} {{ _self.meta_read(s) }} {{ _self.meta_write(s) }} @@ -909,17 +855,6 @@ func (s *{{ name }}) Ptr{{ name }}() *{{ name }} { {% endmacro struct_methods %} -{% macro struct_required_fields(name, fields) %} -var _{{ name }}RequiredFields map[string]struct{} -func init() { - _{{ name }}RequiredFields = map[string]struct{}{ - {% for f in fields|filter(f => not has_token(f, 'optional')) %} - "{{ f|alias }}": {}, - {% endfor %} - } -} -{% endmacro struct_required_fields %} - {% macro bitfields_methods(name) %} func (s *{{ name }}) FieldChanged(index uint64) bool { return s.fieldsMask.FieldChanged(index) @@ -1070,4 +1005,58 @@ func (s *{{ name }}) EncodeMsgpack(enc *msgpack.Encoder) error { {%~ else %} {{ Error('unexpected type ' ~ type) }} {%~ endif %} -{% endmacro msgpack_value_encode %} \ No newline at end of file +{% endmacro msgpack_value_encode %} + + +{% macro struct_required_fields(name, fields) %} +var _{{ name }}RequiredFields map[string]struct{} +func init() { + _{{ name }}RequiredFields = map[string]struct{}{ + {% for f in fields|filter(f => not has_token(f, 'optional')) %} + "{{ f|alias }}": {}, + {% endfor %} + } +} +{% endmacro struct_required_fields %} + + +{% macro meta_read_assoc(s, name) %} +{% set all_fields = get_all_fields(s) %} +func (s *{{ name|default(s.name) }}) readFieldsAssociative(reader meta.Reader) error { + size, err := reader.ContainerSize() + if err != nil { + return err + } + + readFields := make(map[string]struct{}, {{ all_fields|length }}) + for ; size > 0; size-- { + var field string + if err := reader.ReadString(&field, ""); err != nil { + return err + } + + switch field { + {% for f in all_fields %} + case "{{ f|alias }}": + {{ _self.meta_read_field(f.type, 's.' ~ f|fname, f|alias, has_token(f, 'optional')) }} + {% endfor %} + default: + {# continue // do not return an error for nested structs #} + return errors.Errorf("unexpected field `%s`", field) + } + + readFields[field] = struct{}{} + {% if has_token(s, 'table') %} + s.changedFields.SetChanged(field) + {% endif %} + } + + for field := range _{{ name|default(s.name) }}RequiredFields { + if _, ok := readFields[field]; !ok { + return errors.Errorf("field `%s` is not present", field) + } + } + + return nil +} +{% endmacro meta_read_assoc %} \ No newline at end of file