-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.go
83 lines (74 loc) · 1.55 KB
/
tokens.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
package router
import (
"github.com/iesreza/gutil/log"
"regexp"
"strings"
)
var matches = map[string]*regexp.Regexp{
"a": regexp.MustCompile(`[a-zA-Z0-9\.-]+`),
"i": regexp.MustCompile(`[0-9]+`),
"id": regexp.MustCompile(`[0-9]+`),
"s": regexp.MustCompile(`[a-zA-Z0-9\.-]+`),
}
func tokenize(input string) []token {
var tokens []token
parts := strings.Split(input, "/")
for _, item := range parts {
tk := token{}
if len(item) > 3 {
if item[0] == '~' {
tk.lazy = true
}
if (item[0] == '[' || item[1] == '[') && item[len(item)-1] == ']' {
meetStart := false
meetDot := false
varType := ""
for i := 0; i < len(item)-1; i++ {
if !meetStart {
if item[i] == '[' {
meetStart = true
}
continue
}
if item[i] == ':' {
meetDot = true
continue
}
if !meetDot {
varType += string(item[i])
} else {
tk.varName += string(item[i])
}
}
if val, ok := matches[varType]; ok {
tk.match = val
} else {
log.Critical("Invalid variable type \"%s\" in url", item)
}
tk.matchType = 1
} else {
tk.match = item
tk.matchType = 0
}
} else {
tk.match = item
tk.matchType = 0
}
tokens = append(tokens, tk)
}
return tokens
}
type token struct {
match interface{}
varName string
matchType uint
lazy bool
}
func (t *token) isMatch(input string) bool {
if t.matchType == 0 {
return input == t.match.(string)
} else if t.matchType == 1 {
return t.match.(*regexp.Regexp).MatchString(input)
}
return false
}