-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
177 lines (142 loc) · 4.38 KB
/
reflect.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Author: fasion
* Created time: 2023-12-21 14:28:34
* Last Modified by: fasion
* Last Modified time: 2023-12-21 17:12:23
*/
package baseutils
import (
"reflect"
"github.com/fasionchan/goutils/stl"
)
func DereferenceReflectPointer(ptr reflect.Value) (result reflect.Value, ok bool) {
for {
switch ptr.Kind() {
case reflect.Ptr:
if ptr.IsNil() {
return
}
ptr = ptr.Elem()
default:
return ptr, true
}
}
}
func DereferenceReflectPointerWithError(ptr reflect.Value) (result reflect.Value, err error) {
result, ok := DereferenceReflectPointer(ptr)
if !ok {
err = NewBadTypeError("nonpointer", "").WithGivenReflectType(ptr.Type())
}
return
}
func MapListToAnysByReflect(datas any) ([]any, error) {
return MapListByReflect(datas, stl.Echo[any])
}
func MapListByReflect[T any](datas any, mapper func(data any) T) ([]T, error) {
datasValue := reflect.ValueOf(datas)
datasValue, err := DereferenceReflectPointerWithError(datasValue)
if err != nil {
return nil, err
}
switch datasValue.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
default:
return nil, NewBadTypeError("slice/array/string", "").WithGivenReflectType(datasValue.Type())
}
return MapListByReflectValue(datasValue, mapper), nil
}
func MapListToAnysByReflectValue(datas reflect.Value) []any {
return MapListByReflectValue(datas, stl.Echo[any])
}
func MapListByReflectValue[T any](datas reflect.Value, mapper func(data any) T) []T {
return stl.MapIterator[any](NewSliceIteratorByReflectValue(datas), mapper)
}
func MapKeyValuePairPtrsByReflect(m any) (stl.KeyValuePairPtrs[any, any], error) {
return MapMapWithReflect(m, stl.Reference[stl.KeyValuePair[any, any]])
}
func MapKeyValuePairPtrsByReflectValue(m reflect.Value) stl.KeyValuePairPtrs[any, any] {
return MapMapWithReflectValue(m, stl.Reference[stl.KeyValuePair[any, any]])
}
func MapKeyValuePairsByReflect(m any) (stl.KeyValuePairs[any, any], error) {
return MapMapWithReflect(m, stl.Echo[stl.KeyValuePair[any, any]])
}
func MapKeyValuePairsByReflectValue(m reflect.Value) stl.KeyValuePairs[any, any] {
return MapMapWithReflectValue(m, stl.Echo[stl.KeyValuePair[any, any]])
}
func MapMapWithReflect[T any](m any, mapper func(stl.KeyValuePair[any, any]) T) ([]T, error) {
mValue := reflect.ValueOf(m)
mValue, err := DereferenceReflectPointerWithError(mValue)
if err != nil {
return nil, err
}
if mValue.Kind() != reflect.Map {
return nil, NewBlankBadTypeError().WithExpected("map").WithGivenReflectType(mValue.Type())
}
return MapMapWithReflectValue(mValue, mapper), nil
}
func MapMapWithReflectValue[T any](m reflect.Value, mapper func(stl.KeyValuePair[any, any]) T) []T {
return stl.MapIterator[stl.KeyValuePair[any, any]](NewMapIteratorByReflect(m), mapper)
}
func IteratorDataToTypelessSliceByReflect(data any) ([]any, error) {
return IteratorDataToTypelessSliceByReflectValue(reflect.ValueOf(data))
}
func IteratorDataToTypelessSliceByReflectValue(data reflect.Value) ([]any, error) {
data, err := DereferenceReflectPointerWithError(data)
if err != nil {
return nil, err
}
switch data.Kind() {
case reflect.Slice, reflect.Array, reflect.String:
return MapListToAnysByReflectValue(data), nil
case reflect.Map:
return MapKeyValuePairsByReflectValue(data).ToTypelessSlice(), nil
default:
return nil, NewBlankBadTypeError().WithExpected("slice/array/string/map").WithGivenReflectType(data.Type())
}
}
type ListIteratorByReflect struct {
slice reflect.Value
n int
i int
}
func NewSliceIteratorByReflectValue(slice reflect.Value) *ListIteratorByReflect {
return &ListIteratorByReflect{
slice: slice,
n: slice.Len(),
i: -1,
}
}
func (it *ListIteratorByReflect) Len() int {
return it.n
}
func (it *ListIteratorByReflect) Next() bool {
it.i++
return it.i < it.n
}
func (it *ListIteratorByReflect) Data() any {
return it.slice.Index(it.i).Interface()
}
type MapIteratorByReflect struct {
m reflect.Value
n int
iter *reflect.MapIter
}
func NewMapIteratorByReflect(m reflect.Value) *MapIteratorByReflect {
return &MapIteratorByReflect{
m: m,
n: m.Len(),
iter: m.MapRange(),
}
}
func (it *MapIteratorByReflect) Len() int {
return it.n
}
func (it *MapIteratorByReflect) Next() bool {
return it.iter.Next()
}
func (it *MapIteratorByReflect) Data() stl.KeyValuePair[any, any] {
return stl.KeyValuePair[any, any]{
Key: it.iter.Key().Interface(),
Value: it.iter.Value().Interface(),
}
}