From a915af9640527e706091d0bc6723aaf0864f88d3 Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Thu, 23 Nov 2023 21:42:04 +0300 Subject: [PATCH] fix reading null structs --- src/codegen.inc.php | 9 +++++++++ tpl/macros_struct.twig | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 7dbd1c7..0bef3e9 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -454,6 +454,7 @@ function default_value(\mtgMetaField $field) $type = $field->getType(); $tokens = $field->getTokens(); $default = $field->getToken('default'); + $is_virtual = $field->hasToken('virtual'); if($type instanceof \mtgBuiltinType) { @@ -476,6 +477,14 @@ function default_value(\mtgMetaField $field) } else if($type instanceof \mtgMetaEnum) return $default ? go_type($type, $tokens)."_".trim($tokens['default'], '"') : 0; + else if($type instanceof \mtgMetaStruct) + { + if(!empty($default) && $default != 'null') + { + throw new Exception("not supported default value for struct '{$field->getName()}' with type '$type'"); + } + return ($is_virtual ? '&' : '').go_type($type).'{}'; + } else throw new Exception("Unknown type '$type'"); } diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 4800962..7d3435b 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -616,9 +616,15 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { return err } } else { - if err := {{ fname }}.ReadFields(reader); err != nil { + size, err := reader.ContainerSize() + if err != nil { return err } + if size > 0 { + if err := {{ fname }}.ReadFields(reader); err != nil { + return err + } + } if err := reader.EndContainer(); err != nil { return err }