-
Notifications
You must be signed in to change notification settings - Fork 9
/
protodesc.go
52 lines (41 loc) · 1.6 KB
/
protodesc.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package protos
import (
"io/fs"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"namespacelabs.dev/foundation/internal/fnerrors"
)
func LoadDescriptorByName(src *FileDescriptorSetAndDeps, name string) (*protoregistry.Files, protoreflect.Descriptor, error) {
pd, err := protodesc.NewFiles(src.AsFileDescriptorSet())
if err != nil {
return nil, nil, err
}
desc, err := pd.FindDescriptorByName(protoreflect.FullName(name))
return pd, desc, err
}
func LoadMessageByName(src *FileDescriptorSetAndDeps, name string) (*protoregistry.Files, protoreflect.MessageDescriptor, error) {
pd, desc, err := LoadDescriptorByName(src, name)
if err != nil {
return nil, nil, err
}
msgdesc, ok := desc.(protoreflect.MessageDescriptor)
if !ok {
return nil, nil, fnerrors.New("%s: expected a message type", name)
}
return pd, msgdesc, nil
}
func (opts ParseOpts) LoadMessageAtLocation(fsys fs.FS, loc Location, sources []string, name string) (protoreflect.MessageDescriptor, error) {
parsed, err := opts.ParseAtLocation(fsys, loc, sources)
if err != nil {
return nil, fnerrors.BadInputError("failed to parse proto sources %v: %w", sources, err)
}
_, msgdesc, err := LoadMessageByName(parsed, name)
if err != nil {
return nil, fnerrors.BadInputError("%s: failed to load message: %w", name, err)
}
return msgdesc, nil
}