Don't generate a Go file, if that would be empty

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato 2022-04-19 22:15:28 +00:00
parent 5f3b724323
commit 3742c756e8

View File

@ -159,13 +159,6 @@ func collectChildlen(parent *protogen.Message) ([]*protogen.Message, error) {
} }
func generate(plugin *protogen.Plugin, input *protogen.File) error { func generate(plugin *protogen.Plugin, input *protogen.File) error {
file := plugin.NewGeneratedFile(input.GeneratedFilenamePrefix+"_fieldpath.pb.go", input.GoImportPath)
file.P("// Code generated by protoc-gen-go-fieldpath. DO NOT EDIT.")
file.P("// source: ", input.Desc.Path())
file.P("package ", input.GoPackageName)
gen := newGenerator(file)
var messages []*protogen.Message var messages []*protogen.Message
for _, m := range input.Messages { for _, m := range input.Messages {
messages = append(messages, m) messages = append(messages, m)
@ -176,6 +169,18 @@ func generate(plugin *protogen.Plugin, input *protogen.File) error {
messages = append(messages, children...) messages = append(messages, children...)
} }
if len(messages) == 0 {
// Don't generate a Go file, if that would be empty.
return nil
}
file := plugin.NewGeneratedFile(input.GeneratedFilenamePrefix+"_fieldpath.pb.go", input.GoImportPath)
file.P("// Code generated by protoc-gen-go-fieldpath. DO NOT EDIT.")
file.P("// source: ", input.Desc.Path())
file.P("package ", input.GoPackageName)
gen := newGenerator(file)
for _, m := range messages { for _, m := range messages {
gen.genFieldMethod(m) gen.genFieldMethod(m)
} }