-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
116 lines (105 loc) · 2.3 KB
/
util.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
package main
import (
"fmt"
"strings"
"github.com/mingmxren/protokit"
)
func Indent(s string, width int) string {
sb := new(strings.Builder)
sp := strings.Split(s, "\n")
for i, s := range sp {
if len(s) > 0 {
sb.WriteString(strings.Repeat(" ", width))
}
sb.WriteString(s)
if i != len(sp)-1 {
sb.WriteString("\n")
}
}
r := sb.String()
return r
}
func WithComments(s string, comments *protokit.Comment, baseIndex int) string {
sb := new(strings.Builder)
for _, c := range comments.Detached {
sb.WriteString(Indent(fmt.Sprintf("/*\n%s\n */\n\n", Indent(c, 2)), baseIndex))
}
if len(comments.Leading) > 0 {
sb.WriteString(Indent(fmt.Sprintf("\n\n/*\n%s\n */\n", Indent(comments.Leading, 2)), baseIndex))
}
if len(s) > 0 && s[len(s)-1] == '\n' {
sb.WriteString(s[:len(s)-1])
}
if len(comments.Trailing) > 0 {
sb.WriteString(fmt.Sprintf(" /* %s */\n", comments.Trailing))
} else {
sb.WriteString("\n")
}
return sb.String()
}
func GetBuiltinTypeName(field *protokit.PKFieldDescriptor) string {
builtinTypes := map[int32]string{
1: "double",
2: "float",
3: "int64",
4: "uint64",
5: "int32",
6: "fixed64",
7: "fixed64",
8: "bool",
9: "string",
// 10: "TYPE_GROUP",
// 11: "TYPE_MESSAGE",
12: "bytes",
13: "uint32",
// 14: "TYPE_ENUM",
15: "sfixed32",
16: "sfixed64",
17: "sint32",
18: "sint64",
}
if stringType, ok := builtinTypes[int32(field.GetType())]; ok {
return stringType
}
return ""
}
func GetStringType(field *protokit.PKFieldDescriptor) string {
name := GetBuiltinTypeName(field)
if name != "" {
return name
}
return field.GetTypeName()
}
func FindMessage(files []*protokit.PKFileDescriptor, name string) *protokit.PKDescriptor {
for _, f := range files {
for _, m := range f.GetMessages() {
if strings.HasPrefix(name, ".") {
name = name[1:]
}
if m.GetFullName() == name {
return m
}
}
}
return nil
}
func FindEnum(files []*protokit.PKFileDescriptor, name string) *protokit.PKEnumDescriptor {
for _, f := range files {
for _, m := range f.GetEnums() {
if strings.HasPrefix(name, ".") {
name = name[1:]
}
if m.GetFullName() == name {
return m
}
}
}
return nil
}
func LastPart(s string, split string) string {
i := strings.LastIndex(s, split)
if i == -1 {
return s
}
return s[i+len(split):]
}