From 3742c756e8a83e4c0c57bb5fdd6af70c4ff8506a Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Tue, 19 Apr 2022 22:15:28 +0000 Subject: [PATCH] Don't generate a Go file, if that would be empty Signed-off-by: Kazuyoshi Kato --- cmd/protoc-gen-go-fieldpath/generator.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/protoc-gen-go-fieldpath/generator.go b/cmd/protoc-gen-go-fieldpath/generator.go index 229f9c48e..73f1c6fed 100644 --- a/cmd/protoc-gen-go-fieldpath/generator.go +++ b/cmd/protoc-gen-go-fieldpath/generator.go @@ -159,13 +159,6 @@ func collectChildlen(parent *protogen.Message) ([]*protogen.Message, 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 for _, m := range input.Messages { messages = append(messages, m) @@ -176,6 +169,18 @@ func generate(plugin *protogen.Plugin, input *protogen.File) error { 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 { gen.genFieldMethod(m) }