-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.go
193 lines (159 loc) · 4.32 KB
/
array.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package schema
import (
"fmt"
"sort"
"strings"
)
// Array compares all values of the array one by one. Either specific or other Matchers.
func Array(exps ...interface{}) Matcher {
return MatcherFunc("Array", func(data interface{}) *Error {
fieldError := &Error{}
dataArray, ok := data.([]interface{})
if !ok {
return SelfError(fmt.Sprintf("is no array: %t", data))
}
if len(exps) != len(dataArray) {
fieldError.Add(selfField, fmt.Sprintf("length does not match %d != %d", len(dataArray), len(exps)))
}
for i := 0; i < len(exps) && i < len(dataArray); i++ {
actual := dataArray[i]
exp := exps[i]
matchValue(fieldError, errorIdxField(i), exp, actual)
}
if fieldError.Any() {
return fieldError
}
return nil
})
}
// ArrayEach checks all values of an array against the given value or Matcher
func ArrayEach(exp interface{}) Matcher {
return MatcherFunc("ArrayEach", func(data interface{}) *Error {
fieldError := &Error{}
dataArray, ok := data.([]interface{})
if !ok {
return SelfError(fmt.Sprintf("is no array: %t", data))
}
for i := 0; i < len(dataArray); i++ {
actual := dataArray[i]
matchValue(fieldError, errorIdxField(i), exp, actual)
}
if fieldError.Any() {
return fieldError
}
return nil
})
}
// ArrayUnordered checks that all values or matchers are satisfied, ignoring the order of the array.
func ArrayUnordered(exps ...interface{}) Matcher {
return MatcherFunc("ArrayUnordered", func(data interface{}) *Error {
fieldError := &Error{}
dataArray, ok := data.([]interface{})
if !ok {
return SelfError(fmt.Sprintf("is no array: %t", data))
}
if len(exps) != len(dataArray) {
fieldError.Add(selfField, fmt.Sprintf("length does not match %d != %d", len(dataArray), len(exps)))
}
matchedIndices, err := arrayIncludingMatchedIndices(exps, dataArray)
if err != nil {
fieldError.Merge(selfField, err)
}
if len(matchedIndices) != len(dataArray) {
for i := 0; i < len(dataArray); i++ {
if !matchedIndices[i] {
fieldError.Add(errorIdxField(i), "unmatched")
}
}
}
if fieldError.Any() {
return fieldError
}
return nil
})
}
// ArrayIncluding checks that all given Matchers or values are present in the array.
func ArrayIncluding(exps ...interface{}) Matcher {
return MatcherFunc("ArrayIncluding", func(data interface{}) *Error {
dataArray, ok := data.([]interface{})
if !ok {
return SelfError(fmt.Sprintf("is no array: %t", data))
}
_, err := arrayIncludingMatchedIndices(exps, dataArray)
return err
})
}
func arrayIncludingMatchedIndices(exps []interface{}, dataArray []interface{}) (matchedIndices map[int]bool, err *Error) {
fieldError := &Error{}
sortableExps := make([]*origExp, len(exps))
for i, exp := range exps {
sortableExps[i] = &origExp{OriginalIndex: i, Exp: exp}
}
sortExps(sortableExps)
matchedIndices = map[int]bool{}
for _, exp := range sortableExps {
foundMatching := false
for i, v := range dataArray {
if matchedIndices[i] {
continue
}
e := &Error{}
matchValue(e, errorIdxField(i), exp.Exp, v)
if !e.Any() {
matchedIndices[i] = true
foundMatching = true
break
}
}
if !foundMatching {
switch t := exp.Exp.(type) {
case Matcher:
fieldError.Add(selfField, fmt.Sprintf("%s<%d> did not match", t, exp.OriginalIndex))
default:
fieldError.Add(selfField, fmt.Sprintf("%v:%T<%d> not included", t, t, exp.OriginalIndex))
}
}
}
if fieldError.Any() {
return matchedIndices, fieldError
}
return matchedIndices, nil
}
func sortExps(exps sortableExps) {
sort.Sort(exps)
}
type origExp struct {
OriginalIndex int
Exp interface{}
}
type sortableExps []*origExp
func (exps sortableExps) Len() int {
return len(exps)
}
func (exps sortableExps) Swap(i, j int) {
exps[i], exps[j] = exps[j], exps[i]
}
func (exps sortableExps) Less(i, j int) bool {
a, b := exps[i], exps[j]
return expPrio(a.Exp) < expPrio(b.Exp)
}
func expPrio(a interface{}) int {
_, isGenericMatcher := a.(Matcher)
_, isCaptureMatcher := a.(CaptureMatcher)
// order is relevant
switch {
case a == IsPresent:
return 9
case isCaptureMatcher:
return 7
case isGenericMatcher:
return 8
}
return 0
}
func errorIdxField(i int) string {
return fmt.Sprintf("[%d]", i)
}
func isErrorIdxField(f string) bool {
return strings.HasPrefix(f, "[") && strings.HasSuffix(f, "]")
}