forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclearpath.go
89 lines (75 loc) · 1.93 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
package hit
import (
"fmt"
"strings"
"github.com/google/go-cmp/cmp"
)
type pathPart struct {
Func string
Arguments []interface{}
}
type clearPath []pathPart
func (cleanPath clearPath) Push(name string, arguments []interface{}) clearPath {
if arguments == nil {
arguments = []interface{}{}
}
return append(cleanPath, pathPart{
Func: name,
Arguments: arguments,
})
}
func newClearPath(name string, arguments []interface{}) clearPath {
if arguments == nil {
arguments = []interface{}{}
}
return []pathPart{
{
Func: name,
Arguments: arguments,
},
}
}
func funcComparer(x, y func(Hit)) bool {
// return runtime.FuncForPC(reflect.ValueOf(x).Pointer()).Name() == runtime.FuncForPC(reflect.ValueOf(y).Pointer()).Name()
return fmt.Sprintf("%p", x) == fmt.Sprintf("%p", y)
}
func (cleanPath clearPath) Contains(needle clearPath) 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)) {
return false
}
}
return true
}
func (cleanPath clearPath) String() string {
parts := make([]string, len(cleanPath))
for i := range cleanPath {
parts[i] = cleanPath[i].Func
}
return strings.Join(parts, ".")
}
func (cleanPath clearPath) CallString() string {
parts := make([]string, len(cleanPath))
for i := range cleanPath {
args := make([]string, len(cleanPath[i].Arguments))
for i, argument := range cleanPath[i].Arguments {
args[i] = fmt.Sprintf("%#v", argument)
}
parts[i] = fmt.Sprintf("%s(%s)", cleanPath[i].Func, strings.Join(args, ", "))
}
return strings.Join(parts, ".")
}