/
values.go
266 lines (252 loc) · 9.38 KB
/
values.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 utils
import (
"encoding/json"
"fmt"
)
// ToValuesMap converts the given value v to a values map, by first marshalling it to JSON,
// and then unmarshalling the result from JSON into a values map.
// If v cannot be marshalled to JSON, or if the result cannot be unmarshalled into a values map, an error is returned.
func ToValuesMap(v interface{}) (map[string]interface{}, error) {
var m map[string]interface{}
if err := convert(v, &m); err != nil {
return nil, err
}
return m, nil
}
// FromValuesMap converts the given values map values to the given value v, by first marshalling it to JSON,
// and then unmarshalling the result from JSON into v.
// If values cannot be marshalled to JSON, or if the result cannot be unmarshalled into v, an error is returned.
func FromValuesMap(values map[string]interface{}, v interface{}) error {
return convert(values, v)
}
// InitValuesMap returns the given values map if it is non-nil, or a newly allocated values map if it is nil.
func InitValuesMap(values map[string]interface{}) map[string]interface{} {
if values == nil {
values = make(map[string]interface{})
}
return values
}
// GetFromValuesMap returns the element at the specified location in the given values map,
// e.g. GetFromValuesMap(values, "a", 0, "b") returns values["a"][0]["b"], if such an element exists.
// If such an element does not exist, it returns nil.
// All keys must be of type either string (for map keys) or int (for slice indexes).
// If a key type doesn't match the corresponding element type (string for map, int for slice), an error is returned.
func GetFromValuesMap(values map[string]interface{}, keys ...interface{}) (interface{}, error) {
return getFromValues(fromMap(values), keys...)
}
// SetToValuesMap sets the given element v to the specified location in the given values map,
// e.g. SetToValuesMap(values, v, "a", 0, "b") sets values["a"][0]["b"] = v.
// All map and slice elements along the way that don't exist are created and set at their corresponding locations.
// All keys must be of type either string (for map keys) or int (for slice indexes).
// Slice indexes must refer to existing slice elements or the first element beyond the end of a slice.
// If a key type doesn't match the corresponding element type (string for map, int for slice), an error is returned.
func SetToValuesMap(values map[string]interface{}, v interface{}, keys ...interface{}) (map[string]interface{}, error) {
x, err := setToValues(fromMap(values), v, keys...)
return toMap(x), err
}
// DeleteFromValuesMap deletes the element at the specified location in the given values map,
// e.g. DeleteFromValuesMap(values, "a", 0, "b") deletes values["a"][0]["b"].
// If such an element does not exist, it returns the given values map unmodified.
// All keys must be of type either string (for map keys) or int (for slice indexes).
// If a key type doesn't match the corresponding element type (string for map, int for slice), an error is returned.
func DeleteFromValuesMap(values map[string]interface{}, keys ...interface{}) (map[string]interface{}, error) {
x, err := deleteFromValues(fromMap(values), keys...)
return toMap(x), err
}
// convert converts x to y by first marshalling x to JSON, and then unmarshalling the result from JSON into y.
// If x cannot be marshalled to JSON, or if the result cannot be unmarshalled into y, an error is returned.
func convert(x, y interface{}) error {
jsonBytes, err := json.Marshal(x)
if err != nil {
return err
}
if err := json.Unmarshal(jsonBytes, y); err != nil {
return err
}
return nil
}
// getFromValues returns the element at the specified location in the given values (either map or slice),
// e.g. getFromValues(values, "a", 0, "b") returns values["a"][0]["b"], if such an element exists.
// If such an element does not exist, it returns nil.
// All keys must be of type either string (for map keys) or int (for slice indexes).
// If a key type doesn't match the corresponding element type (string for map, int for slice), an error is returned.
func getFromValues(values interface{}, keys ...interface{}) (interface{}, error) {
if values == nil {
return nil, nil
}
if len(keys) == 0 {
return values, nil
}
var ok bool
switch keys[0].(type) {
case string:
key := keys[0].(string)
var m map[string]interface{}
if m, ok = values.(map[string]interface{}); !ok {
return nil, fmt.Errorf("cannot use key '%s' with a non-map value", key)
}
if _, ok = m[key]; !ok {
return nil, nil
}
return getFromValues(m[key], keys[1:]...)
case int:
index := keys[0].(int)
var s []interface{}
if s, ok = values.([]interface{}); !ok {
return nil, fmt.Errorf("cannot use index '%d' with a non-slice value", index)
}
if index < 0 || index >= len(s) {
return nil, nil
}
return getFromValues(s[index], keys[1:]...)
default:
return nil, fmt.Errorf("key '%v' must be of type string or int", keys[0])
}
}
// setToValues sets the given element v to the specified location in the given values (either map or slice),
// e.g. setToValues(values, v, "a", 0, "b") sets values["a"][0]["b"] = v.
// All map and slice elements along the way that don't exist are created and set at their corresponding locations.
// All keys must be of type either string (for map keys) or int (for slice indexes).
// Slice indexes must refer to existing slice elements or the first element beyond the end of a slice.
// If a key type doesn't match the corresponding element type (string for map, int for slice), an error is returned.
func setToValues(values interface{}, v interface{}, keys ...interface{}) (interface{}, error) {
if len(keys) == 0 {
return values, nil
}
var ok bool
switch keys[0].(type) {
case string:
key := keys[0].(string)
if values == nil {
values = map[string]interface{}{}
}
var m map[string]interface{}
if m, ok = values.(map[string]interface{}); !ok {
return values, fmt.Errorf("cannot use key '%s' with a non-map value", key)
}
if len(keys) == 1 {
m[key] = v
} else {
x, err := setToValues(m[key], v, keys[1:]...)
if err != nil {
return m, err
}
m[key] = x
}
return m, nil
case int:
index := keys[0].(int)
if values == nil {
values = []interface{}{}
}
var s []interface{}
if s, ok = values.([]interface{}); !ok {
return values, fmt.Errorf("cannot use index '%d' with a non-slice value", index)
}
if index >= 0 && index < len(s) {
if len(keys) == 1 {
s[index] = v
} else {
x, err := setToValues(s[index], v, keys[1:]...)
if err != nil {
return s, err
}
s[index] = x
}
} else if index == len(s) {
if len(keys) == 1 {
s = append(s, v)
} else {
x, err := setToValues(nil, v, keys[1:]...)
if err != nil {
return s, err
}
s = append(s, x)
}
} else {
return s, fmt.Errorf("index '%d' out of range", index)
}
return s, nil
default:
return values, fmt.Errorf("key '%v' must be of type string or int", keys[0])
}
}
// deleteFromValues deletes the element at the specified location in the given values (either map or slice),
// e.g. deleteFromValues(values, "a", 0, "b") deletes values["a"][0]["b"].
// If such an element does not exist, it returns the given values unmodified.
// All keys must be of type either string (for map keys) or int (for slice indexes).
// If a key type doesn't match the corresponding element type (string for map, int for slice), an error is returned.
func deleteFromValues(values interface{}, keys ...interface{}) (interface{}, error) {
if values == nil {
return nil, nil
}
if len(keys) == 0 {
return values, nil
}
var ok bool
switch keys[0].(type) {
case string:
key := keys[0].(string)
var m map[string]interface{}
if m, ok = values.(map[string]interface{}); !ok {
return values, fmt.Errorf("cannot use key '%s' with a non-map value", key)
}
if _, ok = m[key]; ok {
if len(keys) == 1 {
delete(m, key)
} else {
x, err := deleteFromValues(m[key], keys[1:]...)
if err != nil {
return m, err
}
m[key] = x
}
}
return m, nil
case int:
index := keys[0].(int)
var s []interface{}
if s, ok = values.([]interface{}); !ok {
return values, fmt.Errorf("cannot use index '%d' with a non-slice value", index)
}
if index >= 0 && index < len(s) {
if len(keys) == 1 {
s = append(s[:index], s[index+1:]...)
} else {
x, err := deleteFromValues(s[index], keys[1:]...)
if err != nil {
return s, err
}
s[index] = x
}
}
return s, nil
default:
return values, fmt.Errorf("key '%v' must be of type string or int", keys[0])
}
}
func fromMap(values map[string]interface{}) interface{} {
if values == nil {
return nil
}
return values
}
func toMap(x interface{}) map[string]interface{} {
if x == nil {
return nil
}
return x.(map[string]interface{})
}