Skip to content

Commit

Permalink
调整go api结构
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Feb 18, 2021
1 parent 711940a commit d480aeb
Show file tree
Hide file tree
Showing 30 changed files with 197 additions and 81 deletions.
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct AddressBook {

# 功能

## 输出ProtoPlus编码的Go源码
## 输出ProtoPlus编码消息序列化的Go源码

命令行示例:
```bash
Expand All @@ -107,11 +107,29 @@ protoplus --ppgo_out=msg_gen.go --package=proto proto1.proto proto2.proto

指定输出时的Go包名

* codec

## 输出消息绑定的Go源码

输出源码被引用时, 自动注册到cellnet的消息Registry中

命令行示例:
```bash
protoplus --ppgoreg_out=msg_gen.go --package=proto proto1.proto proto2.proto
```

参数说明:
* ppgoreg_out

Go源码文件名

* package

指定输出时的Go包名

* codec
生成消息注册的默认编码,如在消息中指定编码时,优先使用指定的编码


## 输出ProtoPlus编码的C#源码
## 输出ProtoPlus编码的消息序列化C#源码

输出的C#源码, 需要配合[ProtoPlus C# SDK](https://github.com/davyxu/protoplus/tree/master/api/csharp/ProtoPlus) 使用

Expand All @@ -133,6 +151,26 @@ protoplus --ppcs_out=MsgGen.cs --package=Proto proto1.proto proto2.proto

C#代码生成时,消息类默认基类名称, 默认基类为IProtoStruct


## 输出消息绑定的C#源码

输出的C#源码, 需要配合[ProtoPlus C# SDK](https://github.com/davyxu/protoplus/tree/master/api/csharp/ProtoPlus) 使用

命令行示例:
```bash
protoplus --ppcsreg_out=MsgGen.cs --package=Proto proto1.proto proto2.proto
```

参数说明:
* ppcsreg_out

C#源码文件名

* package

指定输出时的C#命名空间


## 输出Protobuf协议描述文件

输出的Protobuf协议描述文件,可使用protoc编译器编译
Expand Down
4 changes: 2 additions & 2 deletions proto/api.go → api/golang/api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package proto
package ppgo

import (
"github.com/davyxu/protoplus/wire"
"github.com/davyxu/protoplus/api/golang/wire"
"github.com/davyxu/ulexer"
)

Expand Down
2 changes: 1 addition & 1 deletion proto/text_marshaler.go → api/golang/text_marshaler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package proto
package ppgo

import (
"bufio"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package proto
package ppgo

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion proto/text_parser.go → api/golang/text_parser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package proto
package ppgo

import (
"github.com/davyxu/ulexer"
Expand Down
2 changes: 1 addition & 1 deletion proto/text_writer.go → api/golang/text_writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package proto
package ppgo

import (
"bytes"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions cmd/protoplus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type GenEntry struct {
var (
genEntryList = []*GenEntry{
{name: "ppgo_out", usage: "output protoplus message serialize golang source file", outfile: ppgo.GenGo},
{name: "ppgoreg_out", usage: "output protoplus message register entry", outfile: ppgo.GenGoReg},
{name: "ppcs_out", usage: "output protoplus message serialize csharp source file", outfile: ppcs.GenCSharp},
{name: "ppgoreg_out", usage: "output protoplus message register entry in golang", outfile: ppgo.GenGoReg},
{name: "ppcs_out", usage: "output protoplus message serialize csharp source file", outfile: ppcs.GenCS},
{name: "ppcsreg_out", usage: "output protoplus message register entry in csharp", outfile: ppcs.GenCSReg},
{name: "pbscheme_out", usage: "output google protobuf schema file", outfile: pbscheme.GenProto},
{name: "ppscheme_out", usage: "output protoplus scheme json file", outfile: ppscheme.GenJson},
{name: "route_out", usage: "output route table json file", outfile: route.GenJson},
Expand Down
2 changes: 0 additions & 2 deletions example/csharp/Example/ProtoGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,4 @@ public bool Unmarshal(InputStream stream, int fieldNumber, WireFormat.WireType w
#endregion
}



}
52 changes: 52 additions & 0 deletions example/csharp/Example/ProtoGenReg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Generated by github.com/davyxu/protoplus
// DO NOT EDIT!
using System;
using System.Collections.Generic;
using ProtoPlus;
#pragma warning disable 162

namespace Proto
{

public static class MessageVisitor
{
public static void Visit(Action<MetaInfo> callback)
{
callback(new MetaInfo
{
Type = typeof(MyTypeMini),
ID = 0,
SourcePeer = "",
TargetPeer = "",
});
callback(new MetaInfo
{
Type = typeof(MySubType),
ID = 0,
SourcePeer = "",
TargetPeer = "",
});
callback(new MetaInfo
{
Type = typeof(MyType),
ID = 0,
SourcePeer = "",
TargetPeer = "",
});
callback(new MetaInfo
{
Type = typeof(LoginREQ),
ID = 0,
SourcePeer = "client",
TargetPeer = "game",
});
callback(new MetaInfo
{
Type = typeof(LoginACK),
ID = 0,
SourcePeer = "",
TargetPeer = "",
});
}
}
}
13 changes: 0 additions & 13 deletions example/csharp/Example/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,10 @@ static void TestFull()
Debug.Assert(myType.Equals(myType2));
}

static void TestMessage()
{
var mm = new MessageMeta();
MessageMetaRegister.RegisterGeneratedMeta(mm);
var msg = mm.CreateMessageByID(28380);

var meta = mm.GetMetaByType(msg.GetType());

Debug.Assert(meta.ID == 28380);

Debug.Assert(meta.SourcePeer == "client");
}

static void Main(string[] args)
{
TestFull();
TestMessage();
TestSkipField();
}
}
Expand Down
1 change: 0 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ type Context struct {
*model.DescriptorSet
OutputFileName string
ClassBase string
RegEntry bool
}
17 changes: 16 additions & 1 deletion gen/ppcs/gen_cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/davyxu/protoplus/gen"
)

func GenCSharp(ctx *gen.Context) error {
func GenCS(ctx *gen.Context) error {

gen := codegen.NewCodeGen("cs").
RegisterTemplateFunc(codegen.UsefulFunc).
Expand All @@ -20,3 +20,18 @@ func GenCSharp(ctx *gen.Context) error {

return gen.WriteOutputFile(ctx.OutputFileName).Error()
}

func GenCSReg(ctx *gen.Context) error {

gen := codegen.NewCodeGen("csreg").
RegisterTemplateFunc(codegen.UsefulFunc).
RegisterTemplateFunc(UsefulFunc).
ParseTemplate(RegTemplateText, ctx)

if gen.Error() != nil {
fmt.Println(string(gen.Code()))
return gen.Error()
}

return gen.WriteOutputFile(ctx.OutputFileName).Error()
}
14 changes: 12 additions & 2 deletions gen/ppcs/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,19 @@ namespace {{.PackageName}}
#endregion
}
{{end}}
}
`

const RegTemplateText = `// Generated by github.com/davyxu/protoplus
// DO NOT EDIT!
using System;
using System.Collections.Generic;
using ProtoPlus;
#pragma warning disable 162
namespace {{.PackageName}}
{
{{if .RegEntry}}
public static class MessageVisitor
{
public static void Visit(Action<MetaInfo> callback)
Expand All @@ -72,6 +83,5 @@ namespace {{.PackageName}}
});{{end}}
}
}
{{end}}
}
`
5 changes: 5 additions & 0 deletions gen/ppgo/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func init() {
ret += "[]"
}

// 默认指针
if fd.Kind == model.Kind_Struct {
ret += "*"
}

ret += codegen.GoTypeName(fd)
return
}
Expand Down
39 changes: 21 additions & 18 deletions gen/ppgo/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,14 @@ const TemplateText = `// Generated by github.com/davyxu/protoplus
package {{.PackageName}}
import (
"github.com/davyxu/protoplus/proto"
"github.com/davyxu/protoplus/wire"
"unsafe" {{if .RegEntry}}
"reflect"
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/codec" {{end}}
"github.com/davyxu/protoplus/api/golang"
"github.com/davyxu/protoplus/api/golang/wire"
"unsafe"
)
var (
_ *wire.Buffer
_ = proto.Marshal
_ unsafe.Pointer {{if .RegEntry}}
_ cellnet.MessageMeta
_ codec.CodecRecycler
_ reflect.Kind {{end}}
_ = ppgo.Marshal
_ unsafe.Pointer
)
{{range $a, $enumobj := .Enums}}
Expand Down Expand Up @@ -49,18 +43,18 @@ type {{.Name}} struct{ {{range .Fields}}
{{GoFieldName .}} {{ProtoTypeName .}} {{GoStructTag .}}{{FieldTrailingComment .}} {{end}}
}
func (self *{{.Name}}) String() string { return proto.CompactTextString(self) }
func (self *{{.Name}}) String() string { return ppgo.CompactTextString(self) }
func (self *{{.Name}}) Size() (ret int) {
{{range .Fields}}
{{if IsStructSlice .}}
if len(self.{{GoFieldName .}}) > 0 {
for _, elm := range self.{{GoFieldName .}} {
ret += wire.SizeStruct({{PbTagNumber $obj .}}, &elm)
ret += wire.SizeStruct({{PbTagNumber $obj .}}, elm)
}
}
{{else if IsStruct .}}
ret += wire.Size{{CodecName .}}({{PbTagNumber $obj .}}, &self.{{GoFieldName .}})
ret += wire.Size{{CodecName .}}({{PbTagNumber $obj .}}, self.{{GoFieldName .}})
{{else if IsEnum .}}
ret += wire.Size{{CodecName .}}({{PbTagNumber $obj .}}, int32(self.{{GoFieldName .}}))
{{else if IsEnumSlice .}}
Expand All @@ -76,10 +70,10 @@ func (self *{{.Name}}) Marshal(buffer *wire.Buffer) error {
{{range .Fields}}
{{if IsStructSlice .}}
for _, elm := range self.{{GoFieldName .}} {
wire.MarshalStruct(buffer, {{PbTagNumber $obj .}}, &elm)
wire.MarshalStruct(buffer, {{PbTagNumber $obj .}}, elm)
}
{{else if IsStruct .}}
wire.Marshal{{CodecName .}}(buffer, {{PbTagNumber $obj .}}, &self.{{GoFieldName .}})
wire.Marshal{{CodecName .}}(buffer, {{PbTagNumber $obj .}}, self.{{GoFieldName .}})
{{else if IsEnum .}}
wire.Marshal{{CodecName .}}(buffer, {{PbTagNumber $obj .}}, int32(self.{{GoFieldName .}}))
{{else if IsEnumSlice .}}
Expand All @@ -98,13 +92,15 @@ func (self *{{.Name}}) Unmarshal(buffer *wire.Buffer, fieldIndex uint64, wt wire
if err := wire.UnmarshalStruct(buffer, wt, &elm); err != nil {
return err
} else {
self.{{GoFieldName .}} = append(self.{{GoFieldName .}}, elm)
self.{{GoFieldName .}} = append(self.{{GoFieldName .}}, &elm)
return nil
}{{else if IsEnum .}}
v, err := wire.Unmarshal{{CodecName .}}(buffer, wt)
self.{{GoFieldName .}} = {{ProtoTypeName .}}(v)
return err {{else if IsStruct .}}
return wire.Unmarshal{{CodecName .}}(buffer, wt, &self.{{GoFieldName .}}) {{else if IsEnumSlice .}}
var elm {{.Type}}
self.{{GoFieldName .}} = &elm
return wire.Unmarshal{{CodecName .}}(buffer, wt, self.{{GoFieldName .}}) {{else if IsEnumSlice .}}
v, err := wire.Unmarshal{{CodecName .}}(buffer, wt)
for _, vv := range v {
self.{{GoFieldName .}} = append(self.{{GoFieldName .}}, {{ProtoElementTypeName .}}(vv))
Expand Down Expand Up @@ -133,6 +129,13 @@ import (
"reflect"
)
var (
_ cellnet.MessageMeta
_ codec.CodecRecycler
_ reflect.Kind
)
func init() {
{{range .Structs}}
cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
Expand Down
1 change: 1 addition & 0 deletions tests/Make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go build -v -o=${GOPATH}/bin/protoplus github.com/davyxu/protoplus/cmd/protoplus
${GOPATH}/bin/protoplus -ppgo_out=code_gen.go -package=tests code.proto
${GOPATH}/bin/protoplus -ppgoreg_out=reg_gen.go -package=tests code.proto
${GOPATH}/bin/protoplus -ppcs_out=../example/csharp/Example/ProtoGen.cs -package=Proto code.proto
${GOPATH}/bin/protoplus -ppcsreg_out=../example/csharp/Example/ProtoGenReg.cs -package=Proto code.proto
${GOPATH}/bin/protoplus -pbscheme_out=pb_gen.proto -package=proto code.proto
${GOPATH}/bin/protoplus -route_out=route.json -package=proto code.proto
Loading

0 comments on commit d480aeb

Please sign in to comment.