-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuncs.go
More file actions
259 lines (237 loc) · 7.24 KB
/
funcs.go
File metadata and controls
259 lines (237 loc) · 7.24 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package probe
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
// MatchJSON compares two `map[string]any` objects strictly.
// All fields in `src` and `target` must match, including structure and values.
func MatchJSON(src, target map[string]any) bool {
var diffs []string
return deepMatch(src, target, &diffs, "")
}
// DiffJSON compares two `map[string]any` objects strictly and collects differences.
func DiffJSON(src, target map[string]any) string {
var diffs []string
if match := deepMatch(src, target, &diffs, ""); match {
return "No diff"
}
return strings.Join(diffs, "\n")
}
// deepMatch recursively compares `src` and `target`.
func deepMatch(src, target any, diffs *[]string, path string) bool {
// extendPath constructs a new path for nested keys.
extendPath := func(path, key string) string {
if path == "" {
return key
}
return fmt.Sprintf("%s.%s", path, key)
}
switch targetVal := target.(type) {
case map[string]any:
// Check if src is also a map
srcMap, ok := src.(map[string]any)
if !ok {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected a map, got %T", path, src))
return false
}
// Check for missing or mismatched keys
for key, targetValue := range targetVal {
newPath := extendPath(path, key)
srcValue, exists := srcMap[key]
if !exists {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Missing in source", newPath))
return false
}
if !deepMatch(srcValue, targetValue, diffs, newPath) {
return false
}
}
// Check for extra keys in src
for key := range srcMap {
if _, exists := targetVal[key]; !exists {
newPath := extendPath(path, key)
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Extra field in source", newPath))
return false
}
}
return true
case []any:
// Check if src is also a slice
srcSlice, ok := src.([]any)
if !ok {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected a slice, got %T", path, src))
return false
}
if len(srcSlice) != len(targetVal) {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Slice length mismatch (expected %d, got %d)", path, len(targetVal), len(srcSlice)))
return false
}
// Recursively compare each element in the slice
for i, targetElem := range targetVal {
newPath := fmt.Sprintf("%s[%d]", path, i)
if !deepMatch(srcSlice[i], targetElem, diffs, newPath) {
return false
}
}
return true
case int, int64, uint, uint64, float64:
targetStr, ok := AnyToString(target)
if !ok {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected number, got %T", path, target))
return false
}
srcStr, ok := AnyToString(src)
if !ok {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected %#v, got %#v", path, target, src))
return false
}
if srcStr != targetStr {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected %s, got %s", path, targetStr, srcStr))
return false
}
return true
case string:
// If the target value is a regex (e.g., "/regex/")
if len(targetVal) > 2 && targetVal[0] == '/' && targetVal[len(targetVal)-1] == '/' {
pattern := targetVal[1 : len(targetVal)-1] // Extract the regex pattern
re := regexp.MustCompile(pattern)
srcStr, ok := src.(string)
if ok {
if !re.MatchString(srcStr) {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Regex mismatch (pattern: %s, value: %v)", path, pattern, srcStr))
return false
}
} else {
srcStr, ok := AnyToString(src)
if !ok || !re.MatchString(srcStr) {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Regex mismatch (pattern: %s, value: %v)", path, pattern, srcStr))
return false
}
}
return true
}
// Otherwise, compare as a regular string
srcStr, ok := src.(string)
if !ok || srcStr != targetVal {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected '%s', got '%v'", path, targetVal, src))
return false
}
return true
default:
// Compare all other types directly
if src != target {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected '%v', got '%v'", path, target, src))
return false
}
return true
}
}
// ParseJSON parses a JSON string into a map[string]any or []any.
func ParseJSON(s string) (any, error) {
s = strings.TrimSpace(s)
if len(s) == 0 {
return nil, fmt.Errorf("parse_json: empty string")
}
// Try parsing as object
if s[0] == '{' {
var obj map[string]any
if err := json.Unmarshal([]byte(s), &obj); err != nil {
return nil, fmt.Errorf("parse_json: %w", err)
}
return obj, nil
}
// Try parsing as array
if s[0] == '[' {
var arr []any
if err := json.Unmarshal([]byte(s), &arr); err != nil {
return nil, fmt.Errorf("parse_json: %w", err)
}
return arr, nil
}
return nil, fmt.Errorf("parse_json: input is not a JSON object or array")
}
// deepMatchWithDiffs recursively compares `src` and `target` and collects differences.
//
//nolint:unused // Reserved for future use
func deepMatchWithDiffs(src, target any, diffs *[]string, path string) bool {
// extendPath constructs a new path for nested keys.
extendPath := func(path, key string) string {
if path == "" {
return key
}
return fmt.Sprintf("%s.%s", path, key)
}
switch targetVal := target.(type) {
case map[string]any:
srcMap, ok := src.(map[string]any)
if !ok {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected a map, got %T", path, src))
return false
}
// Check for missing or mismatched keys
for key, targetValue := range targetVal {
newPath := extendPath(path, key)
srcValue, exists := srcMap[key]
if !exists {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Missing in source", newPath))
return false
}
if !deepMatchWithDiffs(srcValue, targetValue, diffs, newPath) {
return false
}
}
// Check for extra keys in src
for key := range srcMap {
if _, exists := targetVal[key]; !exists {
newPath := extendPath(path, key)
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Extra field in source", newPath))
return false
}
}
return true
case []any:
srcSlice, ok := src.([]any)
if !ok {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected a slice, got %T", path, src))
return false
}
if len(srcSlice) != len(targetVal) {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Slice length mismatch (expected %d, got %d)", path, len(targetVal), len(srcSlice)))
return false
}
for i := range targetVal {
newPath := fmt.Sprintf("%s[%d]", path, i)
if !deepMatchWithDiffs(srcSlice[i], targetVal[i], diffs, newPath) {
return false
}
}
return true
case string:
// If the target is a regex
if len(targetVal) > 2 && targetVal[0] == '/' && targetVal[len(targetVal)-1] == '/' {
pattern := targetVal[1 : len(targetVal)-1]
re := regexp.MustCompile(pattern)
srcStr, ok := src.(string)
if !ok || !re.MatchString(srcStr) {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Regex mismatch (pattern: %s, value: %v)", path, pattern, src))
return false
}
return true
}
// Regular string comparison
srcStr, ok := src.(string)
if !ok || srcStr != targetVal {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected '%s', got '%v'", path, targetVal, src))
return false
}
return true
default:
if src != target {
*diffs = append(*diffs, fmt.Sprintf("Key '%s': Expected '%v', got '%v'", path, target, src))
return false
}
return true
}
}