Skip to content

Commit

Permalink
add omitempty (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
longXboy committed Jun 14, 2021
1 parent 864a791 commit dc80d08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 9 additions & 6 deletions cmd/protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (
var methodSets = make(map[string]int)

// generateFile generates a _http.pb.go file containing kratos errors definitions.
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if !hasHTTPRule(file.Services) {
func generateFile(gen *protogen.Plugin, file *protogen.File, omitempty bool) *protogen.GeneratedFile {
if len(file.Services) == 0 || (omitempty && !hasHTTPRule(file.Services)) {
return nil
}
filename := file.GeneratedFilenamePrefix + "_http.pb.go"
Expand All @@ -31,12 +31,12 @@ func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
g.P()
g.P("package ", file.GoPackageName)
g.P()
generateFileContent(gen, file, g)
generateFileContent(gen, file, g, omitempty)
return g
}

// generateFileContent generates the kratos errors definitions, excluding the package statement.
func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, omitempty bool) {
if len(file.Services) == 0 {
return
}
Expand All @@ -48,11 +48,11 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
g.P()

for _, service := range file.Services {
genService(gen, file, g, service)
genService(gen, file, g, service, omitempty)
}
}

func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service) {
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)
Expand All @@ -73,6 +73,9 @@ func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.Generated
sd.Methods = append(sd.Methods, buildHTTPRule(g, method, bind))
}
sd.Methods = append(sd.Methods, buildHTTPRule(g, method, rule))
} else if !omitempty {
path := fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name())
sd.Methods = append(sd.Methods, buildMethodDesc(g, method, "POST", path))
}
}
g.P(sd.execute())
Expand Down
8 changes: 5 additions & 3 deletions cmd/protoc-gen-go-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ const version = "v2.0.0-rc3"

func main() {
showVersion := flag.Bool("version", false, "print the version and exit")
omitempty := flag.Bool("omitempty", true, "omit if google.api is empty")

flag.Parse()
if *showVersion {
fmt.Printf("protoc-gen-go-http %v\n", version)
return
}

var flags flag.FlagSet
//var flags flag.FlagSet

protogen.Options{
ParamFunc: flags.Set,
ParamFunc: flag.CommandLine.Set,
}.Run(func(gen *protogen.Plugin) error {
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
for _, f := range gen.Files {
if !f.Generate {
continue
}
generateFile(gen, f)
generateFile(gen, f, *omitempty)
}
return nil
})
Expand Down

0 comments on commit dc80d08

Please sign in to comment.