-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_example_test.go
68 lines (53 loc) · 1.33 KB
/
update_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package astjson_test
import (
"fmt"
"log"
"github.com/wundergraph/astjson"
)
func ExampleObject_Del() {
v := astjson.MustParse(`{"foo": 123, "bar": [1,2], "baz": "xyz"}`)
o, err := v.Object()
if err != nil {
log.Fatalf("cannot otain object: %s", err)
}
fmt.Printf("%s\n", o)
o.Del("bar")
fmt.Printf("%s\n", o)
o.Del("foo")
fmt.Printf("%s\n", o)
o.Del("baz")
fmt.Printf("%s\n", o)
// Output:
// {"foo":123,"bar":[1,2],"baz":"xyz"}
// {"foo":123,"baz":"xyz"}
// {"baz":"xyz"}
// {}
}
func ExampleValue_Del() {
v := astjson.MustParse(`{"foo": 123, "bar": [1,2], "baz": "xyz"}`)
fmt.Printf("%s\n", v)
v.Del("foo")
fmt.Printf("%s\n", v)
v.Get("bar").Del("0")
fmt.Printf("%s\n", v)
// Output:
// {"foo":123,"bar":[1,2],"baz":"xyz"}
// {"bar":[1,2],"baz":"xyz"}
// {"bar":[2],"baz":"xyz"}
}
func ExampleValue_Set() {
v := astjson.MustParse(`{"foo":1,"bar":[2,3]}`)
// Replace `foo` value with "xyz"
v.Set("foo", astjson.MustParse(`"xyz"`))
// Add "newv":123
v.Set("newv", astjson.MustParse(`123`))
fmt.Printf("%s\n", v)
// Replace `bar.1` with {"x":"y"}
v.Get("bar").Set("1", astjson.MustParse(`{"x":"y"}`))
// Add `bar.3="qwe"
v.Get("bar").Set("3", astjson.MustParse(`"qwe"`))
fmt.Printf("%s\n", v)
// Output:
// {"foo":"xyz","bar":[2,3],"newv":123}
// {"foo":"xyz","bar":[2,{"x":"y"},null,"qwe"],"newv":123}
}