-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
54 lines (43 loc) · 1.12 KB
/
params.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
// Copyright 2022 Sylvain Müller. All rights reserved.
// Mount of this source code is governed by a Apache-2.0 license that can be found
// at https://github.com/tigerwill90/fox/blob/master/LICENSE.txt.
package fox
import "context"
type ctxKey struct{}
// paramsKey is the key that holds the Params in a context.Context.
var paramsKey = ctxKey{}
type Param struct {
Key string
Value string
}
type Params []Param
// Get the matching wildcard segment by name.
func (p Params) Get(name string) string {
for i := range p {
if p[i].Key == name {
return p[i].Value
}
}
return ""
}
// Has checks whether the parameter exists by name.
func (p Params) Has(name string) bool {
for i := range p {
if p[i].Key == name {
return true
}
}
return false
}
// clone make a copy of Params.
func (p Params) clone() Params {
cloned := make(Params, len(p))
copy(cloned, p)
return cloned
}
// ParamsFromContext is a helper to retrieve params from context.Context when a http.Handler
// is registered using WrapF or WrapH.
func ParamsFromContext(ctx context.Context) Params {
p, _ := ctx.Value(paramsKey).(Params)
return p
}