/
decode.go
218 lines (198 loc) · 5.22 KB
/
decode.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
206
207
208
209
210
211
212
213
214
215
216
217
218
package codec
import (
"encoding"
"strconv"
"unsafe"
)
// DecodeNil calls DecodeNil on v if v implements NilDecoder.
func DecodeNil(v any) error {
if v, ok := v.(NilDecoder); ok {
return v.DecodeNil()
}
return nil
}
// DecodeBool decodes a boolean value into v.
// If *v is a pointer to a bool, then a bool will be allocated.
// If v implements BoolDecoder, then DecodeBool(b) is called.
func DecodeBool(v any, b bool) error {
switch v := v.(type) {
case *bool:
*v = b
case **bool:
*Must(v) = b
case BoolDecoder:
return v.DecodeBool(b)
}
return nil
}
// DecodeNumber decodes a number encoded as a string into v.
// The following core types are supported:
// int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, and float64.
// Pointers to the above types are also supported, and will be allocated if necessary.
// The interface types IntDecoder, and FloatDecoder are also supported.
// If unable to decode into a numeric type, it will fall back to DecodeString.
func DecodeNumber(v any, n string) error {
switch v := v.(type) {
case *int:
return decodeSignedValue(v, n)
case **int:
return decodeSignedValue(Must(v), n)
case *int8:
return decodeSignedValue(v, n)
case **int8:
return decodeSignedValue(Must(v), n)
case *int16:
return decodeSignedValue(v, n)
case **int16:
return decodeSignedValue(Must(v), n)
case *int32:
return decodeSignedValue(v, n)
case **int32:
return decodeSignedValue(Must(v), n)
case *int64:
return decodeSignedValue(v, n)
case **int64:
return decodeSignedValue(Must(v), n)
case *uint:
return decodeUnsignedValue(v, n)
case **uint:
return decodeUnsignedValue(Must(v), n)
case *uint8:
return decodeUnsignedValue(v, n)
case **uint8:
return decodeUnsignedValue(Must(v), n)
case *uint16:
return decodeUnsignedValue(v, n)
case **uint16:
return decodeUnsignedValue(Must(v), n)
case *uint32:
return decodeUnsignedValue(v, n)
case **uint32:
return decodeUnsignedValue(Must(v), n)
case *uint64:
return decodeUnsignedValue(v, n)
case **uint64:
return decodeUnsignedValue(Must(v), n)
case *float32:
return decodeFloatValue(v, n)
case **float32:
return decodeFloatValue(Must(v), n)
case *float64:
return decodeFloatValue(v, n)
case **float64:
return decodeFloatValue(Must(v), n)
case IntDecoder[int]:
return decodeSigned(v, n)
case IntDecoder[int8]:
return decodeSigned(v, n)
case IntDecoder[int16]:
return decodeSigned(v, n)
case IntDecoder[int32]:
return decodeSigned(v, n)
case IntDecoder[int64]:
return decodeSigned(v, n)
case IntDecoder[uint]:
return decodeUnsigned(v, n)
case IntDecoder[uint8]:
return decodeUnsigned(v, n)
case IntDecoder[uint16]:
return decodeUnsigned(v, n)
case IntDecoder[uint32]:
return decodeUnsigned(v, n)
case IntDecoder[uint64]:
return decodeUnsigned(v, n)
case FloatDecoder[float32]:
return decodeFloat(v, n)
case FloatDecoder[float64]:
return decodeFloat(v, n)
}
return DecodeString(v, n)
}
func decodeSignedValue[T Signed](v *T, n string) error {
i, err := strconv.ParseInt(n, 10, int(unsafe.Sizeof(*v)))
if err != nil {
return err
}
*v = T(i)
return nil
}
func decodeSigned[T Signed](v IntDecoder[T], n string) error {
var x T
i, err := strconv.ParseInt(n, 10, int(unsafe.Sizeof(x))*8)
if err != nil {
return err
}
return v.DecodeInt(T(i))
}
func decodeUnsignedValue[T Unsigned](v *T, n string) error {
i, err := strconv.ParseUint(n, 10, int(unsafe.Sizeof(*v)))
if err != nil {
return err
}
*v = T(i)
return nil
}
func decodeUnsigned[T Unsigned](v IntDecoder[T], n string) error {
var x T
i, err := strconv.ParseUint(n, 10, int(unsafe.Sizeof(x))*8)
if err != nil {
return err
}
return v.DecodeInt(T(i))
}
func decodeFloatValue[T Float](v *T, n string) error {
f, err := strconv.ParseFloat(n, int(unsafe.Sizeof(*v)))
if err != nil {
return err
}
*v = T(f)
return nil
}
func decodeFloat[T Float](v FloatDecoder[T], n string) error {
var x T
f, err := strconv.ParseFloat(n, int(unsafe.Sizeof(x))*8)
if err != nil {
return err
}
return v.DecodeFloat(T(f))
}
// DecodeString decodes s into v. The following types are supported:
// string, *string, and StringDecoder. It will fall back to DecodeBytes
// to attempt to decode into a byte slice or binary decoder.
func DecodeString(v any, s string) error {
switch v := v.(type) {
case *string:
*v = s
return nil
case **string:
*v = &s
return nil
case StringDecoder:
return v.DecodeString(s)
}
return DecodeBytes(v, []byte(s))
}
// DecodeBytes decodes data into v. The following types are supported:
// []byte, BytesDecoder, encoding.BinaryUnmarshaler, and encoding.TextUnmarshaler.
func DecodeBytes(v any, data []byte) error {
switch v := v.(type) {
case *[]byte:
Resize(v, len(data))
copy(*v, data)
case BytesDecoder:
return v.DecodeBytes(data)
case encoding.BinaryUnmarshaler:
return v.UnmarshalBinary(data)
case encoding.TextUnmarshaler:
return v.UnmarshalText(data)
}
return nil
}
// DecodeSlice adapts slice s into an ElementDecoder and decodes it.
func DecodeSlice[T comparable](dec Decoder, s *[]T) error {
return dec.Decode(Slice(s))
}
// DecodeMap adapts a string-keyed map m into a FieldDecoder and decodes it.
func DecodeMap[K ~string, V any](dec Decoder, m *map[K]V) error {
return dec.Decode(Map(m))
}