-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_counter.go
66 lines (58 loc) · 1.45 KB
/
route_counter.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
package cmp
import (
"fmt"
"regexp"
"strings"
"sync"
)
// MakeRoute translates route with * wildcards into a regexp. It adds start/end
// of line asserts, changes * into any characters except when in used for lists
// (like /accounts/*) - in such case it ensures there is no more `/` characters.
// This is not ideal and requires checking routes in correct order but is enough
// for horizon-cmp.
// More here: https://regex101.com/r/EhBRtS/1
func MakeRoute(route string) *Route {
name := route
route = "^" + route
route = strings.ReplaceAll(route, "*", "[^?/]+") // everything except `/` and `?`
route = route + "[?/]?[^/]*$" // ? or / or nothing and then everything except `/``
return &Route{
name: name,
regexp: regexp.MustCompile(route),
}
}
type Route struct {
name string
regexp *regexp.Regexp
counter int
}
type Routes struct {
List []*Route
unmatched []string
mutex sync.Mutex
}
func (r *Routes) Count(path string) {
for _, route := range r.List {
if route.regexp.Match([]byte(path)) {
r.mutex.Lock()
route.counter++
r.mutex.Unlock()
return
}
}
r.mutex.Lock()
r.unmatched = append(r.unmatched, path)
r.mutex.Unlock()
}
func (r *Routes) Print() {
r.mutex.Lock()
defer r.mutex.Unlock()
fmt.Println("Routes coverage:")
for _, route := range r.List {
fmt.Println(route.counter, route.name)
}
fmt.Println("Unmatched paths:")
for _, path := range r.unmatched {
fmt.Println(path)
}
}