forked from fajran/protoc-gen-twirp_java_jaxrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
164 lines (142 loc) · 4.03 KB
/
utils.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"path/filepath"
"strings"
"unicode"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
const (
javaOuterClassSuffix = "OuterClass"
)
func getProtoName(file *descriptor.FileDescriptorProto) string {
name := file.GetName()
ext := filepath.Ext(name)
if ext == ".proto" || ext == ".protodevel" {
name = name[0: len(name)-len(ext)]
}
return name
}
func getJavaOuterClassName(file *descriptor.FileDescriptorProto) string {
name := file.Options.GetJavaOuterClassname()
if name != "" {
return name
}
name = camelCase(getProtoName(file))
outer := name + javaOuterClassSuffix
for _, desc := range file.GetMessageType() {
if strings.Title(desc.GetName()) == name {
return outer
}
}
for _, desc := range file.GetService() {
if strings.Title(desc.GetName()) == name {
return outer
}
}
for _, desc := range file.GetEnumType() {
if strings.Title(desc.GetName()) == name {
return outer
}
}
return name
}
func getJavaOuterClassFile(file *descriptor.FileDescriptorProto) string {
className := getJavaOuterClassName(file)
pkg := getJavaPackage(file)
if pkg == "" {
return fmt.Sprintf("%s.java", className)
} else {
dir := strings.Replace(pkg, ".", "/", -1)
return fmt.Sprintf("%s/%s.java", dir, className)
}
}
func containsType(name string, file *descriptor.FileDescriptorProto) bool {
for _, t := range file.GetEnumType() {
if t.GetName() == name {
return true
}
}
for _, t := range file.GetMessageType() {
if t.GetName() == name {
return true
}
}
for _, t := range file.GetService() {
if t.GetName() == name {
return true
}
}
return false
}
func getJavaServiceClassName(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto) string {
serviceName := camelCase(service.GetName())
return fmt.Sprintf("%s", serviceName)
}
func getJavaServiceClassFile(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto) string {
serviceClass := getJavaServiceClassName(file, service)
pkg := getJavaPackage(file)
if pkg == "" {
return fmt.Sprintf("%s.java", serviceClass)
} else {
dir := strings.Replace(pkg, ".", "/", -1)
return fmt.Sprintf("%s/%s.java", dir, serviceClass)
}
}
func getJavaServiceClientClassName(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto) string {
serviceName := camelCase(service.GetName())
name := fmt.Sprintf("%sClient", serviceName)
if containsType(name, file) {
name = fmt.Sprintf("%sTwirpClient", serviceName)
}
return name
}
func getJavaServiceClientClassFile(file *descriptor.FileDescriptorProto, service *descriptor.ServiceDescriptorProto) string {
serviceClass := getJavaServiceClientClassName(file, service)
return getJavaServiceClientClassFileByString(file, serviceClass)
}
func getJavaServiceClientClassFileByString(file *descriptor.FileDescriptorProto, serviceClass string) string {
pkg := getJavaPackage(file)
if pkg == "" {
return fmt.Sprintf("%s.java", serviceClass)
} else {
dir := strings.Replace(pkg, ".", "/", -1)
return fmt.Sprintf("%s/%s.java", dir, serviceClass)
}
}
func getJavaPackage(file *descriptor.FileDescriptorProto) string {
pkg := file.Options.GetJavaPackage()
if pkg != "" {
return pkg
}
return file.GetPackage()
}
func getJavaType(file *descriptor.FileDescriptorProto, name string) string {
pkg := getJavaPackage(file)
multi := file.Options.GetJavaMultipleFiles()
p := strings.LastIndex(name, ".")
typeName := name[p+1:]
if pkg == "" {
return fmt.Sprintf("%s", typeName)
} else if multi {
return fmt.Sprintf("%s.%s", pkg, typeName)
} else {
outerClass := getJavaOuterClassName(file)
return fmt.Sprintf("%s.%s.%s", pkg, outerClass, typeName)
}
}
func camelCase(str string) string {
parts := strings.Split(str, "_")
for i, part := range parts {
runes := []rune(part)
runes[0] = unicode.ToUpper(runes[0])
parts[i] = string(runes)
}
return strings.Join(parts, "")
}
func lowerCamelCase(str string) string {
cc := camelCase(str)
runes := []rune(cc)
runes[0] = unicode.ToLower(runes[0])
return string(runes)
}