-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
79 lines (71 loc) · 1.62 KB
/
node.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
package mux
import (
"context"
"net/http"
"strconv"
)
type node struct {
name string
typ string
route string
handlers map[string]http.Handler
child []node
}
func (n *node) match(path string, offset uint, r *http.Request) (part string, remain string, req *http.Request) {
// Nil nodes never match.
if n == nil {
return "", "", r
}
// wildcards are a special case that always match the entire remainder of the
// path.
if n.typ == typWild {
r = addValue(r, n.name, n.typ, path, offset, path)
return path, "", r
}
part, remain = nextPart(path)
switch n.typ {
case typStatic:
if n.name == part {
return part, remain, r
}
return "", path, r
case typString:
r = addValue(r, n.name, n.typ, part, offset, part)
return part, remain, r
case typUint:
v, err := strconv.ParseUint(part, 10, 64)
if err != nil {
return "", path, r
}
r = addValue(r, n.name, n.typ, part, offset, v)
return part, remain, r
case typInt:
v, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return "", path, r
}
r = addValue(r, n.name, n.typ, part, offset, v)
return part, remain, r
case typFloat:
v, err := strconv.ParseFloat(part, 64)
if err != nil {
return "", path, r
}
r = addValue(r, n.name, n.typ, part, offset, v)
return part, remain, r
}
panic("unknown type")
}
func addValue(r *http.Request, name, typ, raw string, offset uint, val interface{}) *http.Request {
if name != "" {
pinfo := ParamInfo{
Value: val,
Raw: raw,
Name: name,
Type: typ,
offset: offset,
}
return r.WithContext(context.WithValue(r.Context(), ctxParam(name), pinfo))
}
return r
}