-
Notifications
You must be signed in to change notification settings - Fork 8
/
map.go
53 lines (43 loc) · 1.04 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
package reflects
import (
"reflect"
)
type MapValue reflect.Value
func MapOf(v reflect.Value) MapValue {
if v.Kind() == reflect.Ptr {
if v.IsNil() {
t := v.Type().Elem()
v.Set(reflect.New(t))
}
v = v.Elem()
}
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}
return MapValue(v)
}
func (mv MapValue) Set(key, value interface{}) {
mv.SetValue(reflect.ValueOf(key), reflect.ValueOf(value))
}
func (mv MapValue) SetValue(key, value reflect.Value) {
v := (reflect.Value)(mv)
v.SetMapIndex(key, value)
}
func (mv MapValue) Delete(key interface{}) {
mv.DeleteValue(reflect.ValueOf(key))
}
func (mv MapValue) DeleteValue(key reflect.Value) {
v := (reflect.Value)(mv)
v.SetMapIndex(key, reflect.Zero(v.Type().Elem()))
}
func (mv MapValue) Get(key interface{}) interface{} {
return mv.GetValue(reflect.ValueOf(key)).Interface()
}
func (mv MapValue) GetValue(key reflect.Value) reflect.Value {
v := (reflect.Value)(mv)
return v.MapIndex(key)
}
func (mv MapValue) Interface() interface{} {
v := (reflect.Value)(mv)
return v.Interface()
}