forked from derekdowling/go-json-spec-handler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
match.go
51 lines (44 loc) · 1007 Bytes
/
match.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
package pat
import (
"sort"
"goji.io/internal"
"goji.io/pattern"
"golang.org/x/net/context"
)
type match struct {
context.Context
pat *Pattern
matches []string
}
func (m match) Value(key interface{}) interface{} {
switch key {
case pattern.AllVariables:
var vs map[pattern.Variable]interface{}
if vsi := m.Context.Value(key); vsi == nil {
if len(m.pat.pats) == 0 {
return nil
}
vs = make(map[pattern.Variable]interface{}, len(m.matches))
} else {
vs = vsi.(map[pattern.Variable]interface{})
}
for _, p := range m.pat.pats {
vs[p.name] = m.matches[p.idx]
}
return vs
case internal.Path:
if len(m.matches) == len(m.pat.pats)+1 {
return m.matches[len(m.matches)-1]
}
return ""
}
if k, ok := key.(pattern.Variable); ok {
i := sort.Search(len(m.pat.pats), func(i int) bool {
return m.pat.pats[i].name >= k
})
if i < len(m.pat.pats) && m.pat.pats[i].name == k {
return m.matches[m.pat.pats[i].idx]
}
}
return m.Context.Value(key)
}