-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
example_test.go
234 lines (190 loc) · 5.6 KB
/
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package dyno_test
import (
"encoding/json"
"fmt"
"os"
"github.com/icza/dyno"
)
// Example shows a few of dyno's features, such as getting, setting and appending
// values to / from a dynamic object.
func Example() {
person := map[string]interface{}{
"name": map[string]interface{}{
"first": "Bob",
"last": "Archer",
},
"age": 22,
"fruits": []interface{}{
"apple", "banana",
},
}
// pp prints the person
pp := func(err error) {
json.NewEncoder(os.Stdout).Encode(person) // Output JSON
if err != nil {
fmt.Println("ERROR:", err)
}
}
// Print initial person and its first name:
pp(nil)
v, err := dyno.Get(person, "name", "first")
fmt.Printf("First name: %v, error: %v\n", v, err)
// Change first name:
pp(dyno.Set(person, "Alice", "name", "first"))
// Change complete name from map to a single string:
pp(dyno.Set(person, "Alice Archer", "name"))
// Print and increment age:
age, err := dyno.GetInt(person, "age")
fmt.Printf("Age: %v, error: %v\n", age, err)
pp(dyno.Set(person, age+1, "age"))
// Change a fruits slice element:
pp(dyno.Set(person, "lemon", "fruits", 1))
// Add a new fruit:
pp(dyno.Append(person, "melon", "fruits"))
// Output:
// {"age":22,"fruits":["apple","banana"],"name":{"first":"Bob","last":"Archer"}}
// First name: Bob, error: <nil>
// {"age":22,"fruits":["apple","banana"],"name":{"first":"Alice","last":"Archer"}}
// {"age":22,"fruits":["apple","banana"],"name":"Alice Archer"}
// Age: 22, error: <nil>
// {"age":23,"fruits":["apple","banana"],"name":"Alice Archer"}
// {"age":23,"fruits":["apple","lemon"],"name":"Alice Archer"}
// {"age":23,"fruits":["apple","lemon","melon"],"name":"Alice Archer"}
}
// Example_jsonEdit shows a simple example how JSON can be edited.
// The password placed in the JSON is masked out.
func Example_jsonEdit() {
src := `{"login":{"password":"secret","user":"bob"},"name":"cmpA"}`
fmt.Printf("Input JSON: %s\n", src)
var v interface{}
if err := json.Unmarshal([]byte(src), &v); err != nil {
panic(err)
}
user, err := dyno.Get(v, "login", "user")
fmt.Printf("User: %-6s, error: %v\n", user, err)
password, err := dyno.Get(v, "login", "password")
fmt.Printf("Password: %-6s, error: %v\n", password, err)
// Edit (mask out) password:
if err = dyno.Set(v, "xxx", "login", "password"); err != nil {
fmt.Printf("Failed to set password: %v\n", err)
}
edited, err := json.Marshal(v)
fmt.Printf("Edited JSON: %s, error: %v\n", edited, err)
// Output:
// Input JSON: {"login":{"password":"secret","user":"bob"},"name":"cmpA"}
// User: bob , error: <nil>
// Password: secret, error: <nil>
// Edited JSON: {"login":{"password":"xxx","user":"bob"},"name":"cmpA"}, error: <nil>
}
func ExampleGet() {
m := map[string]interface{}{
"a": 1,
"b": map[interface{}]interface{}{
3: []interface{}{1, "two", 3.3},
},
}
printValue := func(v interface{}, err error) {
fmt.Printf("Value: %-5v, Error: %v\n", v, err)
}
printValue(dyno.Get(m, "a"))
printValue(dyno.Get(m, "b", 3, 1))
printValue(dyno.Get(m, "x"))
sl, _ := dyno.Get(m, "b", 3) // This is: []interface{}{1, "two", 3.3}
printValue(dyno.Get(sl, 4))
// Output:
// Value: 1 , Error: <nil>
// Value: two , Error: <nil>
// Value: <nil>, Error: missing key: x (path element idx: 0)
// Value: <nil>, Error: index out of range: 4 (path element idx: 0)
}
func ExampleSet() {
m := map[string]interface{}{
"a": 1,
"b": map[string]interface{}{
"3": []interface{}{1, "two", 3.3},
},
}
printMap := func(err error) {
json.NewEncoder(os.Stdout).Encode(m) // Output JSON
if err != nil {
fmt.Println("ERROR:", err)
}
}
printMap(dyno.Set(m, 2, "a"))
printMap(dyno.Set(m, "owt", "b", "3", 1))
printMap(dyno.Set(m, 1, "x"))
sl, _ := dyno.Get(m, "b", "3") // This is: []interface{}{1, "owt", 3.3}
printMap(dyno.Set(sl, 1, 4))
// Output:
// {"a":2,"b":{"3":[1,"two",3.3]}}
// {"a":2,"b":{"3":[1,"owt",3.3]}}
// {"a":2,"b":{"3":[1,"owt",3.3]},"x":1}
// {"a":2,"b":{"3":[1,"owt",3.3]},"x":1}
// ERROR: index out of range: 4 (path element idx: 0)
}
func ExampleAppend() {
m := map[string]interface{}{
"a": []interface{}{
"3", 2, []interface{}{1, "two", 3.3},
},
}
printMap := func(err error) {
fmt.Println(m)
if err != nil {
fmt.Println("ERROR:", err)
}
}
printMap(dyno.Append(m, 4, "a"))
printMap(dyno.Append(m, 9, "a", 2))
printMap(dyno.Append(m, 1, "x"))
// Output:
// map[a:[3 2 [1 two 3.3] 4]]
// map[a:[3 2 [1 two 3.3 9] 4]]
// map[a:[3 2 [1 two 3.3 9] 4]]
// ERROR: missing key: x (path element idx: 0)
}
func ExampleAppendMore() {
m := map[string]interface{}{
"ints": []interface{}{
1, 2,
},
}
err := dyno.AppendMore(m, []interface{}{3, 4, 5}, "ints")
fmt.Println(m, err)
// Output:
// map[ints:[1 2 3 4 5]] <nil>
}
func ExampleDelete() {
m := map[string]interface{}{
"name": "Bob",
"ints": []interface{}{
1, 2, 3,
},
}
err := dyno.Delete(m, "name")
fmt.Println(m, err)
err = dyno.Delete(m, 1, "ints")
fmt.Println(m, err)
err = dyno.Delete(m, "ints")
fmt.Println(m, err)
// Output:
// map[ints:[1 2 3]] <nil>
// map[ints:[1 3]] <nil>
// map[] <nil>
}
func ExampleConvertMapI2MapS() {
m := map[interface{}]interface{}{
1: "one",
"numbers": []interface{}{2, 3, 4.4},
}
// m cannot be marshaled using encoding/json:
data, err := json.Marshal(m)
fmt.Printf("JSON: %q, error: %v\n", data, err)
m2 := dyno.ConvertMapI2MapS(m)
// But m2 can be:
data, err = json.Marshal(m2)
fmt.Printf("JSON: %s, error: %v\n", data, err)
// Output:
// JSON: "", error: json: unsupported type: map[interface {}]interface {}
// JSON: {"1":"one","numbers":[2,3,4.4]}, error: <nil>
}