-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_proto_resolver.go
61 lines (54 loc) · 1.41 KB
/
client_proto_resolver.go
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
package registry
import (
"context"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"strings"
)
type versionedResolver struct {
ctx context.Context
registry *Client
refs references
}
func (r *versionedResolver) FindFileByPath(subject string) (protoreflect.FileDescriptor, error) {
for _, ref := range r.refs {
if ref.Subject == subject {
schema, err := r.registry.GetSubjectVersion(r.ctx, subject, ref.Version)
if err != nil {
return nil, err
}
if protoSchema, ok := schema.(*ProtobufSchema); ok {
return protoSchema.descriptor, nil
}
}
}
return nil, protoregistry.NotFound
}
func (r *versionedResolver) FindDescriptorByName(name protoreflect.FullName) (protoreflect.Descriptor, error) {
for _, ref := range r.refs {
schema, err := r.registry.GetSubjectVersion(r.ctx, ref.Subject, ref.Version)
if err != nil {
return nil, err
}
if schema == nil {
return nil, protoregistry.NotFound
}
protoSchema, ok := schema.(*ProtobufSchema)
if !ok {
continue
}
matchParent := false
for p := name.Parent(); p != ""; p = p.Parent() {
if strings.HasSuffix(string(p), string(protoSchema.descriptor.FullName())) {
matchParent = true
}
}
if !matchParent {
continue
}
if m := protoSchema.descriptor.Messages().ByName(name.Name()); m != nil {
return m, nil
}
}
return nil, protoregistry.NotFound
}