get rid of readFieldsAssoc method

This commit is contained in:
Pavel Merzlyakov 2023-06-16 01:02:53 +03:00
parent 4066ee41d1
commit f8af876761
2 changed files with 67 additions and 79 deletions

View File

@ -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) }}

View File

@ -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 %}
{% 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 %}