-
Notifications
You must be signed in to change notification settings - Fork 701
/
template.go
86 lines (75 loc) · 1.65 KB
/
template.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
package main
import (
"bytes"
_ "embed"
"strings"
"text/template"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
//go:embed fieldmask.go.tpl
var tpl string
type service struct {
Name string
FullName string
FilePath string
Message []*message
}
type message struct {
In bool
Out bool
RequestName string
ResponseName string
UpdateInFields []*field
UpdateOutFields []*field
IdentifyFieldGoName string
IdentifyField string
}
type field struct {
UnderLineName string `json:"UnderLineName"`
DotName string `json:"DotName"`
}
type MsgType uint32
const (
MsgTypeIn MsgType = 1
MsgTypeOut MsgType = 2
)
func (s *service) execute() string {
buf := new(bytes.Buffer)
tmpl, err := template.New("fieldmask").Parse(strings.TrimSpace(tpl))
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, s); err != nil {
panic(err)
}
return buf.String()
}
func (msg *message) RemovePrefix() {
for _, f := range msg.UpdateInFields {
f.DotName = removePrefix(f.DotName)
f.UnderLineName = toCamelCase(removePrefix(f.UnderLineName))
}
for _, f := range msg.UpdateOutFields {
f.DotName = removePrefix(f.DotName)
f.UnderLineName = toCamelCase(removePrefix(f.UnderLineName))
}
}
func removePrefix(name string) string {
if name == "" {
return name
}
if name[0] == '.' || name[0] == '_' {
return name[1:]
}
return name
}
func toCamelCase(name string) string {
words := strings.FieldsFunc(name, func(r rune) bool {
return r == '.' || r == '_'
})
for i := range words {
words[i] = cases.Title(language.English).String(words[i])
}
return strings.Join(words, "")
}