-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
158 lines (132 loc) · 3.04 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
// Copyright 2018 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package jsonmap_test
import (
"fmt"
"sort"
"github.com/chai2010/jsonmap"
)
func Example_setAndGet() {
var jsonMap = jsonmap.JsonMap{
"a": map[string]interface{}{
"b": map[string]interface{}{
"c": "value-abc",
},
},
}
jsonMap.SetValue("value-xyz", "x", "y", "z")
v1, ok1 := jsonMap.GetValue("a", "b", "c")
fmt.Println(v1, ok1)
v2, ok2 := jsonMap.GetValue("x", "y", "z")
fmt.Println(v2, ok2)
v3, ok3 := jsonMap.GetValue("1", "2", "3")
fmt.Println(v3, ok3)
// replace x/y/z with x/y
jsonMap.SetValue("value-xy", "x", "y")
v4, ok4 := jsonMap.GetValue("x", "y", "z")
fmt.Println(v4, ok4)
v5, ok5 := jsonMap.GetValue("x", "y")
fmt.Println(v5, ok5)
// Output:
// value-abc true
// value-xyz true
// <nil> false
// <nil> false
// value-xy true
}
func Example_structToMapString() {
// https://github.com/chai2010/diffbot-go-client/blob/master/article.go
type Image struct {
Url string `json:"url"`
PixelHeight int `json:"pixelHeight"`
PixelWidth int `json:"pixelWidth"`
}
type Article struct {
Url string `json:"url"`
Meta map[string]interface{} `json:"meta,omitempty"` // Returned with fields.
Tags string `json:"tags,omitempty"` // Returned with fields.
Image Image `json:"image"`
}
article := Article{
Url: "https://github.com/chai2010",
Meta: map[string]interface{}{
"c": 123,
"d": 3.14,
"e": true,
},
Tags: "aa,bb",
Image: Image{
Url: "a.png",
PixelHeight: 100,
PixelWidth: 200,
},
}
var jsonMap = jsonmap.NewJsonMapFromStruct(article)
m := jsonMap.ToMapString("/")
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println("/article"+k, m[k])
}
// Output:
// /article/image/pixelHeight 100
// /article/image/pixelWidth 200
// /article/image/url a.png
// /article/meta/c 123
// /article/meta/d 3.14
// /article/meta/e true
// /article/tags aa,bb
// /article/url https://github.com/chai2010
}
func Example_toMapString() {
var jsonMap = jsonmap.JsonMap{
"a": map[string]interface{}{
"sub-a": "value-sub-a",
},
"b": map[string]interface{}{
"sub-b": "value-sub-b",
},
"c": 123,
"d": 3.14,
"e": true,
"x": map[string]interface{}{
"a": map[string]interface{}{
"sub-a": "value-sub-a",
},
"b": map[string]interface{}{
"sub-b": "value-sub-b",
},
"c": 123,
"d": 3.14,
"e": true,
"z": map[string]interface{}{
"zz": "value-zz",
},
},
}
m := jsonMap.ToMapString("/")
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k, m[k])
}
// Output:
// /a/sub-a value-sub-a
// /b/sub-b value-sub-b
// /c 123
// /d 3.14
// /e true
// /x/a/sub-a value-sub-a
// /x/b/sub-b value-sub-b
// /x/c 123
// /x/d 3.14
// /x/e true
// /x/z/zz value-zz
}