This repository was archived by the owner on May 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesc_source.go
More file actions
82 lines (71 loc) · 2.63 KB
/
Copy pathdesc_source.go
File metadata and controls
82 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package grpcake
import (
"context"
"errors"
"fmt"
"github.com/bufbuild/protocompile"
"github.com/bufbuild/protocompile/linker"
"github.com/jhump/protoreflect/grpcreflect"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/reflect/protoreflect"
)
// DescriptorSource is a source of protobuf descriptor information. It can be backed by a FileDescriptorSet
// proto (like a file generated by protoc) or a remote server that supports the reflection API.
type DescriptorSource interface {
// FindSymbol returns a descriptor for the given fully-qualified symbol name.
FindServiceDescriptor(fullyQualifiedName string) (protoreflect.ServiceDescriptor, error)
}
type fileSource struct {
files linker.Files
}
// FindServiceDescriptor implements DescriptorSource
func (fs fileSource) FindServiceDescriptor(fullyQualifiedName string) (protoreflect.ServiceDescriptor, error) {
for _, descriptor := range fs.files {
svcDescriptors := descriptor.Services()
for i := 0; i < svcDescriptors.Len(); i++ {
serviceDescriptor := svcDescriptors.Get(i)
if serviceDescriptor.FullName() == protoreflect.FullName(fullyQualifiedName) {
return serviceDescriptor, nil
}
}
}
return nil, fmt.Errorf("error finding service with name %s", fullyQualifiedName)
}
func DescriptorSourceFromProtoFiles(ctx context.Context, importPaths, fileNames []string) (DescriptorSource, error) {
compiler := protocompile.Compiler{
Resolver: protocompile.WithStandardImports(&protocompile.SourceResolver{
ImportPaths: importPaths,
}),
}
files, err := compiler.Compile(ctx, fileNames...)
if err != nil {
return nil, err
}
return fileSource{files: files}, nil
}
type serverSource struct {
client *grpcreflect.Client
}
// DescriptorSourceFromServer creates a DescriptorSource that uses the given gRPC reflection client
// to interrogate a server for descriptor information. If the server does not support the reflection
// API then the various DescriptorSource methods will return ErrReflectionNotSupported
func DescriptorSourceFromServer(_ context.Context, refClient *grpcreflect.Client) DescriptorSource {
return serverSource{client: refClient}
}
func (ss serverSource) FindServiceDescriptor(fullyQualifiedName string) (protoreflect.ServiceDescriptor, error) {
sd, err := ss.client.ResolveService(fullyQualifiedName)
if err != nil {
return nil, reflectionSupport(err)
}
return sd.UnwrapService(), nil
}
func reflectionSupport(err error) error {
if err == nil {
return nil
}
if stat, ok := status.FromError(err); ok && stat.Code() == codes.Unimplemented {
return errors.New("server does not support the reflection API")
}
return err
}