-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
121 lines (107 loc) · 3.31 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
package rflutil
import (
"fmt"
"reflect"
)
type MapEntry struct {
Key reflect.Value
Value reflect.Value
}
// MapLen get number of entries in a map
func MapLen(m reflect.Value) (int, error) {
val := indirectValueTilRoot(m)
if !val.IsValid() || val.Kind() != reflect.Map {
return 0, fmt.Errorf("%w: require map type (got %v)", ErrTypeInvalid, m.Type())
}
return val.Len(), nil
}
// MapGet get value from a map by key
func MapGet[V any, K comparable](m reflect.Value, k K) (V, error) {
var ret V
val := indirectValueTilRoot(m)
if !val.IsValid() || val.Kind() != reflect.Map {
return ret, fmt.Errorf("%w: require map type (got %v)", ErrTypeInvalid, m.Type())
}
mapType := val.Type()
keyVal := reflect.ValueOf(k)
if !mapType.Key().AssignableTo(keyVal.Type()) {
return ret, fmt.Errorf("%w: key type is %v (expect %v)", ErrTypeUnmatched,
mapType.Key(), keyVal.Type())
}
valueVal := val.MapIndex(keyVal)
if !valueVal.IsValid() {
return ret, ErrNotFound
}
ret, ok := valueVal.Interface().(V)
if !ok {
return ret, fmt.Errorf("%w: value type is %v (expect %v)", ErrTypeUnmatched,
mapType.Elem(), reflect.TypeOf(ret))
}
return ret, nil
}
// MapSet set value for a key of a map
func MapSet[K comparable, V any](m reflect.Value, k K, v V) error {
val := indirectValueTilRoot(m)
if !val.IsValid() || val.Kind() != reflect.Map {
return fmt.Errorf("%w: require map type (got %v)", ErrTypeInvalid, m.Type())
}
mapType := val.Type()
keyVal := reflect.ValueOf(k)
if !mapType.Key().AssignableTo(keyVal.Type()) {
return fmt.Errorf("%w: key type is %v (expect %v)", ErrTypeUnmatched,
mapType.Key(), keyVal.Type())
}
valVal := reflect.ValueOf(v)
if !valVal.IsValid() {
if mapType.Elem().Kind() == reflect.Interface {
val.SetMapIndex(keyVal, reflect.Zero(mapType.Elem()))
return nil
}
}
if !mapType.Elem().AssignableTo(valVal.Type()) {
return fmt.Errorf("%w: value type is %v (expect %v)", ErrTypeUnmatched,
mapType.Elem(), valVal.Type())
}
val.SetMapIndex(keyVal, valVal)
return nil
}
// MapDelete delete the given key from a map
func MapDelete[K comparable](m reflect.Value, k K) error {
val := indirectValueTilRoot(m)
if !val.IsValid() || val.Kind() != reflect.Map {
return fmt.Errorf("%w: require map type (got %v)", ErrTypeInvalid, m.Type())
}
mapType := val.Type()
keyVal := reflect.ValueOf(k)
if !mapType.Key().AssignableTo(keyVal.Type()) {
return fmt.Errorf("%w: key type is %v (expect %v)", ErrTypeUnmatched,
mapType.Key(), keyVal.Type())
}
// Set zero value means delete the key from the map
val.SetMapIndex(keyVal, reflect.Value{})
return nil
}
// MapKeys get all keys of a map
func MapKeys(m reflect.Value) ([]reflect.Value, error) {
val := indirectValueTilRoot(m)
if !val.IsValid() || val.Kind() != reflect.Map {
return nil, fmt.Errorf("%w: require map type (got %v)", ErrTypeInvalid, m.Type())
}
return val.MapKeys(), nil
}
// MapEntries get all entries of a map
func MapEntries(m reflect.Value) ([]MapEntry, error) {
val := indirectValueTilRoot(m)
if !val.IsValid() || val.Kind() != reflect.Map {
return nil, fmt.Errorf("%w: require map type (got %v)", ErrTypeInvalid, m.Type())
}
result := make([]MapEntry, 0, val.Len())
iter := val.MapRange()
for iter.Next() {
result = append(result, MapEntry{
Key: iter.Key(),
Value: iter.Value(),
})
}
return result, nil
}