-
Notifications
You must be signed in to change notification settings - Fork 1
/
findentity.go
40 lines (35 loc) · 1005 Bytes
/
findentity.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
// Copyright 2015 Luke Shumaker
package httpentity
import "strings"
func pathSplit(upath string) (string, string) {
parts := strings.SplitN(upath, "/", 2)
switch len(parts) {
case 1:
return parts[0], ""
case 2:
return parts[0], parts[1]
}
panic("not reached")
}
// Takes the normalized path without the leading slash, and with or
// without a trailing slash.
func (router *Router) findEntity(upath string, request Request) (Entity, Response) {
var group EntityGroup = router.Root
for {
nameChild, nameGrandchildren := pathSplit(upath)
entity := group.Subentity(nameChild, request)
if entity == nil {
return nil, group.SubentityNotFound(nameChild, request)
} else if nameGrandchildren == "" {
return entity, Response{}
} else {
if newgroup, ok := entity.(EntityGroup); ok {
group = newgroup
} else {
nameGrandchild, _ := pathSplit(upath)
return nil, group.SubentityNotFound(nameChild+"/"+nameGrandchild, request)
}
}
upath = nameGrandchildren
}
}