github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/coll/jsonpath_test.go (about) 1 package coll 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 ) 9 10 type ( 11 m = map[string]interface{} 12 ar = []interface{} 13 ) 14 15 func TestJSONPath(t *testing.T) { 16 in := m{ 17 "store": m{ 18 "book": ar{ 19 m{ 20 "category": "reference", 21 "author": "Nigel Rees", 22 "title": "Sayings of the Century", 23 "price": 8.95, 24 }, 25 m{ 26 "category": "fiction", 27 "author": "Evelyn Waugh", 28 "title": "Sword of Honour", 29 "price": 12.99, 30 }, 31 m{ 32 "category": "fiction", 33 "author": "Herman Melville", 34 "title": "Moby Dick", 35 "isbn": "0-553-21311-3", 36 "price": 8.99, 37 }, 38 m{ 39 "category": "fiction", 40 "author": "J. R. R. Tolkien", 41 "title": "The Lord of the Rings", 42 "isbn": "0-395-19395-8", 43 "price": 22.99, 44 }, 45 }, 46 "bicycle": m{ 47 "color": "red", 48 "price": 19.95, 49 }, 50 }, 51 } 52 out, err := JSONPath(".store.bicycle.color", in) 53 require.NoError(t, err) 54 assert.Equal(t, "red", out) 55 56 out, err = JSONPath(".store.bicycle.price", in) 57 require.NoError(t, err) 58 assert.InEpsilon(t, 19.95, out, 1e-12) 59 60 _, err = JSONPath(".store.bogus", in) 61 require.Error(t, err) 62 63 _, err = JSONPath("{.store.unclosed", in) 64 require.Error(t, err) 65 66 out, err = JSONPath(".store", in) 67 require.NoError(t, err) 68 assert.EqualValues(t, in["store"], out) 69 70 out, err = JSONPath("$.store.book[*].author", in) 71 require.NoError(t, err) 72 assert.Len(t, out, 4) 73 assert.Contains(t, out, "Nigel Rees") 74 assert.Contains(t, out, "Evelyn Waugh") 75 assert.Contains(t, out, "Herman Melville") 76 assert.Contains(t, out, "J. R. R. Tolkien") 77 78 out, err = JSONPath("$..book[?( @.price < 10.0 )]", in) 79 require.NoError(t, err) 80 expected := ar{ 81 m{ 82 "category": "reference", 83 "author": "Nigel Rees", 84 "title": "Sayings of the Century", 85 "price": 8.95, 86 }, 87 m{ 88 "category": "fiction", 89 "author": "Herman Melville", 90 "title": "Moby Dick", 91 "isbn": "0-553-21311-3", 92 "price": 8.99, 93 }, 94 } 95 assert.EqualValues(t, expected, out) 96 97 in = m{ 98 "a": m{ 99 "aa": m{ 100 "foo": m{ 101 "aaa": m{ 102 "aaaa": m{ 103 "bar": 1234, 104 }, 105 }, 106 }, 107 }, 108 "ab": m{ 109 "aba": m{ 110 "foo": m{ 111 "abaa": true, 112 "abab": "baz", 113 }, 114 }, 115 }, 116 }, 117 } 118 out, err = JSONPath("..foo.*", in) 119 require.NoError(t, err) 120 assert.Len(t, out, 3) 121 assert.Contains(t, out, m{"aaaa": m{"bar": 1234}}) 122 assert.Contains(t, out, true) 123 assert.Contains(t, out, "baz") 124 125 type bicycleType struct { 126 Color string 127 } 128 type storeType struct { 129 Bicycle *bicycleType 130 safe interface{} 131 } 132 133 structIn := &storeType{ 134 Bicycle: &bicycleType{ 135 Color: "red", 136 }, 137 safe: "hidden", 138 } 139 140 out, err = JSONPath(".Bicycle.Color", structIn) 141 require.NoError(t, err) 142 assert.Equal(t, "red", out) 143 144 _, err = JSONPath(".safe", structIn) 145 require.Error(t, err) 146 147 _, err = JSONPath(".*", structIn) 148 require.Error(t, err) 149 }