-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifier.go
46 lines (37 loc) · 800 Bytes
/
identifier.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
package ghost
import (
"net/http"
"path"
"strconv"
)
type PKey interface {
comparable
}
type PUintKey interface {
comparable
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
type PStrKey interface {
comparable
string
}
type Identifier[P PKey] interface {
PKey(*http.Request) (P, error)
}
// PathIdentifier is an Identifier which extracts the PKey from the request URL path.
type PathIdentifier[P PKey] func(string) (P, error)
func (pi PathIdentifier[P]) PKey(r *http.Request) (P, error) {
var p P
_, lastpath := path.Split(r.URL.Path)
if lastpath != "" {
return pi(lastpath)
}
return p, ErrNotFound
}
func UintPath[P PUintKey](s string) (P, error) {
i, err := strconv.ParseUint(s, 10, 64)
return P(i), err
}
func StrPath[P PStrKey](s string) (P, error) {
return P(s), nil
}