Repository navigation
Expand file tree
/
Copy pathmap.go
More file actions
96 lines (81 loc) · 2.71 KB
/
Copy pathmap.go
File metadata and controls
96 lines (81 loc) · 2.71 KB
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
package any
import (
"fmt"
"reflect"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/kun/share"
)
// Map the replacement for Maps.Map
type Map struct {
maps.MapStrAny
}
// MakeMap create a new map instance
func MakeMap() Map {
return Map{MapStrAny: maps.MapStrAnyOf(map[string]interface{}{})}
}
// MapOf create a new map instance
func MapOf(values interface{}) Map {
if values == nil {
return MakeMap()
}
switch values.(type) {
case map[string]interface{}:
return Map{MapStrAny: maps.MapStrAnyOf(values.(map[string]interface{}))}
}
// converts to map
reflectValues := reflect.ValueOf(values)
reflectValues = reflect.Indirect(reflectValues)
if reflectValues.Kind() == reflect.Map {
valuesMap := map[string]interface{}{}
for _, key := range reflectValues.MapKeys() {
k := fmt.Sprintf("%v", key)
valuesMap[k] = reflectValues.MapIndex(key).Interface()
}
return Map{MapStrAny: maps.MapStrAnyOf(valuesMap)}
} else if reflectValues.Kind() == reflect.Struct {
valuesMap := map[string]interface{}{}
typeOfS := reflectValues.Type()
for i := 0; i < reflectValues.NumField(); i++ {
name := share.GetTagName(typeOfS.Field(i), "json")
valuesMap[name] = reflectValues.Field(i).Interface()
}
return Map{MapStrAny: maps.MapStrAnyOf(valuesMap)}
}
panic(fmt.Sprintf("v is %s not a type of map", reflectValues.Kind()))
}
// Any returns the value stored in the map for a key.
func (m Map) Any(key string) *Any {
return Of(m.Get(key))
}
// Flatten The Flatten method is alias of Dot, to flatten a multi-dimensional map[string]inteface{} into a single level map[string]inteface{}
// that uses "dot" notation to indicate depth
func (m Map) Flatten() Map {
return m.Dot()
}
// Dot The Dot method flattens a multi-dimensional map[string]inteface{} into a single level map[string]inteface{}
// that uses "dot" notation to indicate depth
func (m Map) Dot() Map {
res := MakeMap()
m.Range(func(key string, value interface{}) bool {
res.dotSet(key, value)
return true
})
return res
}
// dotSet set the value for a key uses "dot" notation
func (m Map) dotSet(key string, value interface{}) {
m.Set(key, value)
reflectValue := reflect.ValueOf(value)
reflectValue = reflect.Indirect(reflectValue)
valueKind := reflectValue.Kind()
if valueKind == reflect.Slice || valueKind == reflect.Array { // Slice || Array
for i := 0; i < reflectValue.Len(); i++ {
m.dotSet(fmt.Sprintf("%s.%d", key, i), reflectValue.Index(i).Interface()) // array.0
m.dotSet(fmt.Sprintf("%s[%d]", key, i), reflectValue.Index(i).Interface()) // array[0]
}
} else if valueKind == reflect.Map { // Map
for _, sub := range reflectValue.MapKeys() {
m.dotSet(fmt.Sprintf("%s.%v", key, sub), reflectValue.MapIndex(sub).Interface())
}
}
}