From 196c1230581b2204777e46714912166422e50e2d Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Mon, 16 May 2016 16:35:44 -0700 Subject: [PATCH] Add fileDescContainingExtension --- reflection/serverreflection.go | 32 +++++++++++++++++++++++++++++ reflection/serverreflection_test.go | 2 ++ 2 files changed, 34 insertions(+) diff --git a/reflection/serverreflection.go b/reflection/serverreflection.go index e9d0cc13..fe2dae88 100644 --- a/reflection/serverreflection.go +++ b/reflection/serverreflection.go @@ -139,3 +139,35 @@ func (s *serverReflectionServer) filenameForType(st reflect.Type) (string, error } // TODO filenameForMethod and Service + +func (s *serverReflectionServer) fileDescContainingExtension(st reflect.Type, ext int32) (*dpb.FileDescriptorProto, error) { + m, ok := reflect.Zero(reflect.PtrTo(st)).Interface().(proto.Message) + if !ok { + return nil, fmt.Errorf("failed to create message from type: %v", st) + } + + var extDesc *proto.ExtensionDesc + for id, desc := range proto.RegisteredExtensions(m) { + if id == ext { + extDesc = desc + break + } + } + + if extDesc == nil { + return nil, fmt.Errorf("failed to find registered extension for extension number %v", ext) + } + + extT := reflect.TypeOf(extDesc.ExtensionType).Elem() + // TODO doesn't work if extT is simple types, like int32 + // TODO check cache + fd, _, err := s.fileDescForType(extT) + if err != nil { + return nil, err + } + return fd, nil +} + +// TODO filenameContainingExtension +// fd := fileDescContainingExtension() +// return fd.GetName() diff --git a/reflection/serverreflection_test.go b/reflection/serverreflection_test.go index 70c2b79b..9497ac0c 100644 --- a/reflection/serverreflection_test.go +++ b/reflection/serverreflection_test.go @@ -87,3 +87,5 @@ func TestFilenameForType(t *testing.T) { } } } + +// TODO TestFileDescContainingExtension