-
Notifications
You must be signed in to change notification settings - Fork 2
/
handy_example_test.go
48 lines (36 loc) · 1.21 KB
/
handy_example_test.go
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
package astjson_test
import (
"fmt"
"github.com/wundergraph/astjson"
)
func ExampleGetString() {
data := []byte(`{"foo":{"bar":[123,"baz"]}}`)
s := astjson.GetString(data, "foo", "bar", "1")
fmt.Printf("data.foo.bar[1] = %s", s)
// Output:
// data.foo.bar[1] = baz
}
func ExampleGetInt() {
data := []byte(`{"foo": [233,true, {"bar": [2343]} ]}`)
n1 := astjson.GetInt(data, "foo", "0")
fmt.Printf("data.foo[0] = %d\n", n1)
n2 := astjson.GetInt(data, "foo", "2", "bar", "0")
fmt.Printf("data.foo[2].bar[0] = %d\n", n2)
// Output:
// data.foo[0] = 233
// data.foo[2].bar[0] = 2343
}
func ExampleExists() {
data := []byte(`{"foo": [1.23,{"bar":33,"baz":null}]}`)
fmt.Printf("exists(data.foo) = %v\n", astjson.Exists(data, "foo"))
fmt.Printf("exists(data.foo[0]) = %v\n", astjson.Exists(data, "foo", "0"))
fmt.Printf("exists(data.foo[1].baz) = %v\n", astjson.Exists(data, "foo", "1", "baz"))
fmt.Printf("exists(data.foobar) = %v\n", astjson.Exists(data, "foobar"))
fmt.Printf("exists(data.foo.bar) = %v\n", astjson.Exists(data, "foo", "bar"))
// Output:
// exists(data.foo) = true
// exists(data.foo[0]) = true
// exists(data.foo[1].baz) = true
// exists(data.foobar) = false
// exists(data.foo.bar) = false
}