diff --git a/src/Luban.Job.Proto/Source/Defs/DefProto.cs b/src/Luban.Job.Proto/Source/Defs/DefProto.cs index 6c39dee..2a8812d 100644 --- a/src/Luban.Job.Proto/Source/Defs/DefProto.cs +++ b/src/Luban.Job.Proto/Source/Defs/DefProto.cs @@ -2,6 +2,7 @@ using Luban.Common.Utils; using Luban.Job.Proto.RawDefs; using System.Collections.Generic; +using System.Linq; namespace Luban.Job.Proto.Defs { @@ -23,6 +24,25 @@ namespace Luban.Job.Proto.Defs public List Fields { get; set; } = new List(); +#if !LUBAN_LITE + public virtual string GoBinImport + { + get + { + var imports = new HashSet(); + if (this.Fields.Count > 0) + { + imports.Add("errors"); + } + foreach (var f in Fields) + { + f.CType.Apply(Luban.Job.Common.TypeVisitors.GoBinImport.Ins, imports); + } + return string.Join('\n', imports.Select(im => $"import \"{im}\"")); + } + } +#endif + public override void Compile() { var pass = Assembly; diff --git a/src/Luban.Server/Templates/proto/go/proto.tpl b/src/Luban.Server/Templates/proto/go/proto.tpl index ce7c070..23bd376 100644 --- a/src/Luban.Server/Templates/proto/go/proto.tpl +++ b/src/Luban.Server/Templates/proto/go/proto.tpl @@ -2,7 +2,7 @@ go_full_name = x.go_full_name parent_def_type = x.parent_def_type is_abstract_type = x.is_abstract_type - hierarchy_fields = x.hierarchy_fields + fields = x.fields hierarchy_not_abstract_children = x.hierarchy_not_abstract_children -}} @@ -15,59 +15,26 @@ import ( {{x.go_bin_import}} type {{go_full_name}} struct { - {{~for field in hierarchy_fields ~}} + {{~for field in fields ~}} {{field.convention_name}} {{go_define_type field.ctype}} {{~end~}} } const TypeId_{{go_full_name}} = {{x.id}} -func ({{go_full_name}}) GetTypeId() int32 { +func (*{{go_full_name}}) GetTypeId() int32 { return {{x.id}} } -func (_v {{go_full_name}})Serialize(_buf *serialization.ByteBuf) { - {{~for field in hierarchy_fields ~}} +func (_v *{{go_full_name}})Serialize(_buf *serialization.ByteBuf) { + {{~for field in fields ~}} {{go_serialize_field field.ctype ("_v." + field.convention_name) '_buf'}} {{~end~}} } -func (_v {{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) { - {{~for field in hierarchy_fields ~}} +func (_v *{{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) { + {{~for field in fields ~}} {{go_deserialize_field field.ctype ("_v." + field.convention_name) '_buf' 'err'}} {{~end~}} return } - -{{~if is_abstract_type~}} -func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { - _buf.WriteInt(_v.GetTypeId()) - _v.Serialize(_buf) -} - -func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (_v serialization.ISerializable, err error) { - var id int32 - if id, err = _buf.ReadInt() ; err != nil { - return - } - switch id { - {{~for child in hierarchy_not_abstract_children~}} - case {{child.id}}: _v = {{child.go_full_name}}{}; if err = _v.Deserialize(_buf); err != nil { return nil, err } else { return } - {{~end~}} - default: return nil, errors.New("unknown type id") - } -} -{{~else~}} -func Serialize{{go_full_name}}(_v serialization.ISerializable, _buf *serialization.ByteBuf) { - _v.Serialize(_buf) -} - -func Deserialize{{go_full_name}}(_buf *serialization.ByteBuf) (*{{go_full_name}}, error) { - v := &{{go_full_name}}{} - if err := v.Deserialize(_buf); err == nil { - return v, nil - } else { - return nil, err - } -} -{{~end~}} diff --git a/src/Luban.Server/Templates/proto/go/rpc.tpl b/src/Luban.Server/Templates/proto/go/rpc.tpl index 54a2b1f..867edc1 100644 --- a/src/Luban.Server/Templates/proto/go/rpc.tpl +++ b/src/Luban.Server/Templates/proto/go/rpc.tpl @@ -1,2 +1,51 @@ +{{- + go_full_name = x.go_full_name + parent_def_type = x.parent_def_type + is_abstract_type = x.is_abstract_type + hierarchy_fields = x.hierarchy_fields + hierarchy_not_abstract_children = x.hierarchy_not_abstract_children + arg_type = x.targ_type + res_type = x.tres_type +-}} + package {{x.top_module}} -// rpc {{x.full_name}} \ No newline at end of file + +import ( + "bright/serialization" + "errors" +) + +{{x.go_bin_import}} + +type {{go_full_name}} struct { + SeqId int64 + Arg {{go_define_type arg_type}} + Res {{go_define_type res_type}} +} + +const TypeId_{{go_full_name}} = {{x.id}} + +func (*{{go_full_name}}) GetTypeId() int32 { + return {{x.id}} +} + +func (_v *{{go_full_name}})Serialize(_buf *serialization.ByteBuf) { + _buf.WriteLong(_v.SeqId) + if _v.SeqId & 0x1 == 0 { + {{go_serialize_field arg_type "_v.Arg" '_buf'}} + } else { + {{go_serialize_field res_type "_v.Res" '_buf'}} + } +} + +func (_v *{{go_full_name}})Deserialize(_buf *serialization.ByteBuf) (err error) { + if _v.SeqId, err = _buf.ReadLong() ; err != nil { + return + } + if _v.SeqId & 0x1 == 0 { + {{go_deserialize_field arg_type "_v.Arg" '_buf' 'err'}} + } else { + {{go_deserialize_field res_type "_v.Res" '_buf' 'err'}} + } + return +} diff --git a/src/Luban.Server/Templates/proto/go/stub.tpl b/src/Luban.Server/Templates/proto/go/stub.tpl index 5faea50..05c3930 100644 --- a/src/Luban.Server/Templates/proto/go/stub.tpl +++ b/src/Luban.Server/Templates/proto/go/stub.tpl @@ -1 +1,17 @@ package {{namespace}} + +import "bright/net" + +type ProtocolFactory = func () net.Protocol + +var ProtocolStub map[int]ProtocolFactory + +func init() { + ProtocolStub = make(map[int]ProtocolFactory) + {{~for p in protos~}} + ProtocolStub[{{p.id}}] = func () net.Protocol { return &{{p.go_full_name}}{} } + {{~end~}} + {{~for r in rpcs~}} + ProtocolStub[{{r.id}}] = func () net.Protocol { return &{{r.go_full_name}}{} } + {{~end~}} +}