diff --git a/cmd/protoplus/main.go b/cmd/protoplus/main.go index 485d83d..b663d97 100644 --- a/cmd/protoplus/main.go +++ b/cmd/protoplus/main.go @@ -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" @@ -46,6 +48,7 @@ func main() { ctx.PackageName = *flagPackage ctx.StructBase = *flagStructBase ctx.RegEntry = *flagGenReg + ctx.Codec = *flagCodec err = util.ParseFileList(ctx.DescriptorSet) @@ -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 diff --git a/gen/gogopb/gen_proto.go b/gen/gogopb/gen_proto.go index 0266934..32a15b1 100644 --- a/gen/gogopb/gen_proto.go +++ b/gen/gogopb/gen_proto.go @@ -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}} diff --git a/gen/golang/func.go b/gen/golang/func.go index eac6b0d..15cbfa1 100644 --- a/gen/golang/func.go +++ b/gen/golang/func.go @@ -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 diff --git a/gen/golang/gen_go.go b/gen/golang/gen_go.go index 3631318..0b760c1 100644 --- a/gen/golang/gen_go.go +++ b/gen/golang/gen_go.go @@ -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() +} diff --git a/gen/golang/text.go b/gen/golang/text.go index 7b19b34..e99b223 100644 --- a/gen/golang/text.go +++ b/gen/golang/text.go @@ -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}} +} +` diff --git a/model/descriptorset.go b/model/descriptorset.go index bc9db8f..96dcf37 100644 --- a/model/descriptorset.go +++ b/model/descriptorset.go @@ -3,6 +3,7 @@ package model type DescriptorSet struct { Objects []*Descriptor `json:",omitempty"` PackageName string + Codec string } func (self *DescriptorSet) Services() (ret []*Descriptor) {