-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
vmConvertToXNotGo112.go
38 lines (32 loc) · 1018 Bytes
/
vmConvertToXNotGo112.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
// +build !go1.12
package vm
import (
"reflect"
)
// convertMap trys to covert the reflect.Value map to the map reflect.Type
func convertMap(rv reflect.Value, rt reflect.Type) (reflect.Value, error) {
rtKey := rt.Key()
rtElem := rt.Elem()
// create new map
// note creating slice as work around to create map
// just doing MakeMap can give incorrect type for defined types
newMap := reflect.MakeSlice(reflect.SliceOf(rt), 0, 1)
newMap = reflect.Append(newMap, reflect.MakeMap(reflect.MapOf(rtKey, rtElem))).Index(0)
// copy keys to new map
// Before Go 1.12 the only way to do this is to get all the keys.
// Note this is costly for large maps.
mapKeys := rv.MapKeys()
for i := 0; i < len(mapKeys); i++ {
newKey, err := convertReflectValueToType(mapKeys[i], rtKey)
if err != nil {
return rv, err
}
value := rv.MapIndex(mapKeys[i])
value, err = convertReflectValueToType(value, rtElem)
if err != nil {
return rv, err
}
newMap.SetMapIndex(newKey, value)
}
return newMap, nil
}