{% macro decl_units(meta) %} {%- for u in meta.getunits ~%} {%- if u.object is instanceof('\\mtgMetaStruct') ~%} {{ _self.decl_struct(meta, u.object) }} {%- elseif u.object is instanceof('\\mtgMetaEnum') ~%} {{ _self.decl_enum(u.object) }} {%- elseif u.object is instanceof('\\mtgMetaRPC') ~%} {{ _self.decl_rpc(meta, 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() { {{_self.struct_reset_fields(o)}} } 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 { {{_self.struct_write_fields(o)}} return nil } {% if has_token(o, 'bitfields') %} {{_self.bitfields_methods(o)}} {% endif %} {% if has_token(o, 'statist') %} {{_self.stats_methods(meta, o)}} {% endif %} {% if has_token(o, 'POD') and has_token(o, 'id') and has_token(o, 'table') and has_token(o, 'owner') %} {{_self.db_item_methods(meta, o)}} {% endif %} {% 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)}} {{_self.field_annotations(o, f)}} {%- endfor ~%} {%if has_token(o, 'bitfields') %} fieldsMask meta.FieldsMask //@bitfields support {%- endif ~%} {% endmacro %} {% macro field_annotations(o, f) %} {%- if f.name|first != '_' -%} `json:"{{f.name}}" {% if has_token(o, 'table') -%} db:"{{f.name}}"{%- endif -%}` {%- endif -%} {% endmacro %} {% macro struct_reset_fields(o) %} {%- if o.parent ~%} self.{{o.parent.name}}.Reset() {%- endif ~%} {%- for f in o.fields ~%} {{field_reset(f.name|ucfirst, f.type, f.tokens)}} {%- endfor ~%} {%if has_token(o, 'bitfields') %} self.fieldsMask = meta.FieldsMask{} {%- 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 ~%} {%- for f in o.fields ~%} if _cont_size <= 0 { return nil } {%if has_token(o, 'bitfields') ~%} if !use_mask { {%- endif ~%} _cont_size-- {%if has_token(o, 'bitfields') ~%} } if !use_mask || (use_mask && self.HasValue({{loop.index0}})) { {%- endif ~%} {{buf2var(f.name, 'self.' ~ f.name|ucfirst, f.type, 'reader', f.tokens, false)}} {%if has_token(o, 'bitfields') ~%} } {%- endif -%} {%- endfor -%} {% endmacro %} {% macro struct_write_fields(o) %} {%- if o.parent ~%} if err := self.{{o.parent.name}}.WriteFields(writer); err != nil { return err } {%- endif ~%} {%- for f in o.fields ~%} {{var2buf(f.name, 'self.' ~ f.name|ucfirst, f.type, "writer", f.tokens, has_token(f, 'virtual'))}} {%- endfor -%} {% endmacro %} {% macro bitfields_methods(o) %} func(self *{{o.name}}) HasValue(index uint64) bool { return self.fieldsMask.FieldChanged(index) } func(self *{{o.name}}) SetFieldChanged(index uint64) { self.fieldsMask.SetFieldChanged(index) } func(self *{{o.name}}) IsMaskFilled() bool { return self.fieldsMask.IsFilled() } func(self *{{o.name}}) GetMask() meta.FieldsMask { return self.fieldsMask } {% endmacro %} {% macro stats_methods(meta, o) %} func (self *{{o.name}}) Table() string { return "{{token(o, 'statist')}}" } func (self * {{o.name}}) Columns() []string { return []string{ {%- for f in get_all_fields(meta, o) ~%} {%- if not has_token(f, 'statist_skip') ~%} "{{token_or(f, 'statist_alias', f.name)}}", {%- endif ~%} {%- endfor ~%} } } func (self *{{o.name}}) Values() []interface{} { return []interface{}{ {%- for f in get_all_fields(meta, o) ~%} {%- if not has_token(f, 'statist_skip') ~%} self.{{f.name|ucfirst}}, {%- endif ~%} {%- endfor ~%} } } {% endmacro %} {% macro db_item_methods(meta, o) %} func (self *{{o.name}}) NewInstance() meta.IMetaDataItem { return New{{o.name}}() } func (self *{{o.name}}) GetDbTableName() string { return "{{token(o, 'table')}}" } func (self *{{o.name}}) GetDbFields() []string { return self.CLASS_FIELDS() } func (self *{{o.name}}) GetOwnerFieldName() string { return "{{token(o, 'owner')}}" } func (self *{{o.name}}) GetIdFieldName() string { return "{{token(o, 'id')}}" } func (self *{{o.name}}) GetIdValue() uint64 { return uint64(self.{{token(o, 'id')|ucfirst}}) } func (self *{{o.name}}) Import(data interface{}) { switch data.(type) { case {{o.name}}: { row := data.({{o.name}}) {%- for f in get_all_fields(meta, o) ~%} self.{{f.name|ucfirst}} = row.{{f.name|ucfirst}} {%- endfor ~%} break } default: break } } func (self *{{o.name}}) Export(data []interface{}) { {%- for f in get_all_fields(meta, o) ~%} data[{{loop.index0}}] = self.{{f.name|ucfirst}} {%- endfor ~%} } {% 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 {{o.values|first}} } 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 ~%} {{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(meta, o) %} {{_self._decl_rpc_part(meta, o.req, o.code)}} {{_self._decl_rpc_part(meta, o.rsp, o.code)}} func New{{o.name}}() *{{o.name}} { rpc := {{o.name}}{} rpc.Req = New{{o.req.name}}() rpc.Rsp = New{{o.rsp.name}}() return &rpc } type {{o.name}} struct { ErrCode int32 ErrMsg string Req *{{o.req.name}} Rsp *{{o.rsp.name}} } func (rpc *{{o.name}}) SetError(code int32, msg string) { rpc.ErrCode = code rpc.ErrMsg = msg } func (rpc *{{o.name}}) GetError() (int32, string) { return rpc.ErrCode, rpc.ErrMsg } func (rpc *{{o.name}}) GetRequest() meta.IMetaStruct { return rpc.Req } func (rpc *{{o.name}}) GetResponse() meta.IMetaStruct { return rpc.Rsp } func (rpc *{{o.name}}) GetName() string { return "{{o.name}}" } func (rpc *{{o.name}}) GetCode() int32 { return {{o.code}} } type IRPCHandler_{{o.name}} interface { {{o.name}}(*{{o.name}}) error } func (rpc *{{o.name}}) Execute(h interface{}) error{ mh, ok := h.(IRPCHandler_{{o.name}}) if !ok { return errors.New("RPC '{{o.name}}(*{{o.name}}) error' is not implemented") } return mh.{{o.name}}(rpc) } {% endmacro %} {% macro _decl_rpc_part(meta, o, code) %} {{_self.decl_struct(meta, o)}} var {{o.name}}_ func(interface{}, *{{o.name}}, *{{o.name|rpc_invert}}) error func (self *{{o.name}}) I{{o.name}}() *{{o.name}} { return self } func (*{{o.name}}) GetCode() int32 { return {{code}} } {% endmacro %}