metagen_go/tpl/macro.twig

217 lines
5.0 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(meta, u.object) }}
2022-12-08 11:40:55 +03:00
{%- 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(meta, o) %}
{%- if o.parent and has_token(o, 'POD') -%}
{{Error("@POD structs can't have a parent: " ~ o.name)}}
{%- endif -%}
{%- if o.parent and has_token(o, 'bitfields') -%}
{{Error("@bitfields structs can't have a parent: " ~ o.name)}}
{%- endif ~%}
//==============================
type {{o.name}} struct {
{% if o.parent %}
{{o.parent.name}}
{% endif %}
{{_self.decl_struct_fields(o)}}
}
var _{{o.name}}_class_props map[string]string = {{_self.props_map(o.tokens)}}
var _{{o.name}}_class_fields []string = {{_self.struct_fields_names(meta, o)}}
var _{{o.name}}_fields_props meta.ClassFieldsProps = {{_self.struct_fields_props(meta, o)}}
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 {{o.classid}}
}
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()
{{_self.struct_read_fields(o)}}
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 props_map(tokens) %}
map[string]string{
{% for k,v in tokens %}
"{{k}}" : "{{v|replace({'"' : '\\"'})}}",
{%- endfor ~%}
}
{% endmacro %}
{% macro struct_fields_names(meta, o) %}
[]string{
{% for f in get_all_fields(meta, o) ~%}
"{{f.name}}",
{%- endfor ~%}
}
{% endmacro %}
{% macro struct_fields_props(meta, o) %}
map[string]map[string]string{
{% for f in get_all_fields(meta, o) ~%}
"{{f.name|ucfirst}}" : {{_self.props_map(f.tokens)}},
{%- endfor ~%}
}
{% 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 ~%}
{%if has_token(o, 'bitfields') %}
fieldsMask meta.FieldsMask //@bitfields support
{%- endif ~%}
{% endmacro %}
{% macro struct_read_fields(o) %}
{%if has_token(o, 'bitfields') %}
use_mask, mask, err := reader.TryReadMask()
if err != nil {
return err
}
self.fieldsMask = mask
{%- endif ~%}
_cont_size, err := reader.GetContainerSize()
if err != nil {
return err
}
if _cont_size < {{o.fields|length - count_optional(o.fields)}} {
_cont_size = {{o.fields|length - count_optional(o.fields)}}
}
{%- if o.parent ~%}
if err := self.{{o.parent.name}}.ReadFields(reader); err != nil { return err }
{%- endif -%}
{% 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 %}