metagen_go/tpl/macro.twig

75 lines
1.7 KiB
Twig

{% macro decl_units(meta) %}
{%- for u in meta.getunits ~%}
{%- if u.object is instanceof('\\mtgMetaStruct') ~%}
{{ _self.decl_struct(u.object) }}
{%- elseif u.object is instanceof('\\mtgMetaEnum') ~%}
{{ _self.decl_enum(u.object) }}
{%- elseif u.object is instanceof('\\mtgMetaRPC') ~%}
{{ _self.decl_rpc(u.object) }}
{%- endif ~%}
{%- endfor ~%}
{% endmacro %}
{% macro decl_struct(o) %}
{% endmacro %}
{% macro decl_enum(o) %}
const (
{{_self.enum_consts(o)}}
)
type {{o.name}} int32
var _{{o.name}}_values []int = []int{ {{_self.enum_values_list(o)}} }
var _{{o.name}}_map map[string]{{o.name}}
func init() {
_{{o.name}}_map = map[string]{{o.name}}{ {{_self.enum_values_map(o)}} }
}
func (*{{o.name}}) CLASS_ID() uint32 {
return {{o.classid}}
}
func (*{{o.name}}) CLASS_NAME() string {
return "{{o.name}}"
}
func (*{{o.name}}) DEFAULT_VALUE() int32 {
return %default_enum_value%
}
func (self *{{o.name}}) IsValid() bool {
return sort.SearchInts(_{{o.name}}_values, int(*self)) != -1
}
func {{o.name}}_GetNameByValue(value int) string {
for name, num := range _{{o.name}}_map {
if value == int(num) {
return name
}
}
return ""
}
func New{{o.name}}ByName(name string) ({{o.name}}, error) {
if v, ok := _{{o.name}}_map[name]; ok == true {
return v, nil
}
return 0, errors.Errorf("Wrong name of {{o.name}}: '%s'", name)
}
{% endmacro %}
{% macro enum_values_list(o) %}
{%- for v in o.values -%}
{{v}},
{%- endfor ~%}
{% endmacro %}
{% macro enum_values_map(o) %}
{%- for k,v in o.values -%}
"{{k}}" : {{v}},
{%- endfor ~%}
{% endmacro %}
{% macro enum_consts(o) %}
{%- for k,v in o.values ~%}
{{o.name}}_{{k}} {{o.name}} = {{v}}
{%- endfor ~%}
{% endmacro %}
{% macro decl_rpc(o) %}
{% endmacro %}