-
Notifications
You must be signed in to change notification settings - Fork 281
/
routes.go
89 lines (78 loc) · 1.89 KB
/
routes.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package http
// getDepth will return up to the N'th+1 slash in the path.
// e.g. /foo/bar/baz depth 2 will return /foo/bar
// If a prefix slash is not included, we will prepend the prefix slash,
// e.g. foo/bar/baz depth 2 will return /foo/bar
// If there is insufficient path elements, we will return the input element
func getDepth(path string, depth int64) (ret string) {
// if we wanted no depth, then the basepath is empty
if depth == 0 {
return ""
}
// not sure how this is possible but sure
if len(path) == 0 {
return path
}
if path[0] != '/' {
path = "/" + path
}
var hits int64 = 0
for i, v := range path {
if v == '/' {
hits++
}
if hits == depth+1 {
return path[0:i]
break
}
}
return path
}
type RouteMap map[string][]*Route
func (r RouteMap) FlattenCount() int {
ret := 0
for _, v := range r {
ret += len(v)
}
return ret
}
func (r RouteMap) Flatten() []*Route {
ret := make([]*Route, 0)
for _, v := range r {
ret = append(ret, v...)
}
return ret
}
// GroupRouteDepth will collate the routes into the corresponding depth of path
func GroupRouteDepth(routes []*Route, depth int64) RouteMap {
ret := make(map[string][]*Route)
for _, v := range routes {
basePath := getDepth(string(v.Path), depth)
ret[basePath] = append(ret[basePath], v)
}
return ret
}
// UniqueSource will unique the routes based on the ID
func UniqueSource(routes []*Route) []*Route {
ret := make([]*Route, 0)
seen := make(map[string]interface{})
for _, v := range routes {
if _, ok := seen[v.Source]; ok {
continue
}
seen[v.Source] = struct{}{}
ret = append(ret, v)
}
return ret
}
// FilterSource will only return the routes that exist in the map provided
func FilterSource(routes []*Route, want map[string]interface{}) []*Route {
ret := make([]*Route, 0)
for _, v := range routes {
if _, ok := want[v.Source]; !ok {
continue
}
ret = append(ret, v)
}
return ret
}