-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
78 lines (63 loc) · 1.54 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
package hybrid
import (
"reflect"
)
func MapInt64Int(s interface{}, key string, value string) map[int64]int {
m := imap(s, key, value)
ret := make(map[int64]int)
keys := m.MapKeys()
for i := 0; i < len(keys); i++ {
ret[keys[i].Int()] = int(m.MapIndex(keys[i]).Int())
}
return ret
}
func imap(s interface{}, key string, value string) reflect.Value {
// validate the first arg s
if s == nil {
panic("the first arg shouldn't be nil")
}
kind := reflect.TypeOf(s).Kind()
if kind != reflect.Array && kind != reflect.Slice {
panic("only array and slice are supported for mapping")
}
// find elem type
elem := reflect.TypeOf(s).Elem()
if elem.Kind() == reflect.Ptr {
elem = elem.Elem()
}
// validate key and value
keyExist := false
valueExist := false
var keyType reflect.Type
var valueType reflect.Type
for i := 0; i < elem.NumField(); i++ {
field := elem.Field(i)
if key == field.Name {
keyExist = true
keyType = field.Type
}
if value == field.Name {
valueExist = true
valueType = field.Type
}
}
if !keyExist {
panic("key doesn't exist in the struct")
}
if !valueExist {
panic("value doesn't exist in the struct")
}
length := reflect.ValueOf(s).Len()
mapType := reflect.MapOf(keyType, valueType)
m := reflect.MakeMap(mapType)
for i := 0; i < length; i++ {
structVal := reflect.ValueOf(s).Index(i)
if structVal.Kind() == reflect.Ptr {
structVal = structVal.Elem()
}
keyVal := structVal.FieldByName(key)
valueVal := structVal.FieldByName(value)
m.SetMapIndex(keyVal, valueVal)
}
return m
}