diff --git a/src/codegen.inc.php b/src/codegen.inc.php index ad34706..2e5e440 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -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'"); +} + diff --git a/tpl/macro.twig b/tpl/macro.twig index 0c2ac66..125a4ab 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -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) %} //==============================