From 98ea6d48471c62368d2f37a9bf319bd93040eac7 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Fri, 17 Mar 2023 16:22:03 +0900 Subject: [PATCH] Skip registration of conflicted descriptors --- grpcstub.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/grpcstub.go b/grpcstub.go index b662226..e5915ad 100644 --- a/grpcstub.go +++ b/grpcstub.go @@ -856,9 +856,21 @@ func registerFileDescriptors(fds []*desc.FileDescriptor) (err error) { return err } registry.RangeFiles(func(fd protoreflect.FileDescriptor) bool { - if ofd, _ := protoregistry.GlobalFiles.FindFileByPath(fd.Path()); ofd != nil { + if _, err := protoregistry.GlobalFiles.FindFileByPath(fd.Path()); !errors.Is(protoregistry.NotFound, err) { return true } + + // Skip registration of conflicted descriptors + conflict := false + rangeTopLevelDescriptors(fd, func(d protoreflect.Descriptor) { + if _, err := protoregistry.GlobalFiles.FindDescriptorByName(d.FullName()); err == nil { + conflict = true + } + }) + if conflict { + return true + } + err = protoregistry.GlobalFiles.RegisterFile(fd) return (err == nil) }) @@ -873,3 +885,27 @@ func contains(s []string, e string) bool { } return false } + +// copy from google.golang.org/protobuf/reflect/protoregistry +func rangeTopLevelDescriptors(fd protoreflect.FileDescriptor, f func(protoreflect.Descriptor)) { + eds := fd.Enums() + for i := eds.Len() - 1; i >= 0; i-- { + f(eds.Get(i)) + vds := eds.Get(i).Values() + for i := vds.Len() - 1; i >= 0; i-- { + f(vds.Get(i)) + } + } + mds := fd.Messages() + for i := mds.Len() - 1; i >= 0; i-- { + f(mds.Get(i)) + } + xds := fd.Extensions() + for i := xds.Len() - 1; i >= 0; i-- { + f(xds.Get(i)) + } + sds := fd.Services() + for i := sds.Len() - 1; i >= 0; i-- { + f(sds.Get(i)) + } +}