metagen_go/tpl/macro.twig

467 lines
9.8 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') ~%}
2022-12-08 19:21:43 +03:00
{{ _self.decl_struct(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') ~%}
2022-12-08 19:21:43 +03:00
{{ _self.decl_rpc(u.object) }}
2022-12-08 11:40:55 +03:00
{%- endif ~%}
{%- endfor ~%}
{% endmacro %}
2022-12-08 19:21:43 +03:00
{% macro decl_struct(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)}}
2022-12-08 19:21:43 +03:00
var _{{o.name}}_class_fields []string = {{_self.struct_fields_names(o)}}
var _{{o.name}}_fields_props meta.ClassFieldsProps = {{_self.struct_fields_props(o)}}
func {{o.name}}_CLASS_ID() uint32 {
return {{o.classid}}
}
2022-12-08 17:45:28 +03:00
type I{{o.name}} interface {
meta.IMetaStruct
Ptr{{o.name}}() *{{o.name}}
}
2022-12-08 17:45:28 +03:00
func (*{{o.name}}) CLASS_ID() uint32 {
return {{o.classid}}
}
2022-12-08 17:45:28 +03:00
func (*{{o.name}}) CLASS_NAME() string {
return "{{o.name}}"
}
2022-12-08 17:45:28 +03:00
func (*{{o.name}}) CLASS_PROPS() *map[string]string {
return &_{{o.name}}_class_props
}
2022-12-08 17:45:28 +03:00
func (*{{o.name}}) CLASS_FIELDS() []string {
return _{{o.name}}_class_fields
}
2022-12-08 17:45:28 +03:00
func (*{{o.name}}) CLASS_FIELDS_PROPS() *meta.ClassFieldsProps {
return &_{{o.name}}_fields_props
}
2022-12-08 17:45:28 +03:00
//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}}()
}
2022-12-08 17:45:28 +03:00
func (self *{{o.name}}) Ptr{{o.name}}() *{{o.name}} {
return self
}
2022-12-08 17:45:28 +03:00
func New{{o.name}}() *{{o.name}} {
item := new ({{o.name}})
item.Reset()
return item
}
2022-12-08 17:45:28 +03:00
func (self *{{o.name}}) Reset() {
{{_self.struct_reset_fields(o)}}
}
2022-12-08 17:45:28 +03:00
func (self *{{o.name}}) Read(reader meta.Reader) error {
return meta.ReadStruct(reader, self, "")
}
2022-12-08 17:45:28 +03:00
func (self *{{o.name}}) ReadFields(reader meta.Reader) error {
self.Reset()
{{_self.struct_read_fields(o)}}
return nil
}
2022-12-08 17:45:28 +03:00
func (self *{{o.name}}) Write(writer meta.Writer) error {
return meta.WriteStruct(writer, self, "")
}
2022-12-08 17:45:28 +03:00
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 %}
2022-12-08 16:51:08 +03:00
{% if has_token(o, 'statist') %}
2022-12-08 19:21:43 +03:00
{{_self.stats_methods(o)}}
2022-12-08 16:51:08 +03:00
{% endif %}
{% if has_token(o, 'POD') and has_token(o, 'id') and has_token(o, 'table') and has_token(o, 'owner') %}
2022-12-08 19:21:43 +03:00
{{_self.db_item_methods(o)}}
{% endif %}
2022-12-08 11:40:55 +03:00
{% endmacro %}
{% macro props_map(tokens) %}
map[string]string{
2022-12-08 19:45:55 +03:00
{%- for k,v in tokens -%}
"{{k}}" : "{{v|replace({'"' : '\\"'})}}",
2022-12-08 19:45:55 +03:00
{%- endfor -%}
}
{% endmacro %}
2022-12-08 19:21:43 +03:00
{% macro struct_fields_names(o) %}
[]string{
2022-12-08 19:45:55 +03:00
{%- for f in get_all_fields(o) -%}
"{{f.name}}",
2022-12-08 19:45:55 +03:00
{%- endfor -%}
}
{% endmacro %}
2022-12-08 19:21:43 +03:00
{% macro struct_fields_props(o) %}
map[string]map[string]string{
2022-12-08 19:45:55 +03:00
{%- for f in get_all_fields(o) -%}
"{{f.name|ucfirst}}" : {{_self.props_map(f.tokens)}},
2022-12-08 19:45:55 +03:00
{%- endfor -%}
}
{% endmacro %}
{% macro decl_struct_fields(o) %}
{%- for f in o.fields ~%}
2022-12-08 18:06:25 +03:00
{{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 %}
2022-12-08 18:06:25 +03:00
{% 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
}
2022-12-08 19:45:55 +03:00
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 %}
2022-12-08 19:21:43 +03:00
{% macro stats_methods(o) %}
2022-12-08 16:51:08 +03:00
func (self *{{o.name}}) Table() string {
return "{{token(o, 'statist')}}"
}
func (self * {{o.name}}) Columns() []string {
return []string{
2022-12-08 19:21:43 +03:00
{%- for f in get_all_fields(o) ~%}
2022-12-08 16:51:08 +03:00
{%- if not has_token(f, 'statist_skip') ~%}
"{{token_or(f, 'statist_alias', f.name)}}",
{%- endif ~%}
{%- endfor ~%}
}
}
func (self *{{o.name}}) Values() []interface{} {
return []interface{}{
2022-12-08 19:21:43 +03:00
{%- for f in get_all_fields(o) ~%}
2022-12-08 16:51:08 +03:00
{%- if not has_token(f, 'statist_skip') ~%}
self.{{f.name|ucfirst}},
{%- endif ~%}
{%- endfor ~%}
}
}
{% endmacro %}
2022-12-08 19:21:43 +03:00
{% macro db_item_methods(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}})
2022-12-08 19:21:43 +03:00
{%- for f in get_all_fields(o) ~%}
self.{{f.name|ucfirst}} = row.{{f.name|ucfirst}}
{%- endfor ~%}
break
}
default:
break
}
}
func (self *{{o.name}}) Export(data []interface{}) {
2022-12-08 19:21:43 +03:00
{%- for f in get_all_fields(o) ~%}
data[{{loop.index0}}] = self.{{f.name|ucfirst}}
{%- 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) %}
2022-12-08 19:45:55 +03:00
{%- for v in o.values|sort -%}
2022-12-08 11:40:55 +03:00
{{v}},
{%- endfor ~%}
{% endmacro %}
{% macro enum_values_map(o) %}
2022-12-08 19:45:55 +03:00
{%- 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 %}
2022-12-08 19:21:43 +03:00
{% macro decl_rpc(o) %}
2022-12-08 17:45:28 +03:00
2022-12-08 19:21:43 +03:00
{{_self._decl_rpc_part(o.req, o.code)}}
{{_self._decl_rpc_part(o.rsp, o.code)}}
2022-12-08 17:45:28 +03:00
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 %}
2022-12-08 19:21:43 +03:00
{% macro _decl_rpc_part(o, code) %}
2022-12-08 17:45:28 +03:00
2022-12-08 19:21:43 +03:00
{{_self.decl_struct(o)}}
2022-12-08 17:45:28 +03:00
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}}
}
2022-12-08 11:40:55 +03:00
{% endmacro %}
2022-12-08 16:51:08 +03:00