Merge pull request #6828 from kzys/fix-fieldpath

Fix protoc-gen-go-fieldpath
This commit is contained in:
Derek McGowan 2022-04-19 21:48:09 -07:00 committed by GitHub
commit 809549e566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -130,7 +130,7 @@ func (gen *generator) genFieldMethod(m *protogen.Message) {
} }
func isMessageField(f *protogen.Field) bool { func isMessageField(f *protogen.Field) bool {
return f.Desc.Kind() == protoreflect.MessageKind && f.GoIdent.GoName != "Timestamp" return f.Desc.Kind() == protoreflect.MessageKind && f.Desc.Cardinality() != protoreflect.Repeated && f.Message.GoIdent.GoName != "Timestamp"
} }
func isLabelsField(f *protogen.Field) bool { func isLabelsField(f *protogen.Field) bool {
@ -138,17 +138,50 @@ func isLabelsField(f *protogen.Field) bool {
} }
func isAnyField(f *protogen.Field) bool { func isAnyField(f *protogen.Field) bool {
return f.Desc.Kind() == protoreflect.MessageKind && f.GoIdent.GoName == "Any" return f.Desc.Kind() == protoreflect.MessageKind && f.Message.GoIdent.GoName == "Any"
}
func collectChildlen(parent *protogen.Message) ([]*protogen.Message, error) {
var children []*protogen.Message
for _, child := range parent.Messages {
if child.Desc.IsMapEntry() {
continue
}
children = append(children, child)
xs, err := collectChildlen(child)
if err != nil {
return nil, err
}
children = append(children, xs...)
}
return children, nil
} }
func generate(plugin *protogen.Plugin, input *protogen.File) error { func generate(plugin *protogen.Plugin, input *protogen.File) error {
var messages []*protogen.Message
for _, m := range input.Messages {
messages = append(messages, m)
children, err := collectChildlen(m)
if err != nil {
return err
}
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 := plugin.NewGeneratedFile(input.GeneratedFilenamePrefix+"_fieldpath.pb.go", input.GoImportPath)
file.P("// Code generated by protoc-gen-go-fieldpath. DO NOT EDIT.") file.P("// Code generated by protoc-gen-go-fieldpath. DO NOT EDIT.")
file.P("// source: ", input.Desc.Path()) file.P("// source: ", input.Desc.Path())
file.P("package ", input.GoPackageName) file.P("package ", input.GoPackageName)
gen := newGenerator(file) gen := newGenerator(file)
for _, m := range input.Messages {
for _, m := range messages {
gen.genFieldMethod(m) gen.genFieldMethod(m)
} }
return nil return nil