diff --git a/cmd/protoc-gen-dyn-gin/gin.go b/cmd/protoc-gen-dyn-gin/gin.go index 07e2127..224dbfd 100644 --- a/cmd/protoc-gen-dyn-gin/gin.go +++ b/cmd/protoc-gen-dyn-gin/gin.go @@ -34,7 +34,7 @@ func runProtoGen(gen *protogen.Plugin) error { if !f.Generate { continue } - generateFile(gen, f, *omitempty) + generateFile(gen, f, args.Omitempty) } return nil } @@ -82,16 +82,18 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen. } func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service, omitempty bool) { - if service.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() { - g.P("//") - g.P(deprecationComment) + comment := service.Comments.Leading.String() + service.Comments.Trailing.String() + if comment != "" { + comment = strings.TrimSpace(strings.TrimPrefix(strings.TrimSuffix(comment, "\n"), "//")) // nolint } // HTTP Server. sd := &serviceDesc{ + Deprecated: service.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated(), ServiceType: service.GoName, ServiceName: string(service.Desc.FullName()), Metadata: file.Desc.Path(), - UseEncoding: *useEncoding, + Comment: comment, + UseEncoding: args.UseEncoding, } for _, method := range service.Methods { if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() { @@ -173,7 +175,7 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati case method == http.MethodDelete: if body != "" { md.HasBody = true - if !*allowDeleteBody { + if !args.AllowDeleteBody { md.HasBody = false _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s body should not be declared.\n", method, path) } @@ -185,7 +187,7 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati md.HasBody = true } else { md.HasBody = false - if !*allowEmptyPatchBody { + if !args.AllowEmptyPatchBody { _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s is does not declare a body.\n", method, path) } } @@ -237,19 +239,20 @@ func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path } comment := m.Comments.Leading.String() + m.Comments.Trailing.String() if comment != "" { - comment = "// " + m.GoName + strings.TrimPrefix(strings.TrimSuffix(comment, "\n"), "//") + comment = "// " + m.GoName + " " + strings.TrimSpace(strings.TrimPrefix(strings.TrimSuffix(comment, "\n"), "//")) } else { comment = "// " + m.GoName } return &methodDesc{ - Name: m.GoName, - Num: methodSets[m.GoName], - Request: g.QualifiedGoIdent(m.Input.GoIdent), - Reply: g.QualifiedGoIdent(m.Output.GoIdent), - Comment: comment, - Path: transformPathParams(path), - Method: method, - HasVars: len(vars) > 0, + Deprecated: m.Desc.Options().(*descriptorpb.MethodOptions).GetDeprecated(), + Name: m.GoName, + Num: methodSets[m.GoName], + Request: g.QualifiedGoIdent(m.Input.GoIdent), + Reply: g.QualifiedGoIdent(m.Output.GoIdent), + Comment: comment, + Path: transformPathParams(path), + Method: method, + HasVars: len(vars) > 0, } } diff --git a/cmd/protoc-gen-dyn-gin/main.go b/cmd/protoc-gen-dyn-gin/main.go index d233c02..35bd844 100644 --- a/cmd/protoc-gen-dyn-gin/main.go +++ b/cmd/protoc-gen-dyn-gin/main.go @@ -7,17 +7,33 @@ import ( "google.golang.org/protobuf/compiler/protogen" ) -const version = "v0.0.1" +const version = "v0.0.2" -var showVersion = flag.Bool("version", false, "print the version and exit") -var omitempty = flag.Bool("omitempty", true, "omit if google.api is empty") -var allowDeleteBody = flag.Bool("allow_delete_body", false, "allow delete body") -var allowEmptyPatchBody = flag.Bool("allow_empty_patch_body", false, "allow empty patch body") -var useEncoding = flag.Bool("use_encoding", false, "use the framework encoding") +var args = struct { + ShowVersion bool + Omitempty bool + AllowDeleteBody bool + AllowEmptyPatchBody bool + UseEncoding bool +}{ + ShowVersion: false, + Omitempty: true, + AllowDeleteBody: false, + AllowEmptyPatchBody: false, + UseEncoding: false, +} + +func init() { + flag.BoolVar(&args.ShowVersion, "version", false, "print the version and exit") + flag.BoolVar(&args.Omitempty, "omitempty", true, "omit if google.api is empty") + flag.BoolVar(&args.AllowDeleteBody, "allow_delete_body", false, "allow delete body") + flag.BoolVar(&args.AllowEmptyPatchBody, "allow_empty_patch_body", false, "allow empty patch body") + flag.BoolVar(&args.UseEncoding, "use_encoding", false, "use the framework encoding") +} func main() { flag.Parse() - if *showVersion { + if args.ShowVersion { fmt.Printf("protoc-gen-dyn-gin %v\n", version) return } diff --git a/cmd/protoc-gen-dyn-gin/service.go b/cmd/protoc-gen-dyn-gin/service.go index fa9b526..e87ab26 100644 --- a/cmd/protoc-gen-dyn-gin/service.go +++ b/cmd/protoc-gen-dyn-gin/service.go @@ -7,15 +7,18 @@ import ( ) type serviceDesc struct { + Deprecated bool // deprecated or not ServiceType string // Greeter ServiceName string // helloworld.Greeter Metadata string // api/v1/helloworld.proto + Comment string // comment Methods []*methodDesc UseEncoding bool } type methodDesc struct { + Deprecated bool // deprecated or not // method Name string // 方法名 Num int // 方法号 @@ -34,6 +37,10 @@ type methodDesc struct { func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error { methodSets := make(map[string]struct{}) // http interface defined + if s.Deprecated { + g.P(deprecationComment) + } + g.P("// ", serverInterfaceName(s.ServiceType), " ", s.Comment) g.P("type ", serverInterfaceName(s.ServiceType), " interface {") for _, m := range s.Methods { _, ok := methodSets[m.Name] @@ -41,12 +48,18 @@ func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error { continue } methodSets[m.Name] = struct{}{} + if m.Deprecated { + g.P(deprecationComment) + } g.P(m.Comment) g.P(serverMethodName(g, m)) } g.P("}") g.P() // register http server handler + if s.Deprecated { + g.P(deprecationComment) + } g.P("func Register", s.ServiceType, "HTTPServer(g *", g.QualifiedGoIdent(ginPackage.Ident("RouterGroup")), ", srv ", serverInterfaceName(s.ServiceType), ") {") g.P(`r := g.Group("")`) g.P("{") @@ -58,6 +71,9 @@ func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error { g.P() // handler for _, m := range s.Methods { + if m.Deprecated { + g.P(deprecationComment) + } g.P("func ", serverHandlerMethodName(s.ServiceType, m), "(srv ", s.ServiceType, "HTTPServer", ") ", g.QualifiedGoIdent(ginPackage.Ident("HandlerFunc")), " {") { // gin.HandleFunc closure g.P("return func(c *", g.QualifiedGoIdent(ginPackage.Ident("Context")), ") {") diff --git a/cmd/protoc-gen-dyn-resty/main.go b/cmd/protoc-gen-dyn-resty/main.go index 4db708b..2297e14 100644 --- a/cmd/protoc-gen-dyn-resty/main.go +++ b/cmd/protoc-gen-dyn-resty/main.go @@ -7,16 +7,30 @@ import ( "google.golang.org/protobuf/compiler/protogen" ) -const version = "0.1.0" +const version = "0.1.1" -var showVersion = flag.Bool("version", false, "print the version and exit") -var omitempty = flag.Bool("omitempty", true, "omit if google.api is empty") -var allowDeleteBody = flag.Bool("allow_delete_body", false, "allow delete body") -var allowEmptyPatchBody = flag.Bool("allow_empty_patch_body", false, "allow empty patch body") +var args = struct { + ShowVersion bool + Omitempty bool + AllowDeleteBody bool + AllowEmptyPatchBody bool +}{ + ShowVersion: false, + Omitempty: true, + AllowDeleteBody: false, + AllowEmptyPatchBody: false, +} + +func init() { + flag.BoolVar(&args.ShowVersion, "version", false, "print the version and exit") + flag.BoolVar(&args.Omitempty, "omitempty", true, "omit if google.api is empty") + flag.BoolVar(&args.AllowDeleteBody, "allow_delete_body", false, "allow delete body") + flag.BoolVar(&args.AllowEmptyPatchBody, "allow_empty_patch_body", false, "allow empty patch body") +} func main() { flag.Parse() - if *showVersion { + if args.ShowVersion { fmt.Printf("protoc-gen-dyn-resty %v\n", version) return } diff --git a/cmd/protoc-gen-dyn-resty/resty.go b/cmd/protoc-gen-dyn-resty/resty.go index 3b0656c..214a8bb 100644 --- a/cmd/protoc-gen-dyn-resty/resty.go +++ b/cmd/protoc-gen-dyn-resty/resty.go @@ -30,7 +30,7 @@ func runProtoGen(gen *protogen.Plugin) error { if !f.Generate { continue } - generateFile(gen, f, *omitempty) + generateFile(gen, f, args.Omitempty) } return nil } @@ -179,7 +179,7 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati case method == http.MethodDelete: if body != "" { md.HasBody = true - if !*allowDeleteBody { + if !args.AllowDeleteBody { md.HasBody = false _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s body should not be declared.\n", method, path) } @@ -191,7 +191,7 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati md.HasBody = true } else { md.HasBody = false - if !*allowEmptyPatchBody { + if !args.AllowEmptyPatchBody { _, _ = fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: %s %s is does not declare a body.\n", method, path) } } diff --git a/cmd/protoc-gen-dyn-resty/service.go b/cmd/protoc-gen-dyn-resty/service.go index 8053ce4..7929d66 100644 --- a/cmd/protoc-gen-dyn-resty/service.go +++ b/cmd/protoc-gen-dyn-resty/service.go @@ -7,13 +7,16 @@ import ( ) type serviceDesc struct { + Deprecated bool // deprecated or not ServiceType string // Greeter ServiceName string // helloworld.Greeter Metadata string // api/v1/helloworld.proto + Comment string // comment Methods []*methodDesc } type methodDesc struct { + Deprecated bool // deprecated or not // method Name string // 方法名 Num int // 方法号 @@ -31,8 +34,15 @@ type methodDesc struct { func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error { // http interface defined + if s.Deprecated { + g.P(deprecationComment) + } + g.P("// ", clientInterfaceName(s.ServiceType), " ", s.Comment) g.P("type ", clientInterfaceName(s.ServiceType), " interface {") for _, m := range s.Methods { + if m.Deprecated { + g.P(deprecationComment) + } g.P(m.Comment) g.P(clientMethodName(g, m, true)) } @@ -45,6 +55,9 @@ func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error { g.P("}") g.P() // http client factory method. + if s.Deprecated { + g.P(deprecationComment) + } g.P("func New", s.ServiceType, "HTTPClient(c *", g.QualifiedGoIdent(transportHttpPackage.Ident("Client")), ") ", clientInterfaceName(s.ServiceType), " {") g.P("return &", clientImplStructName(s.ServiceType), " {") g.P("cc: c,") @@ -54,6 +67,9 @@ func executeServiceDesc(g *protogen.GeneratedFile, s *serviceDesc) error { // http client implement methods. for _, m := range s.Methods { + if m.Deprecated { + g.P(deprecationComment) + } g.P(m.Comment) g.P("func (c *", clientImplStructName(s.ServiceType), ")", clientMethodName(g, m, false), " {") g.P("var err error")