From 00bb2d96dda0bbcd2dade7afc9bc24291d95e3c1 Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Thu, 27 Jul 2023 09:20:44 +0300 Subject: [PATCH] skip reading field if not found --- tpl/macros_struct.twig | 50 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 5fd73dc..854c419 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -688,11 +688,11 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { {% macro meta_read_field(type, fname, alias) %} {% if type is builtin %} - if err := reader.Read{{ type|builtin_type_suffix }}(&{{ fname }}, "{{ alias }}"); err != nil { + if err := reader.Read{{ type|builtin_type_suffix }}(&{{ fname }}, "{{ alias }}"); err != nil && err != meta.FieldNotFound { return err } {% elseif type is enum %} - if err := reader.ReadInt32((*int32)(&{{ fname }}), "{{ alias }}"); err != nil { + if err := reader.ReadInt32((*int32)(&{{ fname }}), "{{ alias }}"); err != nil && err != meta.FieldNotFound { return err } if !{{ fname }}.IsValid() { @@ -700,32 +700,38 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { } {% elseif type is struct %} if err := reader.BeginContainer("{{ alias }}"); err != nil { - return err - } - if err := {{ fname }}.ReadFields(reader); err != nil { - return err - } - if err := reader.EndContainer(); err != nil { - return err + if err != meta.FieldNotFound { + return err + } + } else { + if err := {{ fname }}.ReadFields(reader); err != nil { + return err + } + if err := reader.EndContainer(); err != nil { + return err + } } {% elseif type is array %} {% set name = fname|split('.')|last %} {% set size_var = name|lcfirst ~ 'Size' %} {% set tmp_var = 'tmp' ~ name %} if err := reader.BeginContainer("{{ alias }}"); err != nil { - return err - } - {{ size_var }}, err := reader.ContainerSize() - if err != nil { - return err - } - for ; {{ size_var }} > 0; {{ size_var }}-- { - var {{ tmp_var }} {{ type.value|go_type }} - {{ _self.meta_read_field(type.value, tmp_var) }} - {{ fname }} = append({{ fname }}, {{ tmp_var }}) - } - if err := reader.EndContainer(); err != nil { - return err + if err != meta.FieldNotFound { + return err + } + } else { + {{ size_var }}, err := reader.ContainerSize() + if err != nil { + return err + } + for ; {{ size_var }} > 0; {{ size_var }}-- { + var {{ tmp_var }} {{ type.value|go_type }} + {{ _self.meta_read_field(type.value, tmp_var) }} + {{ fname }} = append({{ fname }}, {{ tmp_var }}) + } + if err := reader.EndContainer(); err != nil { + return err + } } {% else %} {{ Error('unkown type ' ~ type) }}