-
Notifications
You must be signed in to change notification settings - Fork 33
/
helpers.go
230 lines (201 loc) · 4.7 KB
/
helpers.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package schematic
import (
"bytes"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
"unicode"
"unicode/utf8"
)
var helpers = template.FuncMap{
"initialCap": initialCap,
"initialLow": initialLow,
"methodCap": methodCap,
"asComment": asComment,
"fieldName": fieldName,
"fieldTag": fieldTag,
"params": params,
"requestParams": requestParams,
"args": args,
"values": values,
"goType": goType,
"linkGoType": linkGoType,
"returnType": returnType,
"defineCustomType": defineCustomType,
"paramType": paramType,
}
var (
newlines = regexp.MustCompile(`(?m:\s*$)`)
acronyms = regexp.MustCompile(`(Url|Http|Id|Io|Ip|Ike|Uuid|Api|Uri|Ssl|Cname|Oauth|Otp|Cidr|Nat|Vpn)$`)
camelcase = regexp.MustCompile(`(?m)[-.$/:_{}\s]+`)
)
func goType(p *Schema) string {
return p.GoType()
}
func linkGoType(l *Link) string {
t, _ := l.GoType()
return t
}
func required(n string, def *Schema) bool {
return contains(n, def.Required)
}
func fieldTag(n string, required bool) string {
return fmt.Sprintf("`%s %s`", jsonTag(n, required), urlTag(n, required))
}
func fieldName(name string) string {
fieldName := initialCap(name)
if strings.HasSuffix(name, "?") {
fieldName = "Is" + strings.TrimSuffix(fieldName, "?")
}
return fieldName
}
func jsonTag(n string, required bool) string {
tags := []string{n}
if !required {
tags = append(tags, "omitempty")
}
return fmt.Sprintf("json:\"%s\"", strings.Join(tags, ","))
}
func urlTag(n string, required bool) string {
tags := []string{n}
if !required {
tags = append(tags, "omitempty")
}
tags = append(tags, "key")
return fmt.Sprintf("url:\"%s\"", strings.Join(tags, ","))
}
func contains(n string, r []string) bool {
for _, r := range r {
if r == n {
return true
}
}
return false
}
func initialCap(ident string) string {
if ident == "" {
panic("blank identifier")
}
return depunct(ident, true)
}
func methodCap(ident string) string {
return initialCap(strings.ToLower(ident))
}
func initialLow(ident string) string {
if ident == "" {
panic("blank identifier")
}
return depunct(ident, false)
}
func depunct(ident string, initialCap bool) string {
matches := camelcase.Split(ident, -1)
for i, m := range matches {
if initialCap || i > 0 {
m = capFirst(m)
}
matches[i] = acronyms.ReplaceAllStringFunc(m, func(c string) string {
if len(c) > 4 {
return strings.ToUpper(c[:2]) + c[2:]
}
return strings.ToUpper(c)
})
}
return strings.Join(matches, "")
}
func capFirst(ident string) string {
r, n := utf8.DecodeRuneInString(ident)
return string(unicode.ToUpper(r)) + ident[n:]
}
func asComment(c string) string {
var buf bytes.Buffer
const maxLen = 70
r := []rune(c)
for len(r) > 0 {
line := r
if len(line) < maxLen {
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
break
}
line = line[:maxLen]
si := lastIndex(line, func(r rune) bool {
return unicode.IsSpace(r)
})
if si != -1 {
line = line[:si]
}
fmt.Fprintf(&buf, "// %s\n", removeNewlines(line))
r = r[len(line):]
if si != -1 {
r = r[1:]
}
}
return buf.String()
}
func values(n string, s *Schema, l *Link) string {
v := s.Values(n, l)
return strings.Join(v, ", ")
}
func params(name string, l *Link) string {
var p []string
order, params := l.Parameters(name)
for _, n := range order {
p = append(p, fmt.Sprintf("%s %s", initialLow(n), params[n]))
}
return strings.Join(p, ", ")
}
func requestParams(l *Link) string {
_, params := l.Parameters("")
if strings.ToUpper(l.Method) == "DELETE" {
return ""
}
p := []string{""}
if _, ok := params["o"]; ok {
p = append(p, "o")
} else {
p = append(p, "nil")
}
if _, ok := params["lr"]; ok {
p = append(p, "lr")
} else if strings.ToUpper(l.Method) == "GET" {
p = append(p, "nil")
}
return strings.Join(p, ", ")
}
func args(h *HRef) string {
return strings.Join(h.Order, ", ")
}
func sortedKeys(m map[string]*Schema) (keys []string) {
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return
}
func returnType(name string, s *Schema, l *Link) string {
if defineCustomType(s, l) {
return initialCap(fmt.Sprintf("%s-%s-Result", name, l.Title))
}
return initialCap(name)
}
func paramType(name string, l *Link) string {
if l.AcceptsCustomType() {
return initialCap(fmt.Sprintf("%s-%s-Opts", name, l.Title))
}
return initialCap(name)
}
func defineCustomType(s *Schema, l *Link) bool {
return l.TargetSchema != nil && l.TargetSchema != s
}
func removeNewlines(s []rune) string {
return strings.Replace(string(s), "\n", "\n// ", -1)
}
func lastIndex(s []rune, f func(rune) bool) int {
for i := len(s) - 1; i > 0; i-- {
if f(s[i]) {
return i
}
}
return -1
}