-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
57 lines (51 loc) · 1.46 KB
/
paths.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
package dispatch
import (
"fmt"
"strings"
)
// PathVars is an alias for map[string]string, used for captured path variables.
type PathVars map[string]string
// An APIPath represents a specified path and method, such as GET/users/{uuid}.
type APIPath struct {
PathParts []string
Method string
}
// NewAPIPath creates an APIPath object from a path string, in the format
// GET/users/{uuid}.
func NewAPIPath(path string) (*APIPath, error) {
parts := strings.Split(path, "/")
// path must have at least a method and one slash
if len(parts) < 2 {
return nil, fmt.Errorf("Invalid path: %s", path)
}
return &APIPath{
Method: parts[0],
PathParts: parts[1:],
}, nil
}
// Match tests an APIPath against a path string, and returns a map of path
// variables and a boolean representing whether it was a match.
func (a *APIPath) Match(method, path string) (pathVars PathVars, ok bool) {
if method != a.Method {
return
}
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
parts := strings.Split(path, "/")
if len(parts) != len(a.PathParts) {
return
}
pathVars = make(map[string]string)
for i, p := range parts {
apiPart := a.PathParts[i]
if len(apiPart) > 1 && apiPart[0] == '{' && apiPart[len(apiPart)-1] == '}' {
// This path part is a path variable
pathVars[apiPart[1:len(apiPart)-1]] = p
} else if p != apiPart {
// If not a path variable, and they don't match, this path is incorrect
return nil, false
}
}
return pathVars, true
}