forked from raystack/stencil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotoutils.go
43 lines (38 loc) · 1.13 KB
/
protoutils.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
package stencil
import (
"fmt"
"strings"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
)
func getJavaPackage(fileDesc protoreflect.FileDescriptor) string {
file := protodesc.ToFileDescriptorProto(fileDesc)
options := file.Options
if options != nil && options.JavaPackage != nil {
return *options.JavaPackage
}
return ""
}
func defaultKeyFn(msg protoreflect.MessageDescriptor) string {
fullName := string(msg.FullName())
file := msg.ParentFile()
protoPackage := string(file.Package())
pkg := getJavaPackage(file)
if pkg == "" {
return fullName
} else if protoPackage == "" {
return fmt.Sprintf("%s.%s", pkg, fullName)
}
return strings.Replace(fullName, protoPackage, pkg, 1)
}
func getFilesRegistry(data []byte) (*protoregistry.Files, error) {
msg := &descriptorpb.FileDescriptorSet{}
err := proto.Unmarshal(data, msg)
if err != nil {
return nil, fmt.Errorf("invalid file descriptorset file. %w", err)
}
return protodesc.NewFiles(msg)
}