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 }