-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
71 lines (65 loc) · 1.46 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
package main
import (
"fmt"
"github.com/mingmxren/protokit"
"strings"
)
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 GetStringType(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 field.GetTypeName()
}