-
Notifications
You must be signed in to change notification settings - Fork 13
/
map.go
160 lines (146 loc) · 3.89 KB
/
map.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
// Copyright 2016-2020 The Libsacloud Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mapconv
import (
"fmt"
"reflect"
"strings"
)
// Map is wrapper of map[string]interface{}
type Map map[string]interface{}
// Map returns output map
func (m *Map) Map() map[string]interface{} {
return *m
}
// Set sets map value with dot-separated key
func (m *Map) Set(key string, value interface{}) {
keys := strings.Split(key, ".")
var dest map[string]interface{} = *m
for i, k := range keys {
last := i == len(keys)-1
isSlice := strings.HasPrefix(k, "[]")
k = strings.Replace(k, "[]", "", -1)
var v interface{}
if last {
v = value
}
if !last && isSlice {
values := valueToSlice(value)
var nestMap []map[string]interface{}
for _, value := range values {
nested := Map(map[string]interface{}{})
key := strings.Join(keys[i+1:], ".")
nested.Set(key, value)
nestMap = append(nestMap, nested)
}
if _, ok := dest[k]; !ok {
dest[k] = nestMap
} else {
existed, ok := dest[k].([]map[string]interface{})
if !ok {
dest[k] = nestMap
}
dest[k] = append(existed, nestMap...)
}
return
}
setValueWithDefault(dest, k, v)
if !last {
dest = dest[k].(map[string]interface{})
}
}
}
func valueToSlice(value interface{}) []interface{} {
v := reflect.ValueOf(value)
if v.Kind() == reflect.Slice {
ret := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
ret[i] = v.Index(i).Interface()
}
return ret
}
return []interface{}{value}
}
func setValueWithDefault(values map[string]interface{}, key string, value interface{}) {
if value == nil {
value = map[string]interface{}{}
}
if _, ok := values[key]; !ok {
values[key] = value
}
}
// Get returns map value with dot-separated key
func (m *Map) Get(key string) (interface{}, error) {
keys := strings.Split(key, ".")
targetMap := *m
for i, k := range keys {
last := i == len(keys)-1
k = strings.Replace(k, "[]", "", -1)
value := targetMap[k]
if value == nil || reflect.ValueOf(value).IsZero() {
return nil, nil
}
if last {
return value, nil
}
switch value := value.(type) {
case map[string]interface{}:
targetMap = value
case []interface{}:
var values []interface{}
for _, v := range value {
if _, ok := v.(map[string]interface{}); !ok {
return nil, fmt.Errorf("elements of key %q(part of %q) are not map[string]interface{}", k, key)
}
nested := Map(v.(map[string]interface{}))
key := strings.Join(keys[i+1:], ".")
nv, err := nested.Get(key)
if err != nil {
return nil, err
}
if nv != nil {
nvs, ok := nv.([]interface{})
if ok {
values = append(values, nvs...)
} else {
values = append(values, nv)
}
}
}
return values, nil
case []map[string]interface{}:
var values []interface{}
for _, v := range value {
nested := Map(v)
key := strings.Join(keys[i+1:], ".")
nv, err := nested.Get(key)
if err != nil {
return nil, err
}
if nv != nil {
nvs, ok := nv.([]interface{})
if ok {
values = append(values, nvs...)
} else {
values = append(values, nv)
}
}
}
return values, nil
default:
return nil, fmt.Errorf("key %q(part of %q) is not map[string]interface{} or []map[string]interface{}", k, key)
}
}
return nil, fmt.Errorf("failed output get input map: invalid state - key:%s values:%v", key, *m)
}