-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclearpath.go
130 lines (112 loc) · 2.66 KB
/
clearpath.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package hit
import (
"fmt"
"strings"
"io"
"github.com/google/go-cmp/cmp"
)
type pathPart struct {
Func string
Arguments []interface{}
}
type callPath []pathPart
func (cleanPath callPath) Push(name string, arguments []interface{}) callPath {
if arguments == nil {
arguments = []interface{}{}
}
return append(cleanPath, pathPart{
Func: name,
Arguments: arguments,
})
}
func newCallPath(name string, arguments []interface{}) callPath {
if arguments == nil {
arguments = []interface{}{}
}
return []pathPart{
{
Func: name,
Arguments: arguments,
},
}
}
func funcComparer(x, y func(Hit)) bool {
return fmt.Sprintf("%p", x) == fmt.Sprintf("%p", y)
}
func ioReaderComparer(x, y io.Reader) bool {
return fmt.Sprintf("%p", x) == fmt.Sprintf("%p", y)
}
func (cleanPath callPath) Contains(needle callPath) bool {
haySize := len(cleanPath)
needleSize := len(needle)
if needleSize > haySize {
return false
}
haySize = needleSize
for i := 0; i < haySize; i++ {
if cleanPath[i].Func != needle[i].Func {
return false
}
hayArgSize := len(cleanPath[i].Arguments)
needleArgSize := len(needle[i].Arguments)
if hayArgSize > needleArgSize {
hayArgSize = needleArgSize
}
if !cmp.Equal(cleanPath[i].Arguments[:hayArgSize], needle[i].Arguments,
cmp.Comparer(funcComparer),
cmp.Comparer(ioReaderComparer),
) {
return false
}
}
return true
}
func (cleanPath callPath) Equal(b callPath) bool {
aSize := len(cleanPath)
bSize := len(b)
if bSize != aSize {
return false
}
for i := 0; i < aSize; i++ {
if cleanPath[i].Func != b[i].Func {
return false
}
aArgSize := len(cleanPath[i].Arguments)
bArgSize := len(b[i].Arguments)
if aArgSize > bArgSize {
aArgSize = bArgSize
}
if !cmp.Equal(cleanPath[i].Arguments[:aArgSize], b[i].Arguments,
cmp.Comparer(funcComparer),
cmp.Comparer(ioReaderComparer),
) {
return false
}
}
return true
}
func (cleanPath callPath) CallString(withArguments bool) string {
parts := make([]string, len(cleanPath))
for i := range cleanPath {
var args []string
if withArguments {
args = make([]string, len(cleanPath[i].Arguments))
for i, argument := range cleanPath[i].Arguments {
switch v := argument.(type) {
case []byte:
values := make([]string, len(v))
for j, b := range v {
values[j] = fmt.Sprintf("%#v", b)
}
args[i] = fmt.Sprintf("[]uint8{%s}", strings.Join(values, ", "))
case float32, float64:
args[i] = fmt.Sprintf("%#f", argument)
default:
args[i] = fmt.Sprintf("%#v", argument)
}
}
}
parts[i] = fmt.Sprintf("%s(%s)", cleanPath[i].Func, strings.Join(args, ", "))
}
return strings.Join(parts, ".")
}