-
Notifications
You must be signed in to change notification settings - Fork 1
/
to.go
205 lines (170 loc) · 3.99 KB
/
to.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package unmarshal
import (
"fmt"
"reflect"
"github.com/Foxcapades/Argonaut/internal/xreflect"
)
type InvalidTypeError struct {
Value reflect.Value
}
func (i *InvalidTypeError) Error() string {
return fmt.Sprintf("cannot unmarshal type %s", i.Value.Type())
}
type InvalidUnmarshalError struct {
Value reflect.Value
Argument string
}
func (i InvalidUnmarshalError) Error() string {
if xreflect.IsNil(&i.Value) {
return "attempted to unmarshal into nil"
}
return "attempted to unmarshal into a non-pointer"
}
func ToUnmarshalable(
arg string,
ov reflect.Value,
skipPtr bool,
unmarshalerType reflect.Type,
) (reflect.Value, error) {
if !skipPtr && ((ov.Kind() != reflect.Ptr && ov.Kind() != reflect.Func) || xreflect.IsNil(&ov)) {
return reflect.Value{}, &InvalidUnmarshalError{Value: ov, Argument: arg}
}
v := GetRootValue(ov, unmarshalerType)
kind := v.Kind()
if xreflect.IsBasic(v.Type()) {
return v, nil
}
if v.Type().AssignableTo(unmarshalerType) {
return v, nil
}
if kind == reflect.Slice {
return ToValidSlice(v, ov, unmarshalerType)
}
if kind == reflect.Map {
return ToValidMap(v, ov, unmarshalerType)
}
if kind == reflect.Interface {
return v, nil
}
if kind == reflect.Func {
return v, nil
}
return reflect.Value{}, &InvalidTypeError{Value: v}
}
// ToValidSlice is an internal method that is not exposed to package consumers.
//
// Valid slice types:
// []<basic>
// []<*basic>
// [][]byte
// []*[]byte
func ToValidSlice(v, ov reflect.Value, ut reflect.Type) (out reflect.Value, err error) {
err = ValidateContainerValue(v.Type().Elem(), ov, ut)
if err != nil {
return
}
return v, nil
}
// ToValidMap is butts and this comment line is meaningless.
//
// Valid map types:
// map[<basic>]<basic>
// map[<basic>]<*basic>
// map[<basic>][]byte
// map[<basic>]<*[]byte>
func ToValidMap(v, ov reflect.Value, ut reflect.Type) (reflect.Value, error) {
vt := v.Type()
if !xreflect.IsBasic(vt.Key()) {
return reflect.Value{}, &InvalidTypeError{Value: ov}
}
if err := ValidateContainerValue(vt.Elem(), ov, ut); err != nil {
return reflect.Value{}, err
}
return v, nil
}
func ValidateContainerValue(t reflect.Type, ov reflect.Value, ut reflect.Type) error {
sk := t.Kind()
if xreflect.IsBasic(t) {
return nil
}
if sk == reflect.Ptr {
if xreflect.IsBasic(t.Elem()) {
return nil
}
if xreflect.IsByteSlice(t.Elem()) {
return nil
}
if xreflect.IsUnmarshaler(t, ut) || xreflect.IsUnmarshaler(t.Elem(), ut) {
return nil
}
if xreflect.IsInterface(t.Elem()) {
return nil
}
return &InvalidTypeError{Value: ov}
}
if xreflect.IsByteSlice(t) {
return nil
}
if xreflect.IsBasicSlice(t) {
return nil
}
if xreflect.IsUnmarshaler(t, ut) {
return nil
}
if xreflect.IsUnmarshalerSlice(t, ut) {
return nil
}
if xreflect.IsInterface(t) {
return nil
}
return &InvalidTypeError{Value: ov}
}
// GetRootValue returns the root of the given value. The root will be the
// dereferenced type of the given reflect.Value instance.
//
// Parameters:
// 1. v = reflect.Value of the type whose root value should be determined.
// 2. ut = unmarshaler type to test if the given value is an argo.Unmarshaler
// instance.
func GetRootValue(v reflect.Value, ut reflect.Type) reflect.Value {
// Used for recursion detection
c := v
haveAddr := false
// see json.Unmarshaler indirect()
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
haveAddr = true
v = v.Addr()
}
for {
if v.Kind() == reflect.Interface && !v.IsNil() {
tmp := v.Elem()
if tmp.Kind() == reflect.Ptr && !tmp.IsNil() {
haveAddr = false
v = tmp
continue
}
}
if v.Kind() != reflect.Ptr {
break
}
if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v {
v = v.Elem()
break
}
if v.IsNil() {
if v.CanAddr() {
v.Set(reflect.New(v.Type().Elem()))
}
}
if v.Type().AssignableTo(ut) {
break
}
if haveAddr {
v = c
haveAddr = false
} else {
v = v.Elem()
}
}
return v
}