-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap.go
334 lines (308 loc) · 11 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2014 Dmitry Ponomarev <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gocast
import (
"context"
"reflect"
)
// TryMapCopy converts source into destination or return error
func TryMapCopy[K comparable, V any](dst map[K]V, src any, recursive bool, tags ...string) error {
return TryMapCopyContext(context.Background(), dst, src, recursive, tags...)
}
// TryMapCopyContext converts source into destination or return error
func TryMapCopyContext[K comparable, V any](ctx context.Context, dst map[K]V, src any, recursive bool, tags ...string) error {
if dst == nil || src == nil {
if dst == nil {
return wrapError(ErrInvalidParams, "TryMapCopyContext `destenation` parameter is nil")
}
return wrapError(ErrInvalidParams, "TryMapCopyContext `source` parameter is nil")
}
var (
srcVal = reflectTarget(reflect.ValueOf(src))
srcType = srcVal.Type()
)
switch srcType.Kind() {
case reflect.Map:
for _, k := range srcVal.MapKeys() {
field := srcVal.MapIndex(k)
key, err := TryCast[K](k.Interface())
if err == nil {
if recursive {
dst[key], err = TryCastRecursiveContext[V](ctx, field.Interface(), tags...)
} else {
dst[key], err = TryCastContext[V](ctx, field.Interface(), tags...)
}
}
if err != nil {
return wrapError(err, `"`+Str(k.Interface())+`" map key`)
}
}
case reflect.Struct:
for i := 0; i < srcVal.NumField(); i++ {
name, omitempty := fieldNameFromTags(srcType.Field(i), tags...)
if len(name) > 0 {
key, err := TryCast[K](name)
if err != nil {
return err
}
field := srcVal.Field(i)
fl := getValue(field.Interface())
if !omitempty || !IsEmpty(fl) {
if recursive {
dst[key], err = TryCastRecursiveContext[V](ctx, fl, tags...)
} else {
dst[key], err = TryCastContext[V](ctx, fl, tags...)
}
if err != nil {
return wrapError(err, "`"+name+"` struct key")
}
} // end if !omitempty || !IsEmpty(fl)
}
}
default:
return wrapError(ErrUnsupportedSourceType, srcType.String())
}
return nil
}
// ToMap cast your Source into the Destination type
// tag defines the tags name in the structure to map the keys
func ToMap(dst, src any, recursive bool, tags ...string) error {
return ToMapContext(context.Background(), dst, src, recursive, tags...)
}
// ToMap cast your Source into the Destination type
// tag defines the tags name in the structure to map the keys
func ToMapContext(ctx context.Context, dst, src any, recursive bool, tags ...string) error {
if dst == nil || src == nil {
if dst == nil {
return wrapError(ErrInvalidParams, "ToMapContext `destenation` parameter is nil")
}
return wrapError(ErrInvalidParams, "ToMapContext `source` parameter is nil")
}
var (
err error
destVal = reflectTarget(reflect.ValueOf(dst))
destType = destVal.Type()
srcVal = reflectTarget(reflect.ValueOf(src))
srcType = srcVal.Type()
)
if dst = destVal.Interface(); dst == nil {
dst = reflect.MakeMap(destType).Interface()
destVal = reflect.ValueOf(dst)
}
switch dest := dst.(type) {
case map[any]any:
switch srcType.Kind() {
case reflect.Map:
for _, k := range srcVal.MapKeys() {
field := srcVal.MapIndex(k)
if recursive {
dest[k.Interface()], err = mapDestValue(field.Interface(), destType, recursive, tags...)
if err != nil {
return wrapError(err, Str(k.Interface()))
}
} else {
dest[k.Interface()] = field.Interface()
}
}
case reflect.Struct:
for i := 0; i < srcVal.NumField(); i++ {
name, omitempty := fieldNameFromTags(srcType.Field(i), tags...)
if len(name) > 0 {
field := srcVal.Field(i)
fl := getValue(field.Interface())
if !omitempty || !IsEmpty(fl) {
if recursive {
dest[name], err = mapDestValue(fl, destType, recursive, tags...)
if err != nil {
return wrapError(err, "`"+name+"` value")
}
} else {
dest[name] = fl
}
} // end if !omitempty || !IsEmpty(fl)
}
}
default:
err = wrapError(ErrUnsupportedSourceType, srcType.String())
}
case map[string]any:
err = TryMapCopyContext(ctx, dest, src, recursive, tags...)
case map[string]string:
err = TryMapCopyContext(ctx, dest, src, recursive, tags...)
default:
switch destType.Kind() {
case reflect.Map, reflect.Struct:
keyType := destType.Key()
elemType := destType.Elem()
switch srcType.Kind() {
case reflect.Map:
for _, k := range srcVal.MapKeys() {
keyVal, err := ReflectTryToTypeContext(ctx, k, keyType, recursive, tags...)
if err != nil {
return wrapError(err, Str(k.Interface()))
}
mapVal := reflectTarget(srcVal.MapIndex(k))
val, err := ReflectTryToTypeContext(ctx, mapVal, elemType, recursive, tags...)
if err != nil {
return wrapError(err, "`"+Str(k.Interface())+"` value")
}
destVal.SetMapIndex(reflect.ValueOf(keyVal), reflect.ValueOf(val))
}
case reflect.Struct:
for i := 0; i < srcVal.NumField(); i++ {
name, omitempty := fieldNameFromTags(srcType.Field(i), tags...)
if len(name) > 0 {
flVal := reflectTarget(srcVal.Field(i))
fl := getValue(flVal.Interface())
if !omitempty || !IsEmpty(fl) {
keyVal, err := TryToType(name, keyType)
if err != nil {
return wrapError(err, name)
}
val, err := ReflectTryToTypeContext(ctx, flVal, elemType, recursive, tags...)
if err != nil {
return wrapError(err, "`"+name+"` value")
}
destVal.SetMapIndex(reflect.ValueOf(keyVal), reflect.ValueOf(val))
}
} // end if
}
default:
err = wrapError(ErrUnsupportedSourceType, srcType.String())
}
default:
err = wrapError(ErrUnsupportedType, destType.String())
}
}
return err
}
// TryMapFrom source creates new map to convert
func TryMapFrom[K comparable, V any](src any, recursive bool, tags ...string) (map[K]V, error) {
return TryMapFromContext[K, V](context.Background(), src, recursive, tags...)
}
// TryMapFrom source creates new map to convert
func TryMapFromContext[K comparable, V any](ctx context.Context, src any, recursive bool, tags ...string) (map[K]V, error) {
dst := make(map[K]V)
err := TryMapCopyContext(ctx, dst, src, recursive, tags...)
return dst, err
}
// TryMapRecursive creates new map to convert from soruce type with recursive field processing
func TryMapRecursive[K comparable, V any](src any, tags ...string) (map[K]V, error) {
return TryMapFrom[K, V](src, true, tags...)
}
// TryMapRecursiveContext creates new map to convert from soruce type with recursive field processing
func TryMapRecursiveContext[K comparable, V any](ctx context.Context, src any, tags ...string) (map[K]V, error) {
return TryMapFromContext[K, V](ctx, src, true, tags...)
}
// TryMap creates new map to convert from soruce type
func TryMap[K comparable, V any](src any, tags ...string) (map[K]V, error) {
return TryMapFrom[K, V](src, false, tags...)
}
// TryMapContext creates new map to convert from soruce type
func TryMapContext[K comparable, V any](ctx context.Context, src any, tags ...string) (map[K]V, error) {
return TryMapFromContext[K, V](ctx, src, false, tags...)
}
// MapRecursive creates map from source or returns nil
func MapRecursive[K comparable, V any](src any, tags ...string) map[K]V {
m, _ := TryMapRecursive[K, V](src, tags...)
return m
}
// MapRecursiveContext creates map from source or returns nil
func MapRecursiveContext[K comparable, V any](ctx context.Context, src any, tags ...string) map[K]V {
m, _ := TryMapRecursiveContext[K, V](ctx, src, tags...)
return m
}
// Map creates map from source or returns nil
func Map[K comparable, V any](src any, tags ...string) map[K]V {
m, _ := TryMap[K, V](src, tags...)
return m
}
// MapContext creates map from source or returns nil
func MapContext[K comparable, V any](ctx context.Context, src any, tags ...string) map[K]V {
m, _ := TryMapContext[K, V](ctx, src, tags...)
return m
}
// ToMapFrom any Map/Object type
func ToMapFrom(src any, recursive bool, tags ...string) (map[any]any, error) {
dst := make(map[any]any)
err := ToMap(dst, src, recursive, tags...)
return dst, err
}
// ToSiMap converts input Map/Object type into the map[string]any
//
// Deprecated: Use TryMapFrom[string, any](...) instead
func ToSiMap(src any, recursive bool, tags ...string) (map[string]any, error) {
return TryMapFrom[string, any](src, recursive, tags...)
}
// ToStringMap converts input Map/Object type into the map[string]string
//
// Deprecated: Use TryMapFrom[string, string](...) instead
func ToStringMap(src any, recursive bool, tags ...string) (map[string]string, error) {
return TryMapFrom[string, string](src, recursive, tags...)
}
// IsMap checks if the input value is a Map/Object type
func IsMap(v any) bool {
switch v.(type) {
case map[any]any, map[string]any, map[string]string, map[string]int, map[int]int:
return true
default:
return reflect.ValueOf(v).Kind() == reflect.Map
}
}
///////////////////////////////////////////////////////////////////////////////
/// MARK: Helpers
///////////////////////////////////////////////////////////////////////////////
func reflectMapValueByStringKeys(src reflect.Value, keys []string) any {
mKeys := src.MapKeys()
for _, key := range keys {
for _, mKey := range mKeys {
if Str(mKey.Interface()) == key {
return src.MapIndex(mKey).Interface()
}
}
}
return nil
}
func mapDestValue(fl any, destType reflect.Type, recursive bool, tags ...string) (any, error) {
field := reflect.ValueOf(fl)
switch field.Kind() {
case reflect.Slice, reflect.Array:
if field.Len() > 0 {
switch field.Index(0).Kind() {
case reflect.Map, reflect.Struct:
list := make([]any, field.Len())
for i := 0; i < field.Len(); i++ {
var v any = reflect.New(destType)
if err := ToMap(v, field.Index(i), recursive, tags...); err != nil {
return nil, err
}
list = append(list, v)
}
return list, nil
}
}
case reflect.Map, reflect.Struct:
var v any = reflect.New(destType)
if err := ToMap(v, fl, recursive, tags...); err != nil {
return nil, err
}
return v, nil
}
return fl, nil
}