-
Notifications
You must be signed in to change notification settings - Fork 3
/
parameters.go
executable file
·86 lines (76 loc) · 2.08 KB
/
parameters.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
// Copyright 2016 José Santos <henrique_1609@me.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package router
import "strings"
// Parameter holds the parameters matched in the route
type Parameter struct {
*routeNode // matched node
path string // url path given
wildcard int // size of the wildcard match in the end of the string
}
// IndexOf returns the index of the argument by name
func (vv *Parameter) IndexOf(name string) int {
if i, has := vv.names[name]; has {
return i
}
return -1
}
// Len returns number arguments matched in the provided URL
func (vv *Parameter) Len() int {
return len(vv.names)
}
// ByName returns the url parameter by name
func (vv *Parameter) ByName(name string) string {
if i, has := vv.names[name]; has {
return vv.findParam(i)
}
return ""
}
// findParam walks up the matched node looking for parameters returns the last parameter
func (vv *Parameter) findParam(idx int) (param string) {
var (
curIndex = len(vv.names) - 1
urlPath = vv.path
pathLen = len(vv.path)
curNode = vv.routeNode
)
if curNode.text[0] == '*' {
pathLen -= vv.wildcard
if curIndex == idx {
param = urlPath[pathLen:]
return
}
curIndex--
curNode = curNode.parent
}
for curNode != nil {
if curNode.text[0] == ':' {
nextSlash := strings.LastIndexByte(urlPath, '/')
if nextSlash == -1 {
return
}
pathLen = nextSlash + 1
if curIndex == idx {
param = urlPath[pathLen:]
return
}
curIndex--
} else {
pathLen -= len(curNode.text)
}
urlPath = urlPath[0:pathLen]
curNode = curNode.parent
}
return
}