diff --git a/src/codegen.inc.php b/src/codegen.inc.php index bb2a0be..ac39433 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -47,7 +47,6 @@ function supported_tokens() 'alias', 'default', - 'optional', 'virtual', 'statist', @@ -218,16 +217,6 @@ function addTwigFunctions(\Twig\Environment $twig) 'get_all_fields', fn(mtgMetaStruct $s): array => \mtg_get_all_fields($s), )); - $twig->addFunction(new TwigFunction( - 'count_optional', - function($os) { - $opts = 0; - foreach($os as $o) - if($o->hasToken('optional')) - ++$opts; - return $opts; - } - )); $twig->addFunction(new TwigFunction( 'arr_fill', fn($value, int $count): array => array_fill(0, $count, $value) @@ -520,8 +509,6 @@ function buf2var($name, $fname, mtgType $type, $buf, array $tokens = array(), $i $offset = "\n "; $str .= "/*[]{$name}*/"; $str .= $offset . _read_op($tokens, "{$buf}.BeginContainer(\"$name\")"); - //we don't want to propagate 'optionalness' below - unset($tokens['optional']); $str .= $offset . "_{$name}_size, err := {$buf}.GetContainerSize()"; $str .= $offset . "if err != nil { return err }"; @@ -585,7 +572,7 @@ function _write_op($op) function _read_op(array $tokens, $op) { - return "if err := $op; err != nil { return " . (array_key_exists("optional", $tokens) ? "/*optional*/nil" : "err"). " }"; + return "if err := $op; err != nil { return err }"; } function rpc_invert($name) diff --git a/tpl/macro.twig b/tpl/macro.twig index 3dca7f4..c7da73b 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -190,8 +190,8 @@ self.fieldsMask = meta.FieldsMask{} return err } - if _cont_size < {{(o.fields|length) - count_optional(o.fields)}} { - _cont_size = {{(o.fields|length) - count_optional(o.fields)}} + if _cont_size < {{(o.fields|length)}} { + _cont_size = {{(o.fields|length)}} } {%- if o.parent ~%} diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 5926716..48eb1c1 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -598,7 +598,6 @@ func (s *{{ name|default(o.name) }}) Reset() { {% macro meta_read(s, name) %} {% set fields_len = s.fields|length %} -{% set opt_fields_len = count_optional(s.fields) %} func (s *{{ name|default(s.name) }}) Read(reader meta.Reader) error { if err := reader.BeginContainer(""); err != nil { @@ -626,8 +625,8 @@ func (s *{{ name|default(s.name) }}) ReadFields(reader meta.Reader) error { return err } - if contSize < {{ fields_len - opt_fields_len }} { - contSize = {{ fields_len - opt_fields_len }} + if contSize < {{ fields_len }} { + contSize = {{ fields_len }} } {% if s.parent %} @@ -1069,4 +1068,4 @@ func (s *{{ name|default(s.name) }}) readFieldsAssociative(reader meta.Reader) e return nil } -{% endmacro meta_read_assoc %} \ No newline at end of file +{% endmacro meta_read_assoc %}