Gradually adding support for all features

This commit is contained in:
Pavel Shevaev 2022-12-08 13:29:16 +03:00
parent af0e8119c1
commit 34eb73a20e
2 changed files with 63 additions and 1 deletions

View File

@ -80,4 +80,59 @@ function _add_twig_support(\Twig\Environment $twig)
return $v;
}
));
$twig->addFilter(new \Twig\TwigFilter('go_type',
function($type, $tokens)
{
return go_type($type, $tokens);
}
));
$twig->addFilter(new \Twig\TwigFilter('ucfirst',
function($str)
{
return ucfirst($str);
}
));
}
function go_type(\mtgType $type, array $tokens = array())
{
if($type instanceof \mtgArrType)
{
$vtype = $type->getValue();
$native = go_type($vtype);
$str = "[]";
if(array_key_exists("virtual", $tokens))
$str .= "I";
else
$str .= $vtype instanceof mtgMetaStruct ? "*" : "";
$str .= $native;
return $str;
}
else if($type instanceof \mtgBuiltinType)
{
if($type->isFloat())
return "float32";
else if($type->isDouble())
return "float64";
else if($type->isBlob())
return "[]byte";
else
return $type->getName();
}
else if($type instanceof \mtgMetaEnum)
return $type->getName();
else if($type instanceof \mtgMetaStruct)
{
if(array_key_exists("virtual", $tokens))
return "I{$type->getName()}";
else
return $type->getName();
}
else
throw new Exception("Unknown type '$type'");
}

View File

@ -17,7 +17,8 @@ type {{o.name}} struct {
{% if o.parent %}
{{o.parent.name}}
{% endif %}
%fields%
{{_self.decl_struct_fields(o)}}
}
var _{{o.name}}_class_props map[string]string
var _{{o.name}}_class_fields []string = %fields_names%
@ -87,6 +88,12 @@ func (self *{{o.name}}) WriteFields(writer meta.Writer) error {
{% endmacro %}
{% macro decl_struct_fields(o) %}
{%- for f in o.fields ~%}
{{f.name|ucfirst}} {{f.type|go_type(f.tokens)}} {{f.name|first != '_' ? '`json:"' ~ f.name ~ '"`'}}
{%- endfor ~%}
{% endmacro %}
{% macro decl_enum(o) %}
//==============================