diff --git a/src/codegen.inc.php b/src/codegen.inc.php index e009c5f..aa398de 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -4,7 +4,7 @@ namespace metagen_go; use Exception; use Twig\{TwigTest, TwigFilter, TwigFunction}; -function get_twig(array $inc_path = []) +function get_twig(array $inc_path = []): \Twig\Environment { array_unshift($inc_path, __DIR__ . "/../tpl/"); $loader = new \Twig\Loader\FilesystemLoader($inc_path); @@ -165,6 +165,10 @@ function add_twig_filters(\Twig\Environment $twig) 'varname', fn(\mtgMetaField $field): string => lcfirst(snake2camel($field->getName())) )); + $twig->addFilter(new TwigFilter( + 'tname', + fn(\mtgMetaField $field): string => $field->getType() instanceof \mtgArrType ? $field->getType()->getValue()->getName() : $field->getType()->getName() + )); $twig->addFilter(new TwigFilter( 'builtin_type_suffix', fn (\mtgBuiltinType $type): string => builtin_type_method_suffix($type), @@ -173,6 +177,33 @@ function add_twig_filters(\Twig\Environment $twig) 'default_val', fn(\mtgMetaField $field) => default_value($field) )); + $twig->addFilter(new TwigFilter( + 'fields', + function (\mtgMetaField $field): array { + $type = $field->getType(); + if($type instanceof \mtgArrType) + { + $type = $type->getValue(); + } + return $type->getFields(); + } + )); + $twig->addFilter(new TwigFilter( + 'pk_fields', + function (\mtgMetaField $field): array { + $type = $field->getType(); + if($type instanceof \mtgArrType) + { + $type = $type->getValue(); + } + $pkey = explode(',', $type->getToken('table_pkey')); + $fields = []; + foreach ($pkey as $fieldName) { + $fields[] = $type->getField($fieldName); + } + return $fields; + } + )); $twig->addFilter(new TwigFilter( 'rpc_invert', diff --git a/tpl/macros_struct.twig b/tpl/macros_struct.twig index 252a626..d2280ed 100644 --- a/tpl/macros_struct.twig +++ b/tpl/macros_struct.twig @@ -43,7 +43,7 @@ type {{ s.name }} struct { {%~ for f in pkey %} - {{ f|fname }} {{ f.type|go_type }} `json:"{{ f|alias }}" msgpack:"{{ f|alias }}"` + {{ f|fname }} {{ f.type|go_type }} `json:"{{ f|alias }},string" msgpack:"{{ f|alias }}"` {%~ endfor %} {{ json_fields_type }} @@ -51,7 +51,7 @@ type {{ s.name }} struct { 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|alias }},string" msgpack:"{{ f|alias }}"` {%~ endfor %} changedFields meta.ChangedFields @@ -847,7 +847,7 @@ type {{ s.name }} struct { {% endif %} {% for f in s.fields %} - {{ f|fname }} {{ f.type|go_type }} `json:"{{ f|alias }}" msgpack:"{{ f|alias }}"` + {{ f|fname }} {{ f.type|go_type }} `json:"{{ f|alias }},string" msgpack:"{{ f|alias }}"` {% endfor %} {% if has_token(s, 'bitfields') %}