summaryrefslogtreecommitdiff
path: root/coll/jsonpath_test.go
blob: c7bacc201267dc8b30f34da251a42a09222c958b (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
package coll

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type (
	m  = map[string]any
	ar = []any
)

func TestJSONPath(t *testing.T) {
	in := m{
		"store": m{
			"book": ar{
				m{
					"category": "reference",
					"author":   "Nigel Rees",
					"title":    "Sayings of the Century",
					"price":    8.95,
				},
				m{
					"category": "fiction",
					"author":   "Evelyn Waugh",
					"title":    "Sword of Honour",
					"price":    12.99,
				},
				m{
					"category": "fiction",
					"author":   "Herman Melville",
					"title":    "Moby Dick",
					"isbn":     "0-553-21311-3",
					"price":    8.99,
				},
				m{
					"category": "fiction",
					"author":   "J. R. R. Tolkien",
					"title":    "The Lord of the Rings",
					"isbn":     "0-395-19395-8",
					"price":    22.99,
				},
			},
			"bicycle": m{
				"color": "red",
				"price": 19.95,
			},
		},
	}
	out, err := JSONPath(".store.bicycle.color", in)
	require.NoError(t, err)
	assert.Equal(t, "red", out)

	out, err = JSONPath(".store.bicycle.price", in)
	require.NoError(t, err)
	assert.InEpsilon(t, 19.95, out, 1e-12)

	_, err = JSONPath(".store.bogus", in)
	require.Error(t, err)

	_, err = JSONPath("{.store.unclosed", in)
	require.Error(t, err)

	out, err = JSONPath(".store", in)
	require.NoError(t, err)
	assert.Equal(t, in["store"], out)

	out, err = JSONPath("$.store.book[*].author", in)
	require.NoError(t, err)
	assert.Len(t, out, 4)
	assert.Contains(t, out, "Nigel Rees")
	assert.Contains(t, out, "Evelyn Waugh")
	assert.Contains(t, out, "Herman Melville")
	assert.Contains(t, out, "J. R. R. Tolkien")

	out, err = JSONPath("$..book[?( @.price < 10.0 )]", in)
	require.NoError(t, err)
	expected := ar{
		m{
			"category": "reference",
			"author":   "Nigel Rees",
			"title":    "Sayings of the Century",
			"price":    8.95,
		},
		m{
			"category": "fiction",
			"author":   "Herman Melville",
			"title":    "Moby Dick",
			"isbn":     "0-553-21311-3",
			"price":    8.99,
		},
	}
	assert.Equal(t, expected, out)

	in = m{
		"a": m{
			"aa": m{
				"foo": m{
					"aaa": m{
						"aaaa": m{
							"bar": 1234,
						},
					},
				},
			},
			"ab": m{
				"aba": m{
					"foo": m{
						"abaa": true,
						"abab": "baz",
					},
				},
			},
		},
	}
	out, err = JSONPath("..foo.*", in)
	require.NoError(t, err)
	assert.Len(t, out, 3)
	assert.Contains(t, out, m{"aaaa": m{"bar": 1234}})
	assert.Contains(t, out, true)
	assert.Contains(t, out, "baz")

	type bicycleType struct {
		Color string
	}
	type storeType struct {
		Bicycle *bicycleType
		safe    any
	}

	structIn := &storeType{
		Bicycle: &bicycleType{
			Color: "red",
		},
		safe: "hidden",
	}

	out, err = JSONPath(".Bicycle.Color", structIn)
	require.NoError(t, err)
	assert.Equal(t, "red", out)

	_, err = JSONPath(".safe", structIn)
	require.Error(t, err)

	_, err = JSONPath(".*", structIn)
	require.Error(t, err)
}