From 130fc2a533d31e86f22e73133d603b2944be0563 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Fri, 9 Dec 2022 11:22:17 +0300 Subject: [PATCH] Fixing codegen syntax errors --- src/codegen.inc.php | 6 +++--- tpl/macro.twig | 26 ++++++++++++-------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 17f0ac1..4b7527c 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -88,11 +88,11 @@ function _add_twig_support(\Twig\Environment $twig) } )); $twig->addFunction(new \Twig\TwigFunction('count_optional', - function($units) + function($os) { $opts = 0; - foreach($units as $u) - if($u->hasToken('optional')) + foreach($os as $o) + if($o->hasToken('optional')) ++$opts; return $opts; } diff --git a/tpl/macro.twig b/tpl/macro.twig index 1130e30..3dca7f4 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -23,7 +23,6 @@ type {{o.name}} struct { {% if o.parent %} {{o.parent.name}} {% endif %} - {{_self.decl_struct_fields(o)}} } @@ -120,46 +119,45 @@ func (self *{{o.name}}) WriteFields(writer meta.Writer) error { {% endmacro %} -{% macro props_map(tokens) %} +{%- macro props_map(tokens) -%} map[string]string{ {%- for k,v in tokens -%} "{{k}}" : "{{v|replace({'"' : '\\"'})}}", {%- endfor -%} } -{% endmacro %} +{%- endmacro -%} -{% macro struct_fields_names(o) %} +{%- macro struct_fields_names(o) -%} []string{ {%- for f in get_all_fields(o) -%} "{{f.name}}", {%- endfor -%} } -{% endmacro %} +{%- endmacro -%} -{% macro struct_fields_props(o) %} +{%- macro struct_fields_props(o) -%} map[string]map[string]string{ {%- for f in get_all_fields(o) -%} "{{f.name|ucfirst}}" : {{_self.props_map(f.tokens)}}, {%- endfor -%} } -{% endmacro %} +{%- endmacro -%} -{% macro decl_struct_fields(o) %} +{%- macro decl_struct_fields(o) -%} {%- for f in o.fields ~%} {{f.name|ucfirst}} {{f.type|go_type(f.tokens)}} {{_self.field_annotations(o, f)}} {%- endfor ~%} - {%if has_token(o, 'bitfields') %} fieldsMask meta.FieldsMask //@bitfields support {%- endif ~%} -{% endmacro %} +{%- endmacro -%} -{% macro field_annotations(o, f) %} +{%- macro field_annotations(o, f) -%} {%- if f.name|first != '_' -%} `json:"{{f.name}}" {% if has_token(o, 'table') -%} db:"{{f.name}}"{%- endif -%}` {%- endif -%} -{% endmacro %} +{%- endmacro -%} {% macro struct_reset_fields(o) %} @@ -171,7 +169,7 @@ fieldsMask meta.FieldsMask //@bitfields support {{field_reset(f.name|ucfirst, f.type, f.tokens)}} {%- endfor ~%} -{%if has_token(o, 'bitfields') %} +{% if has_token(o, 'bitfields') ~%} self.fieldsMask = meta.FieldsMask{} {%- endif ~%} @@ -179,7 +177,7 @@ self.fieldsMask = meta.FieldsMask{} {% macro struct_read_fields(o) %} -{%if has_token(o, 'bitfields') %} +{%- if has_token(o, 'bitfields') ~%} use_mask, mask, err := reader.TryReadMask() if err != nil { return err