From dddf761b74bd5403864025f2399f2f75918b16da Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Thu, 9 Nov 2023 13:01:04 +0300 Subject: [PATCH] json tag generation changed --- src/codegen.inc.php | 22 ++++++++++++++++------ tpl/macros_struct.twig | 4 ++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index cc1ae7c..9890345 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -150,12 +150,7 @@ function add_twig_filters(\Twig\Environment $twig) )); $twig->addFilter(new TwigFilter( 'alias', - function(\mtgMetaField $field): string { - if ($field->hasToken('alias')) { - return $field->getToken('alias'); - } - return $field->getName(); - } + fn(\mtgMetaField $field): string => get_field_alias_or_name($field) )); $twig->addFilter(new TwigFilter( 'fname', @@ -314,6 +309,21 @@ function add_twig_filters(\Twig\Environment $twig) return $fields; } )); + $twig->addFilter(new TwigFilter( + 'json_tag', + function(\mtgMetaField $field): string { + $tag = get_field_alias_or_name($field); + return $tag.',string'; + } + )); +} + +function get_field_alias_or_name(\mtgMetaField $field): string +{ + if ($field->hasToken('alias')) { + return $field->getToken('alias'); + } + return $field->getName(); } function add_twig_functions(\Twig\Environment $twig) diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index d0429ae..4f98285 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -8,7 +8,7 @@ type {{ s.name }} struct { {% endif %} {% for f in raw_fields %} - {{ f|fname }} {{ f.type|go_type(f.tokens) }} `json:"{{ f|alias }}" msgpack:"{{ f|alias }}"` + {{ f|fname }} {{ f.type|go_type(f.tokens) }} `json:"{{ f|json_tag }}" msgpack:"{{ f|alias }}"` {% endfor %} {% if json_fields|length > 0 %} @@ -44,7 +44,7 @@ type {{ s.name }} struct { {% if json_fields|length > 0 %} type {{ json_fields_type }} struct{ {%~ for f in json_fields %} - {{ f|fname }} {{ f.type|go_type }} `json:"{{ f|alias }}" msgpack:"{{ f|alias }}"` + {{ f|fname }} {{ f.type|go_type }} `json:"{{ f|json_tag }}" msgpack:"{{ f|alias }}"` {%~ endfor %} }