Skip to content

Commit

Permalink
支持设置默认编码,生成独立go绑定
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Dec 31, 2020
1 parent e4c9a6d commit 0198c16
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
13 changes: 13 additions & 0 deletions cmd/protoplus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ var (
flagGoOut = flag.String("go_out", "", "output golang source file")
flagCSOut = flag.String("cs_out", "", "output csharp source file")
flagJsonOut = flag.String("json_out", "", "output json file")
flagGoRegOut = flag.String("goreg_out", "", "output golang message register source file")
flagJson = flag.Bool("json", false, "output json to std out")
flagGenReg = flag.Bool("genreg", false, "gen message register entry")
flagStructBase = flag.String("structbase", "IProtoStruct", "struct inherite class type name in c#")
flagCodec = flag.String("codec", "protoplus", "default codec in register entry")
)

const Version = "2.0.0"
Expand All @@ -46,6 +48,7 @@ func main() {
ctx.PackageName = *flagPackage
ctx.StructBase = *flagStructBase
ctx.RegEntry = *flagGenReg
ctx.Codec = *flagCodec

err = util.ParseFileList(ctx.DescriptorSet)

Expand All @@ -63,6 +66,16 @@ func main() {
}
}

if *flagGoRegOut != "" {
ctx.OutputFileName = *flagGoRegOut

err = golang.GenGoReg(&ctx)

if err != nil {
goto OnError
}
}

if *flagCSOut != "" {
ctx.OutputFileName = *flagCSOut

Expand Down
2 changes: 2 additions & 0 deletions gen/gogopb/gen_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const protoCodeTemplate = `// Generated by github.com/davyxu/protoplus
// DO NOT EDIT!
syntax = "proto3";
option go_package= ".;proto";
package {{.PackageName}};
{{range $a, $enumobj := .Enums}}
Expand Down
2 changes: 1 addition & 1 deletion gen/golang/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
UsefulFunc["StructCodec"] = func(d *model.Descriptor) string {
codecName := d.TagValueString("Codec")
if codecName == "" {
return "protoplus"
return d.DescriptorSet.Codec
}

return codecName
Expand Down
16 changes: 16 additions & 0 deletions gen/golang/gen_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ func GenGo(ctx *gen.Context) error {

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

func GenGoReg(ctx *gen.Context) error {

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

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

return gen.WriteOutputFile(ctx.OutputFileName).Error()
}
20 changes: 20 additions & 0 deletions gen/golang/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,23 @@ func init() {
}
`

const RegTemplateText = `// Generated by github.com/davyxu/protoplus
package {{.PackageName}}
import (
"github.com/davyxu/cellnet"
"github.com/davyxu/cellnet/codec"
"reflect"
)
func init() {
{{range .Structs}}
cellnet.RegisterMessageMeta(&cellnet.MessageMeta{
Codec: codec.MustGetCodec("{{StructCodec .}}"),
Type: reflect.TypeOf((*{{.Name}})(nil)).Elem(),
ID: {{StructMsgID .}},
New: func() interface{} { return &{{.Name}}{} },
}) {{end}}
}
`
1 change: 1 addition & 0 deletions model/descriptorset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
type DescriptorSet struct {
Objects []*Descriptor `json:",omitempty"`
PackageName string
Codec string
}

func (self *DescriptorSet) Services() (ret []*Descriptor) {
Expand Down

0 comments on commit 0198c16

Please sign in to comment.