aboutsummaryrefslogtreecommitdiff
path: root/jsonmatch/machinery.go
blob: ae0c2543806659b4f72d86621cfd70e1e39b6038 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// SPDX-FileCopyrightText: 2025 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package jsonmatch

import (
	"encoding/json"
	"fmt"
	"maps"
	"slices"
	"strconv"
	"strings"

	. "go.xyrillian.de/gg/option"
)

func marshalExpectedForDiff(value any) string {
	// `expected` values can technically contain any sort of nonsense,
	// so we want to print something useful even if marshaling fails
	buf, err := json.Marshal(value)
	if err != nil {
		return fmt.Sprintf("<not marshalable to JSON, %%#v is %#v>", value)
	}
	return string(buf)
}

func marshalActualForDiff(value any) string {
	// `actual` values are always safe to marshal because they were
	// unmarshaled from JSON into any and thus can only contain safe
	buf, err := json.Marshal(value)
	if err != nil {
		// this line is therefore unreachable in tests and only exists as defense in depth
		return fmt.Sprintf("<marshal error: %s>", err.Error())
	}
	return string(buf)
}

// Given a string that is probably a JSON message, look at the first non-blank
// character to determine what kind of value the JSON message has on its top level.
// The exact character returned does not matter; this is only used to check if two messages are vaguely of the same type.
func kindForJSONMessage(s string) byte {
	s = strings.TrimSpace(s)
	if s == "" {
		// defense in depth: this function should never be called on functionally empty inputs
		return '?'
	}
	b := s[0]
	switch b {
	case '{', '[', '"', 'n':
		return b // object, array, string or null respectively
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
		return '0' // number (NOTE: leading + is not allowed, leading decimal dot is not allowed without a 0 before it)
	case 't', 'f':
		return 'b' // boolean
	default:
		return '?' // syntax error
	}
}

// Entrypoint into this file coming from all DiffAgainst() implementations.
func diffAgainst(expected any, buf []byte) []Diff {
	var actual any
	err := json.Unmarshal(buf, &actual)
	if err != nil {
		return []Diff{{
			Kind:         fmt.Sprintf("unmarshal error (%s)", err.Error()),
			Pointer:      "",
			ExpectedJSON: marshalExpectedForDiff(expected),
			ActualJSON:   strings.ToValidUTF8(string(buf), "\uFFFD"),
		}}
	}

	// While recursing through the object, we maintain a `path` that identifies
	// where we are in the callstack, e.g. when comparing
	//
	//	actual = { "foo": { "bar": [ 5, 23 ] } }
	//	expected = { "foo": { "bar": [ 5, 42 ] } }
	//
	// we would generate a diff at Path = {"foo", "bar", 1}. Since diffs are
	// usually rare, we only build Pointer strings out of these paths when we
	// really need them. During recursion, `path` is maintained as a sequence of
	// path fragments, most of which are constants to keep allocations to a
	// minimum. WARNING: Because the `path` slice is heavily reused across nested
	// function calls, it is not safe to store references to the `path` slice.
	path := make([]pathElement, 0, 32)
	return getDiffsForValue(path, expected, actual)
}

type pathElement struct {
	Key   Option[string]
	Index int
}

func keyElement(key string) pathElement { return pathElement{Some(key), 0} }
func indexElement(idx int) pathElement  { return pathElement{None[string](), idx} }

func pathIntoPointer(path []pathElement) Pointer {
	if len(path) == 0 {
		return ""
	}
	fragments := make([]string, len(path)+1)
	fragments[0] = ""
	for idx, elem := range path {
		if key, ok := elem.Key.Unpack(); ok {
			fragments[idx+1] = keyIntoPointerFragment(key)
		} else {
			fragments[idx+1] = strconv.Itoa(elem.Index)
		}
	}
	return Pointer(strings.Join(fragments, "/"))
}

func keyIntoPointerFragment(key string) string {
	buf, _ := json.Marshal(key)
	s := string(buf)
	s = strings.TrimPrefix(s, "\"")
	s = strings.TrimSuffix(s, "\"")
	s = strings.ReplaceAll(s, "~", "~0")
	s = strings.ReplaceAll(s, "/", "~1")
	return s
}

// NOTE: getDiffsForValue is the main part of the recursion to generate the diff.
func getDiffsForValue(path []pathElement, expected, actual any) []Diff {
	// specialized handling for relevant recursible or capturable types
	switch expected := expected.(type) {
	case map[string]any:
		return getDiffsForObject(path, expected, actual)
	case Object:
		return getDiffsForObject(path, expected, actual)
	case []any:
		return getDiffsForArray(path, expected, actual)
	case Array:
		return getDiffsForArray(path, expected, actual)
	case []map[string]any:
		downcasted := make([]any, len(expected))
		for idx, val := range expected {
			downcasted[idx] = val
		}
		return getDiffsForArray(path, downcasted, actual)
	case []Object:
		downcasted := make([]any, len(expected))
		for idx, val := range expected {
			downcasted[idx] = val
		}
		return getDiffsForArray(path, downcasted, actual)
	case capturedField:
		return getDiffsForCapturedField(path, expected, actual)
	case irrelevant:
		return nil
	case nil:
		// this case needs to be handled separately because the code below
		// cannot deal with reflect.TypeOf(expected) returning nil
		if actual == nil {
			return nil
		} else {
			return []Diff{{
				Kind:         "type mismatch",
				Pointer:      pathIntoPointer(path),
				ExpectedJSON: "null",
				ActualJSON:   marshalActualForDiff(actual),
			}}
		}
	}

	// generic handling for values or structures that we do not recurse into further:
	// check that `expected` encodes to JSON in an equivalent way to `actual`
	actualJSON := marshalActualForDiff(actual)
	expectedJSON := marshalExpectedForDiff(expected)
	if expectedJSON == actualJSON {
		return nil
	}

	// if `expected` is using a custom type, we might have to do some heavy lifting:
	// `actual` has all its objects as map[string]any, so keys serialize in sorted order,
	// but `expected` might have struct type instead, where keys serialize in declaration order;
	// this can be normalized by roundtripping `expectedJSON` through map[string]any once
	// (if any of these steps fail, this is intentionally not an error because it's only a last resort)
	var roundtrip any
	err := json.Unmarshal([]byte(expectedJSON), &roundtrip)
	if err == nil {
		buf, err := json.Marshal(roundtrip)
		if err == nil {
			expectedJSON = string(buf)
			if expectedJSON == actualJSON {
				return nil
			}
		}
	}

	kind := "value mismatch"
	if kindForJSONMessage(actualJSON) != kindForJSONMessage(expectedJSON) {
		kind = "type mismatch"
	}
	return []Diff{{
		Kind:         kind,
		Pointer:      pathIntoPointer(path),
		ExpectedJSON: expectedJSON,
		ActualJSON:   actualJSON,
	}}
}

func getDiffsForObject(path []pathElement, expected map[string]any, actual any) []Diff {
	if actual, ok := actual.(map[string]any); ok {
		return getDiffsForConfirmedObject(path, expected, actual)
	}
	return []Diff{{
		Kind:         "type mismatch",
		Pointer:      pathIntoPointer(path),
		ExpectedJSON: marshalExpectedForDiff(expected),
		ActualJSON:   marshalActualForDiff(actual),
	}}
}

func getDiffsForConfirmedObject(path []pathElement, expected, actual map[string]any) (diffs []Diff) {
	// recurse into all fields
	for _, key := range slices.Sorted(maps.Keys(actual)) {
		subpath := append(path, keyElement(key))
		expectedValue, exists := expected[key]
		if exists {
			diffs = append(diffs, getDiffsForValue(subpath, expectedValue, actual[key])...)
		} else {
			diffs = append(diffs, Diff{
				Kind:         "value mismatch",
				Pointer:      pathIntoPointer(subpath),
				ExpectedJSON: "<missing>",
				ActualJSON:   marshalActualForDiff(actual[key]),
			})
		}
	}
	for _, key := range slices.Sorted(maps.Keys(expected)) {
		_, exists := actual[key]
		if !exists {
			subpath := append(path, keyElement(key))
			diffs = append(diffs, Diff{
				Kind:         "value mismatch",
				Pointer:      pathIntoPointer(subpath),
				ExpectedJSON: marshalExpectedForDiff(expected[key]),
				ActualJSON:   "<missing>",
			})
		}
	}

	return diffs
}

func getDiffsForArray(path []pathElement, expected []any, actual any) []Diff {
	if actual, ok := actual.([]any); ok {
		return getDiffsForConfirmedArray(path, expected, actual)
	}
	return []Diff{{
		Kind:         "type mismatch",
		Pointer:      pathIntoPointer(path),
		ExpectedJSON: marshalExpectedForDiff(expected),
		ActualJSON:   marshalActualForDiff(actual),
	}}
}

func getDiffsForConfirmedArray(path []pathElement, expected, actual []any) (diffs []Diff) {
	// recurse into all elements
	for idx := range max(len(actual), len(expected)) {
		subpath := append(path, indexElement(idx))
		switch {
		case idx >= len(actual):
			diffs = append(diffs, Diff{
				Kind:         "value mismatch",
				Pointer:      pathIntoPointer(subpath),
				ActualJSON:   "<missing>",
				ExpectedJSON: marshalExpectedForDiff(expected[idx]),
			})
		case idx >= len(expected):
			diffs = append(diffs, Diff{
				Kind:         "value mismatch",
				Pointer:      pathIntoPointer(subpath),
				ActualJSON:   marshalActualForDiff(actual[idx]),
				ExpectedJSON: "<missing>",
			})
		default:
			diffs = append(diffs, getDiffsForValue(subpath, expected[idx], actual[idx])...)
		}
	}

	return diffs
}

func getDiffsForCapturedField(path []pathElement, expected capturedField, actual any) []Diff {
	actualJSON := marshalActualForDiff(actual)
	err := json.Unmarshal([]byte(actualJSON), expected.PointerToTarget)
	if err != nil {
		return []Diff{{
			Kind:         fmt.Sprintf("cannot unmarshal into capture slot (%s)", err.Error()),
			Pointer:      pathIntoPointer(path),
			ActualJSON:   actualJSON,
			ExpectedJSON: fmt.Sprintf("<capture slot of type %T>", expected.PointerToTarget),
		}}
	}
	return nil
}