|
| 1 | +package dproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | +) |
| 8 | + |
| 9 | +func Example() { |
| 10 | + data := `{"name": "Alice", "scores": [85, 92, 78]}` |
| 11 | + var v any |
| 12 | + if err := json.Unmarshal([]byte(data), &v); err != nil { |
| 13 | + log.Fatal(err) |
| 14 | + } |
| 15 | + proxy := New(v) |
| 16 | + |
| 17 | + name, _ := proxy.M("name").String() |
| 18 | + fmt.Println(name) |
| 19 | + |
| 20 | + score, _ := proxy.M("scores").A(1).Int64() |
| 21 | + fmt.Println(score) |
| 22 | + |
| 23 | + // Output: |
| 24 | + // Alice |
| 25 | + // 92 |
| 26 | +} |
| 27 | + |
| 28 | +func ExampleNew() { |
| 29 | + v := map[string]any{ |
| 30 | + "items": []any{"a", "b", "c"}, |
| 31 | + } |
| 32 | + p := New(v) |
| 33 | + |
| 34 | + item, _ := p.M("items").A(2).String() |
| 35 | + fmt.Println(item) |
| 36 | + |
| 37 | + // Output: |
| 38 | + // c |
| 39 | +} |
| 40 | + |
| 41 | +func ExampleProxy_M() { |
| 42 | + v := parseJSON(`{"user": {"name": "Bob", "age": 30}}`) |
| 43 | + |
| 44 | + name, _ := New(v).M("user").M("name").String() |
| 45 | + fmt.Println(name) |
| 46 | + |
| 47 | + age, _ := New(v).M("user").M("age").Int64() |
| 48 | + fmt.Println(age) |
| 49 | + |
| 50 | + // Output: |
| 51 | + // Bob |
| 52 | + // 30 |
| 53 | +} |
| 54 | + |
| 55 | +func ExampleProxy_A() { |
| 56 | + v := parseJSON(`["apple", "banana", "cherry"]`) |
| 57 | + |
| 58 | + p := New(v) |
| 59 | + for _, i := range []int{0, 1, 2} { |
| 60 | + s, _ := p.A(i).String() |
| 61 | + fmt.Println(s) |
| 62 | + } |
| 63 | + |
| 64 | + // Output: |
| 65 | + // apple |
| 66 | + // banana |
| 67 | + // cherry |
| 68 | +} |
| 69 | + |
| 70 | +func ExampleProxy_P() { |
| 71 | + v := parseJSON(`{ |
| 72 | + "store": { |
| 73 | + "book": {"title": "Go Programming"}, |
| 74 | + "price": 3000 |
| 75 | + } |
| 76 | + }`) |
| 77 | + |
| 78 | + title, _ := New(v).P("/store/book/title").String() |
| 79 | + fmt.Println(title) |
| 80 | + |
| 81 | + price, _ := New(v).P("/store/price").Int64() |
| 82 | + fmt.Println(price) |
| 83 | + |
| 84 | + // Output: |
| 85 | + // Go Programming |
| 86 | + // 3000 |
| 87 | +} |
| 88 | + |
| 89 | +func ExampleProxy_Q() { |
| 90 | + v := parseJSON(`{ |
| 91 | + "users": [ |
| 92 | + {"name": "Alice", "role": "admin"}, |
| 93 | + {"name": "Bob", "role": "user"}, |
| 94 | + {"name": "Carol", "role": "admin"} |
| 95 | + ] |
| 96 | + }`) |
| 97 | + |
| 98 | + roles, _ := New(v).Q("role").StringArray() |
| 99 | + for _, r := range roles { |
| 100 | + fmt.Println(r) |
| 101 | + } |
| 102 | + |
| 103 | + // Unordered output: |
| 104 | + // admin |
| 105 | + // user |
| 106 | + // admin |
| 107 | +} |
| 108 | + |
| 109 | +func ExampleProxy_ProxySet() { |
| 110 | + // From a typed slice |
| 111 | + v1 := []int{10, 20, 30} |
| 112 | + ps1 := New(v1).ProxySet() |
| 113 | + fmt.Println(ps1.Len()) |
| 114 | + |
| 115 | + // From a typed map |
| 116 | + v2 := map[string]int{"a": 1, "b": 2} |
| 117 | + ps2 := New(v2).ProxySet() |
| 118 | + fmt.Println(ps2.Len()) |
| 119 | + |
| 120 | + // From a JSON array |
| 121 | + v3 := parseJSON(`[true, false, true]`) |
| 122 | + ps3 := New(v3).ProxySet() |
| 123 | + bools, _ := ps3.BoolArray() |
| 124 | + fmt.Println(bools) |
| 125 | + |
| 126 | + // Output: |
| 127 | + // 3 |
| 128 | + // 2 |
| 129 | + // [true false true] |
| 130 | +} |
| 131 | + |
| 132 | +func ExampleProxySet_Qc() { |
| 133 | + v := parseJSON(`[ |
| 134 | + {"name": "Alice", "age": 30}, |
| 135 | + {"name": "Bob"}, |
| 136 | + {"name": "Carol", "age": 25} |
| 137 | + ]`) |
| 138 | + |
| 139 | + ages, _ := New(v).ProxySet().Qc("age").Int64Array() |
| 140 | + for _, a := range ages { |
| 141 | + fmt.Println(a) |
| 142 | + } |
| 143 | + |
| 144 | + // Unordered output: |
| 145 | + // 30 |
| 146 | + // 25 |
| 147 | +} |
| 148 | + |
| 149 | +func ExampleProxySet_Q() { |
| 150 | + v := parseJSON(`[ |
| 151 | + {"nested": {"city": "NYC"}}, |
| 152 | + {"nested": {"city": "LA"}} |
| 153 | + ]`) |
| 154 | + |
| 155 | + cities, _ := New(v).ProxySet().Q("city").StringArray() |
| 156 | + for _, c := range cities { |
| 157 | + fmt.Println(c) |
| 158 | + } |
| 159 | + |
| 160 | + // Unordered output: |
| 161 | + // NYC |
| 162 | + // LA |
| 163 | +} |
| 164 | + |
| 165 | +func ExampleProxySet_genericMap() { |
| 166 | + // Q works with typed maps, not just map[string]any |
| 167 | + v := []map[string]int{{"x": 1}, {"x": 2, "y": 3}} |
| 168 | + |
| 169 | + xs, _ := New(v).ProxySet().Q("x").Int64Array() |
| 170 | + for _, x := range xs { |
| 171 | + fmt.Println(x) |
| 172 | + } |
| 173 | + |
| 174 | + // Unordered output: |
| 175 | + // 1 |
| 176 | + // 2 |
| 177 | +} |
| 178 | + |
| 179 | +func ExampleProxySet_StringArray() { |
| 180 | + v := parseJSON(`["foo", "bar", "baz"]`) |
| 181 | + |
| 182 | + result, _ := New(v).ProxySet().StringArray() |
| 183 | + fmt.Println(result) |
| 184 | + |
| 185 | + // Output: |
| 186 | + // [foo bar baz] |
| 187 | +} |
| 188 | + |
| 189 | +func ExamplePointer() { |
| 190 | + v := parseJSON(`{"cities": ["Tokyo", "Osaka", "Hakata"]}`) |
| 191 | + |
| 192 | + city, _ := Pointer(v, "/cities/0").String() |
| 193 | + fmt.Println(city) |
| 194 | + |
| 195 | + // Output: |
| 196 | + // Tokyo |
| 197 | +} |
| 198 | + |
| 199 | +func ExampleDrain() { |
| 200 | + v := parseJSON(`{ |
| 201 | + "a": 1, |
| 202 | + "b": "hello", |
| 203 | + "c": 3.14 |
| 204 | + }`) |
| 205 | + |
| 206 | + var d Drain |
| 207 | + p := New(v) |
| 208 | + |
| 209 | + _ = d.Int64(p.M("a")) |
| 210 | + _ = d.String(p.M("b")) |
| 211 | + _ = d.Float64(p.M("c")) |
| 212 | + |
| 213 | + if d.Has() { |
| 214 | + fmt.Println("errors:", d.CombineErrors()) |
| 215 | + } else { |
| 216 | + fmt.Println("all ok") |
| 217 | + } |
| 218 | + |
| 219 | + // Output: |
| 220 | + // all ok |
| 221 | +} |
| 222 | + |
| 223 | +func ExampleDrain_error() { |
| 224 | + v := parseJSON(`{"x": "not a number"}`) |
| 225 | + |
| 226 | + var d Drain |
| 227 | + p := New(v) |
| 228 | + |
| 229 | + val := d.Int64(p.M("x")) |
| 230 | + fmt.Println("got:", val) |
| 231 | + |
| 232 | + if err := d.CombineErrors(); err != nil { |
| 233 | + fmt.Println("error:", err) |
| 234 | + } |
| 235 | + |
| 236 | + // Output: |
| 237 | + // got: 0 |
| 238 | + // error: not matched types: expected=int64 actual=string: x |
| 239 | +} |
0 commit comments