forked from ironbay/dynamic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic.go
165 lines (150 loc) · 3.45 KB
/
dynamic.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
package dynamic
import (
"encoding/json"
"strings"
)
func Empty() map[string]interface{} {
return map[string]interface{}{}
}
func Set(input map[string]interface{}, value interface{}, path ...string) map[string]interface{} {
if len(path) == 0 {
return input
}
field, path := path[len(path)-1], path[:len(path)-1]
current := input
for _, segment := range path {
next := current[segment]
if next == nil {
next = map[string]interface{}{}
current[segment] = next
} else {
_, ok := next.(map[string]interface{})
if !ok {
next = map[string]interface{}{}
current[segment] = next
}
}
current = next.(map[string]interface{})
}
current[field] = value
return current
}
func Get(input map[string]interface{}, path ...string) interface{} {
field, rest := path[len(path)-1], path[:len(path)-1]
current := input
var ok bool
for _, segment := range rest {
next := current[segment]
if next == nil {
return nil
}
current, ok = next.(map[string]interface{})
if !ok {
return nil
}
}
return current[field]
}
func Keys(input map[string]interface{}) []string {
result := []string{}
for key, _ := range input {
result = append(result, key)
}
return result
}
func Inflate(input map[string]interface{}) map[string]interface{} {
for key, value := range input {
splits := strings.Split(key, ".")
if casted, ok := value.(map[string]interface{}); ok {
value = Inflate(casted)
}
delete(input, key)
Set(input, value, splits...)
}
return input
}
func Merge(destination, source map[string]interface{}) {
for key, value := range source {
nest, ok := value.(map[string]interface{})
if !ok {
destination[key] = value
continue
}
var match map[string]interface{}
exists, ok := destination[key]
if ok {
match, _ = exists.(map[string]interface{})
}
if match == nil {
match = make(map[string]interface{})
destination[key] = match
}
Merge(match, nest)
}
}
func Map(destination map[string]interface{}, arr []string) {
for _, val := range arr {
destination[val] = true
}
}
func String(input map[string]interface{}, path ...string) string {
match := Get(input, path...)
if match == nil {
return ""
}
return match.(string)
}
func Int(input map[string]interface{}, path ...string) int64 {
match := Get(input, path...)
if match == nil {
return 0
}
if number, ok := match.(json.Number); ok {
if i, err := number.Int64(); err == nil {
return i
}
return 0
}
return match.(int64)
}
func Float(input map[string]interface{}, path ...string) float64 {
match := Get(input, path...)
if match == nil {
return 0
}
if number, ok := match.(json.Number); ok {
if float, err := number.Float64(); err == nil {
return float
}
return 0
}
return match.(float64)
}
func Object(input map[string]interface{}, path ...string) map[string]interface{} {
match := Get(input, path...)
if match == nil {
return map[string]interface{}{}
}
return match.(map[string]interface{})
}
func Build(args ...interface{}) map[string]interface{} {
result := Empty()
for n := 0; n < len(args); n += 2 {
result[args[n].(string)] = args[n+1]
}
return result
}
func Array(input map[string]interface{}, path ...string) []interface{} {
match := Get(input, path...)
if match == nil {
return []interface{}{}
}
return match.([]interface{})
}
func Clone(input map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
for key, value := range input {
result[key] = value
}
return result
}