-
Notifications
You must be signed in to change notification settings - Fork 2
/
pattern.go
229 lines (180 loc) · 4.88 KB
/
pattern.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
// Package pattern provides a simple regexp pattern matching library majorly
// for constructing URL matchers.
// Patterns in this package follow the follow approach in declaring custom match
// segments.
//
// pattern: /name/{id:[/\d+/]}/log/{date:[/\w+\W+/]}
// pattern: /name/:id
//
//
package pattern
import (
"regexp"
"strings"
)
// Params defines a map of stringed keys and values.
type Params map[string]string
// Matchable defines an interface for matchers.
type Matchable interface {
IsParam() bool
HasHash() bool
Segment() string
Validate(string) bool
}
// Matchers defines a list of machers for validating patterns with.
type Matchers []Matchable
// URIMatcher defines an interface for a URI matcher.
type URIMatcher interface {
Validate(string) (Params, string, bool)
Pattern() string
Priority() int
}
// matchProvider provides a class array-path matcher
type matchProvider struct {
pattern string
matchers Matchers
endless bool
priority int
}
// New returns a new instance of a URIMatcher.
func New(pattern string) URIMatcher {
pattern = addSlash(pattern)
pm := SegmentList(pattern)
m := matchProvider{
priority: CheckPriority(pattern),
pattern: pattern,
matchers: pm,
endless: IsEndless(pattern),
}
return &m
}
// Priority returns the priority status of this giving pattern.
func (m *matchProvider) Priority() int {
return m.priority
}
// Pattern returns the pattern string for this matcher.
func (m *matchProvider) Pattern() string {
return m.pattern
}
// Validate returns true/false if the giving string matches the pattern, returning
// a map of parameters match against segments of the pattern.
func (m *matchProvider) Validate(path string) (Params, string, bool) {
path = addSlash(path)
stripped := stripAndClean(path)
hashedSrc := stripAndCleanButHash(path)
cleaned := cleanPath(stripped)
src := splitPattern(cleaned)
srclen := len(src)
total := len(m.matchers)
if !m.endless && total != srclen {
return nil, "", false
}
if m.endless && total > srclen {
return nil, "", false
}
var state bool
param := make(Params)
var lastIndex int
var doneHash bool
for index, v := range m.matchers {
lastIndex = index
if v.HasHash() {
doneHash = true
}
if v.Validate(src[index]) {
if v.IsParam() {
param[v.Segment()] = src[index]
}
state = true
continue
} else {
state = false
break
}
}
if lastIndex+1 == srclen {
return param, "", state
}
remPath := strings.Join(src[lastIndex+1:], "/")
if doneHash || !strings.Contains(hashedSrc, "#") {
return param, addSlash(remPath), state
}
var rems []string
fragment := SegmentList(hashedSrc)[lastIndex+1:]
for _, item := range fragment {
if item.HasHash() {
hashed := "#" + item.Segment()
rems = append(rems, hashed)
continue
}
rems = append(rems, item.Segment())
}
return param, addSlash(strings.Join(rems, "/")), state
}
//==============================================================================
// SegmentList returns list of SegmentMatcher which implements the Matchable
// interface, with each made of each segment of the pattern.
func SegmentList(pattern string) Matchers {
pattern = stripAndCleanButHash(pattern)
var set Matchers
if hashIndex := strings.Index(pattern, "#"); hashIndex != -1 {
if hashIndex == 0 {
pattern = strings.Join([]string{"/", pattern}, "")
} else {
last := pattern[hashIndex-1 : hashIndex]
if string(last[0]) != "/" {
splits := strings.Split(pattern, "#")
pattern = strings.Join([]string{splits[0], "/#", splits[1]}, "")
}
}
}
for _, val := range splitPattern(pattern) {
set = append(set, Segment(val))
}
return set
}
//==============================================================================
// SegmentMatcher defines a single piece of pattern to be matched against.
type SegmentMatcher struct {
*regexp.Regexp
original string
param bool
hashed bool
}
// Segment returns a Matchable for a specific part of a pattern eg. :name, age,
// {id:[\\d+]}.
func Segment(segment string) Matchable {
if segment == "*" {
segment = "/*"
}
hashed := strings.HasPrefix(segment, "#")
if hashed {
segment = segment[1:]
}
id, rx, b := YankSpecial(segment)
mrk := regexp.MustCompile(rx)
sm := SegmentMatcher{
Regexp: mrk,
original: id,
param: b,
hashed: hashed,
}
return &sm
}
// HasHash returns true/false if this segment hash the hash.
func (s *SegmentMatcher) HasHash() bool {
return s.hashed
}
// IsParam returns true/false if the segment is also a paramter.
func (s *SegmentMatcher) IsParam() bool {
return s.param
}
// Segment returns the original string that makes up this segment matcher.
func (s *SegmentMatcher) Segment() string {
return s.original
}
// Validate validates the value against the matcher.
func (s *SegmentMatcher) Validate(m string) bool {
return s.MatchString(m)
}
//==============================================================================