metagen_go/tpl/macro.twig

157 lines
3.7 KiB
Twig
Raw Normal View History

2022-12-08 11:40:55 +03:00
{% 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) %}
//==============================
type {{o.name}} struct {
{% if o.parent %}
{{o.parent.name}}
{% endif %}
{{_self.decl_struct_fields(o)}}
}
var _{{o.name}}_class_props map[string]string
var _{{o.name}}_class_fields []string = %fields_names%
var _{{o.name}}_fields_props meta.ClassFieldsProps
func init() {
_{{o.name}}_class_props = %class_props%
_{{o.name}}_fields_props = %fields_props%
}
func {{o.name}}_CLASS_ID() uint32 {
return {{o.classid}}
}
type I{{o.name}} interface {
meta.IMetaStruct
Ptr{{o.name}}() *{{o.name}}
}
func (*{{o.name}}) CLASS_ID() uint32 {
return %class_id%
}
func (*{{o.name}}) CLASS_NAME() string {
return "{{o.name}}"
}
func (*{{o.name}}) CLASS_PROPS() *map[string]string {
return &_{{o.name}}_class_props
}
func (*{{o.name}}) CLASS_FIELDS() []string {
return _{{o.name}}_class_fields
}
func (*{{o.name}}) CLASS_FIELDS_PROPS() *meta.ClassFieldsProps {
return &_{{o.name}}_fields_props
}
//convenience getter
func Ptr{{o.name}}(m meta.IMetaStruct) *{{o.name}} {
p, ok := m.(I{{o.name}})
if !ok {
return nil
}
return p.Ptr{{o.name}}()
}
func (self *{{o.name}}) Ptr{{o.name}}() *{{o.name}} {
return self
}
func New{{o.name}}() *{{o.name}} {
item := new ({{o.name}})
item.Reset()
return item
}
func (self *{{o.name}}) Reset() {
%fields_reset%
}
func (self *{{o.name}}) Read(reader meta.Reader) error {
return meta.ReadStruct(reader, self, "")
}
func (self *{{o.name}}) ReadFields(reader meta.Reader) error {
self.Reset()
%read_buffer%
return nil
}
func (self *{{o.name}}) Write(writer meta.Writer) error {
return meta.WriteStruct(writer, self, "")
}
func (self *{{o.name}}) WriteFields(writer meta.Writer) error {
%write_buffer%
return nil
}
%ext_methods%
%analytics_methods%
2022-12-08 11:40:55 +03:00
{% 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 %}
2022-12-08 11:40:55 +03:00
{% macro decl_enum(o) %}
//==============================
2022-12-08 11:40:55 +03:00
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 {{o.values|first}}
2022-12-08 11:40:55 +03:00
}
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|sort ~%}
2022-12-08 11:40:55 +03:00
{{v}},
{%- endfor ~%}
{% endmacro %}
{% macro enum_values_map(o) %}
{%- for k,v in o.values ~%}
2022-12-08 11:40:55 +03:00
"{{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 %}