forked from raystack/stencil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotoutils.go
35 lines (30 loc) · 885 Bytes
/
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
package stencil
import (
"strings"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
)
func forEachMessage(msgs protoreflect.MessageDescriptors, f func(protoreflect.MessageDescriptor)) {
for i := 0; i < msgs.Len(); i++ {
msg := msgs.Get(i)
f(msg)
forEachMessage(msg.Messages(), f)
}
}
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(file protoreflect.FileDescriptor, msg protoreflect.MessageDescriptor) string {
fullName := string(msg.FullName())
protoPackage := string(file.Package())
pkg := getJavaPackage(file)
if pkg == "" {
return fullName
}
return strings.Replace(fullName, protoPackage, pkg, 1)
}