-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct-matcher.go
117 lines (97 loc) · 2.27 KB
/
struct-matcher.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
package extra
import (
"fmt"
"reflect"
"go.uber.org/mock/gomock"
)
// StMatcher interface improved to add functions.
type StMatcher interface {
gomock.Matcher
// Add a matcher for a specific field
Field(fName string, match interface{}) StMatcher
}
type sStorage struct {
match interface{}
fName string
}
type structMatcher struct {
fields []*sStorage
}
func (f *structMatcher) String() string {
str := ""
// Loop over all fields
for in, v := range f.fields {
// Add separator for display
if in > 0 {
str += ", "
}
str += fmt.Sprintf("field %s", v.fName)
// Try to cast to a matcher interface
m, ok := v.match.(gomock.Matcher)
// Check if cast is ok
if ok {
str += fmt.Sprintf(" must match %s", m.String())
} else {
str += fmt.Sprintf(" must be equal to %v", v.match)
}
}
return str
}
func (f *structMatcher) Field(fName string, match interface{}) StMatcher {
// Check if field name exists
if fName == "" {
return f
}
// Field name exists => add data
f.fields = append(f.fields, &sStorage{fName: fName, match: match})
// Return
return f
}
func (f *structMatcher) Matches(x interface{}) bool {
// Check if x is nil
if x == nil {
return false
}
// Value of interface input
rval := reflect.ValueOf(x)
rkind := rval.Kind()
// Check if reflect value is supported or not
if rkind != reflect.Struct && rkind != reflect.Ptr {
return false
}
// Default case
res := len(f.fields) != 0
// Create reflect indirect
indirect := reflect.Indirect(rval)
// Loop over all fields
for _, v := range f.fields {
// Try to get field value
fval := indirect.FieldByName(v.fName)
// Check if field doesn't exist
if !fval.IsValid() {
// In this case returning false is ok
// This case appears when the field isn't found in structure
return false
}
// Get data from field
data := fval.Interface()
// Try to cast a gomock matcher
m, ok := v.match.(gomock.Matcher)
// Check if cast is ok
if ok {
// Run matcher
res = res && m.Matches(data)
} else {
res = res && v.match == data
}
// Check result
if !res {
// If result isn't true at this step, stop now
return false
}
}
// Default case
return res
}
// StructMatcher will return a new struct matcher.
func StructMatcher() StMatcher { return &structMatcher{} }