forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_method_parser.go
285 lines (237 loc) · 6.6 KB
/
controller_method_parser.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package mvc
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"unicode"
"github.com/kataras/iris/v12/core/router"
"github.com/kataras/iris/v12/macro"
)
const (
tokenBy = "By"
tokenWildcard = "Wildcard" // "ByWildcard".
)
// word lexer, not characters.
type methodLexer struct {
words []string
cur int
}
func newMethodLexer(s string) *methodLexer {
l := new(methodLexer)
l.reset(s)
return l
}
/*
var allowedCapitalWords = map[string]struct{}{
"ID": {},
"JSON": {},
}
*/
func (l *methodLexer) reset(s string) {
l.cur = -1
var words []string
if s != "" {
end := len(s)
start := -1
// outter:
for i, n := 0, end; i < n; i++ {
c := rune(s[i])
if unicode.IsUpper(c) {
// it doesn't count the last uppercase
if start != -1 {
/*
for allowedCapitalWord := range allowedCapitalWords {
capitalWordEnd := i + len(allowedCapitalWord) // takes last char too, e.g. ReadJSON, we need the JSON.
if len(s) >= capitalWordEnd {
word := s[i:capitalWordEnd]
if word == allowedCapitalWord {
words = append(words, word)
i = capitalWordEnd
start = i
continue outter
}
}
}
*/
end = i
words = append(words, s[start:end])
}
start = i
continue
}
end = i + 1
}
if end > 0 && len(s) >= end {
words = append(words, s[start:])
}
}
l.words = words
}
func (l *methodLexer) next() (w string) {
cur := l.cur + 1
if w = l.peek(cur); w != "" {
l.cur++
}
return
}
func (l *methodLexer) skip() {
if cur := l.cur + 1; cur < len(l.words) {
l.cur = cur
} else {
l.cur = len(l.words) - 1
}
}
func (l *methodLexer) peek(idx int) string {
if idx < len(l.words) {
return l.words[idx]
}
return ""
}
func (l *methodLexer) peekNext() (w string) {
return l.peek(l.cur + 1)
}
func genParamKey(argIdx int) string {
return "param" + strconv.Itoa(argIdx) // param0, param1, param2...
}
type methodParser struct {
lexer *methodLexer
fn reflect.Method
macros *macro.Macros
customPathWordFunc CustomPathWordFunc
}
func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) bool, wordFunc CustomPathWordFunc) (method, path string, err error) {
if skipper(fn.Name) {
return "", "", errSkip
}
p := &methodParser{
fn: fn,
lexer: newMethodLexer(fn.Name),
macros: macros,
customPathWordFunc: wordFunc,
}
return p.parse()
}
func methodTitle(httpMethod string) string {
httpMethodFuncName := strings.Title(strings.ToLower(httpMethod))
return httpMethodFuncName
}
var errSkip = errors.New("skip")
var allMethods = append(router.AllMethods[0:], []string{"ALL", "ANY"}...)
// CustomPathWordFunc describes the function which can be passed
// through `Application.SetCustomPathWordFunc` to customize
// the controllers method parsing.
type CustomPathWordFunc func(path, w string, wordIndex int) string
func addPathWord(path, w string) string {
if path[len(path)-1] != '/' {
path += "/"
}
path += strings.ToLower(w)
return path
}
func (p *methodParser) parse() (method, path string, err error) {
funcArgPos := 0
path = "/"
// take the first word and check for the method.
w := p.lexer.next()
for _, httpMethod := range allMethods {
possibleMethodFuncName := methodTitle(httpMethod)
if strings.Index(w, possibleMethodFuncName) == 0 {
method = httpMethod
break
}
}
if method == "" {
// this is not a valid method to parse, we just skip it,
// it may be used for end-dev's use cases.
return "", "", errSkip
}
wordIndex := 0
for {
w := p.lexer.next()
if w == "" {
break
}
if w == tokenBy {
funcArgPos++ // starting with 1 because in typ.NumIn() the first is the struct receiver.
// No need for these:
// ByBy will act like /{param:type}/{param:type} as users expected
// if func input arguments are there, else act By like normal path /by.
//
// if p.lexer.peekPrev() == tokenBy || typ.NumIn() == 1 { // ByBy, then act this second By like a path
// a.relPath += "/" + strings.ToLower(w)
// continue
// }
if path, funcArgPos, err = p.parsePathParam(path, w, funcArgPos); err != nil {
return "", "", err
}
continue
}
// custom static path.
if p.customPathWordFunc != nil {
path = p.customPathWordFunc(path, w, wordIndex)
} else {
// default static path.
path = addPathWord(path, w)
}
wordIndex++
}
return
}
func (p *methodParser) parsePathParam(path string, w string, funcArgPos int) (string, int, error) {
typ := p.fn.Type
if typ.NumIn() <= funcArgPos {
// By found but input arguments are not there, so act like /by path without restricts.
path = addPathWord(path, w)
return path, funcArgPos, nil
}
var (
paramKey = genParamKey(funcArgPos) // argfirst, argsecond...
m = p.macros.GetMaster() // default (String by-default)
trailings = p.macros.GetTrailings()
)
// string, int...
goType := typ.In(funcArgPos).Kind()
nextWord := p.lexer.peekNext()
if nextWord == tokenWildcard {
p.lexer.skip() // skip the Wildcard word.
if len(trailings) == 0 {
return "", 0, errors.New("no trailing path parameter found")
}
m = trailings[0]
} else {
// validMacros := p.macros.LookupForGoType(goType)
// instead of mapping with a reflect.Kind which has its limitation,
// we map the param types with a go type as a string,
// so custom structs such as "user" can be mapped to a macro with indent || alias == "user".
m = p.macros.Get(strings.ToLower(goType.String()))
if m == nil {
if typ.NumIn() > funcArgPos {
// has more input arguments but we are not in the correct
// index now, maybe the first argument was an `iris/context.Context`
// so retry with the "funcArgPos" incremented.
//
// the "funcArgPos" will be updated to the caller as well
// because we return it among the path and the error.
return p.parsePathParam(path, w, funcArgPos+1)
}
return "", 0, fmt.Errorf("invalid syntax: the standard go type: %s found in controller's function: %s at position: %d does not match any valid macro", goType, p.fn.Name, funcArgPos)
}
}
// /{argfirst:path}, /{argfirst:int64}...
if path[len(path)-1] != '/' {
path += "/"
}
path += fmt.Sprintf("{%s:%s}", paramKey, m.Indent())
if nextWord == "" && typ.NumIn() > funcArgPos+1 {
// By is the latest word but func is expected
// more path parameters values, i.e:
// GetBy(name string, age int)
// The caller (parse) doesn't need to know
// about the incremental funcArgPos because
// it will not need it.
return p.parsePathParam(path, nextWord, funcArgPos+1)
}
return path, funcArgPos, nil
}