Skip to content

Commit

Permalink
fix: add cmd deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Aug 18, 2023
1 parent 89af205 commit 2be95d2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 32 deletions.
35 changes: 19 additions & 16 deletions cmd/protoc-gen-dyn-gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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,
}
}

Expand Down
30 changes: 23 additions & 7 deletions cmd/protoc-gen-dyn-gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/protoc-gen-dyn-gin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 方法号
Expand All @@ -34,19 +37,29 @@ 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]
if ok { // unique because additional_bindings
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("{")
Expand All @@ -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")), ") {")
Expand Down
26 changes: 20 additions & 6 deletions cmd/protoc-gen-dyn-resty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/protoc-gen-dyn-resty/resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/protoc-gen-dyn-resty/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 方法号
Expand All @@ -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))
}
Expand All @@ -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,")
Expand All @@ -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")
Expand Down

0 comments on commit 2be95d2

Please sign in to comment.