Skip to content

Commit df2d592

Browse files
olivierlemasleRob Percival
andauthored
Add --omit_package_doc flag (#1702)
Makes it possible to omit the package comment from generated code. This is useful if the generated code is added to an existing package that already has package documentation (the generated package comment might be misleading in that case). Co-authored-by: olivierlemasle <o.lemasle@gmail.com> Co-authored-by: Rob Percival <robpercival@google.com>
1 parent 2a56465 commit df2d592

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

protoc-gen-grpc-gateway/descriptor/registry.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ type Registry struct {
9494
// generateUnboundMethods causes the registry to generate proxy methods even for
9595
// RPC methods that have no HttpRule annotation.
9696
generateUnboundMethods bool
97+
98+
// omitPackageDoc, if false, causes a package comment to be included in the generated code.
99+
omitPackageDoc bool
97100
}
98101

99102
type repeatedFieldSeparator struct {
@@ -542,6 +545,16 @@ func (r *Registry) SetGenerateUnboundMethods(generate bool) {
542545
r.generateUnboundMethods = generate
543546
}
544547

548+
// SetOmitPackageDoc controls whether the generated code contains a package comment (if set to false, it will contain one)
549+
func (r *Registry) SetOmitPackageDoc(omit bool) {
550+
r.omitPackageDoc = omit
551+
}
552+
553+
// GetOmitPackageDoc returns whether a package comment will be omitted from the generated code
554+
func (r *Registry) GetOmitPackageDoc() bool {
555+
return r.omitPackageDoc
556+
}
557+
545558
// sanitizePackageName replaces unallowed character in package name
546559
// with allowed character.
547560
func sanitizePackageName(pkgName string) string {

protoc-gen-grpc-gateway/internal/gengateway/generator.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ func (g *generator) generate(file *descriptor.File) (string, error) {
172172
RegisterFuncSuffix: g.registerFuncSuffix,
173173
AllowPatchFeature: g.allowPatchFeature,
174174
}
175+
if g.reg != nil {
176+
params.OmitPackageDoc = g.reg.GetOmitPackageDoc()
177+
}
175178
return applyTemplate(params, g.reg)
176179
}
177180

protoc-gen-grpc-gateway/internal/gengateway/template.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type param struct {
1919
UseRequestContext bool
2020
RegisterFuncSuffix string
2121
AllowPatchFeature bool
22+
OmitPackageDoc bool
2223
}
2324

2425
type binding struct {
@@ -221,11 +222,11 @@ var (
221222
// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT.
222223
// source: {{.GetName}}
223224
224-
/*
225+
{{if not .OmitPackageDoc}}/*
225226
Package {{.GoPkg.Name}} is a reverse proxy.
226227
227228
It translates gRPC into RESTful JSON APIs.
228-
*/
229+
*/{{end}}
229230
package {{.GoPkg.Name}}
230231
import (
231232
{{range $i := .Imports}}{{if $i.Standard}}{{$i | printf "%s\n"}}{{end}}{{end}}

protoc-gen-grpc-gateway/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
repeatedPathParamSeparator = flag.String("repeated_path_param_separator", "csv", "configures how repeated fields should be split. Allowed values are `csv`, `pipes`, `ssv` and `tsv`.")
3636
allowPatchFeature = flag.Bool("allow_patch_feature", true, "determines whether to use PATCH feature involving update masks (using google.protobuf.FieldMask).")
3737
allowColonFinalSegments = flag.Bool("allow_colon_final_segments", false, "determines whether colons are permitted in the final segment of a path")
38+
omitPackageDoc = flag.Bool("omit_package_doc", false, "if true, no package comment will be included in the generated code")
3839
versionFlag = flag.Bool("version", false, "print the current version")
3940
warnOnUnboundMethods = flag.Bool("warn_on_unbound_methods", false, "emit a warning message if an RPC method has no HttpRule annotation")
4041
generateUnboundMethods = flag.Bool("generate_unbound_methods", false, "generate proxy methods even for RPC methods that have no HttpRule annotation")
@@ -98,6 +99,7 @@ func main() {
9899
reg.SetAllowDeleteBody(*allowDeleteBody)
99100
reg.SetAllowRepeatedFieldsInBody(*allowRepeatedFieldsInBody)
100101
reg.SetAllowColonFinalSegments(*allowColonFinalSegments)
102+
reg.SetOmitPackageDoc(*omitPackageDoc)
101103

102104
if *warnOnUnboundMethods && *generateUnboundMethods {
103105
glog.Warningf("Option warn_on_unbound_methods has no effect when generate_unbound_methods is used.")

0 commit comments

Comments
 (0)