-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
236 lines (210 loc) · 5.3 KB
/
value.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package dataparse
import (
"fmt"
"net"
"reflect"
"time"
)
// Value is one of the two central types in dataparse.
// It is used to transform data between various representations.
type Value struct {
Data any
cfg *fromConfig
}
// NewValue returns the passed data as a Value.
func NewValue(data any, opts ...FromOption) Value {
return Value{
Data: data,
cfg: newFromConfig(opts...),
}
}
//go:generate go run ./cmd/gen-value-numbers
// IsNil returns true if the data Value stores is nil.
func (v Value) IsNil() bool {
return v.Data == nil
}
type Fromer interface {
From(Value) error
}
type CustomToFunc func(source Value, other any) (any, bool, error)
var customTo = []CustomToFunc{stdlibToer}
func AddCustomToFunc(fn CustomToFunc) {
customTo = append(customTo, fn)
}
// To transforms the stored data into the target type and returns any
// occurring errors.
//
// The passed value must be a pointer.
//
// To utilizes the various transformation methods and returns their
// errors.
//
// If the parameter satisfies the Fromer interface it will be used to
// set the value.
func (v Value) To(other any, opts ...ToOption) error {
if fromer, ok := other.(Fromer); ok {
return fromer.From(v)
}
target := reflect.ValueOf(other)
if target.Kind() != reflect.Pointer {
return ErrValueIsNotPointer
}
// dereference until the target is a pointer but the value pointer
// to is not
// for target.Kind() == reflect.Pointer && target.Elem().Kind() == reflect.Pointer {
for target.Kind() == reflect.Pointer {
if target.IsNil() {
// initialize pointer with a valid value
target.Set(reflect.New(target.Type().Elem()))
}
// handle pointers to constants or structs that satisfy the
// Fromer interface
if fromer, ok := target.Interface().(Fromer); ok {
return fromer.From(v)
}
target = target.Elem()
}
// handle slices but skip named types (like net.IP which is
// a []byte)
if target.Type().Name() == "" && target.Kind() == reflect.Slice || target.Kind() == reflect.Array {
vs, err := v.List()
if err != nil {
return fmt.Errorf("dataparse: target is a slice, error converting %T to slice: %w",
v.Data, err)
}
converts := reflect.MakeSlice(
target.Type(),
len(vs),
len(vs),
)
for i, v := range vs {
if err := v.To(converts.Index(i).Addr().Interface(), opts...); err != nil {
return err
}
}
target.Set(converts)
return nil
}
for _, fn := range customTo {
customVal, ok, err := fn(v, target.Interface())
if err != nil {
return fmt.Errorf("dataparse: error in custom toer: %w", err)
}
if !ok {
continue
}
target.Set(reflect.ValueOf(customVal))
return nil
}
// If the passed value is a pointer to a struct try
// converting Value to map and call .To
if target.Kind() == reflect.Struct {
m, err := v.Map()
if err != nil {
return err
}
return m.To(other, opts...)
}
return fmt.Errorf("dataparse: unhandled type: %T", other)
}
func stdlibToer(v Value, other any) (any, bool, error) {
switch other.(type) {
case string:
newValue, err := v.String()
return newValue, true, err
case int:
newValue, err := v.Int()
return newValue, true, err
case int8:
newValue, err := v.Int8()
return newValue, true, err
case int16:
newValue, err := v.Int16()
return newValue, true, err
case int32:
newValue, err := v.Int32()
return newValue, true, err
case int64:
newValue, err := v.Int64()
return newValue, true, err
case uint:
newValue, err := v.Uint()
return newValue, true, err
case uint8:
newValue, err := v.Uint8()
return newValue, true, err
case uint16:
newValue, err := v.Uint16()
return newValue, true, err
case uint32:
newValue, err := v.Uint32()
return newValue, true, err
case uint64:
newValue, err := v.Uint64()
return newValue, true, err
case float32:
newValue, err := v.Float32()
return newValue, true, err
case float64:
newValue, err := v.Float64()
return newValue, true, err
case bool:
newValue, err := v.Bool()
return newValue, true, err
case net.IP:
newValue, err := v.IP()
return newValue, true, err
case time.Time:
newValue, err := v.Time()
return newValue, true, err
default:
return nil, false, nil
}
}
// List returns the underlying data as a slice of Values.
//
// The passed separators are passed to .ListString if the underlying
// value is a string.
//
// Warning: This method is very simplistic and at the moment only
// returns a proper slice of values if the underlying data is a slice.
func (v Value) List(seps ...string) ([]Value, error) {
if v.Data == nil {
return []Value{}, ErrValueIsNil
}
switch reflect.TypeOf(v.Data).Kind() {
case reflect.String:
s, err := v.ListString(seps...)
if err != nil {
return nil, err
}
vs := make([]Value, len(s))
for i := range s {
vs[i] = NewValue(s[i])
}
return vs, nil
case reflect.Slice:
l := reflect.ValueOf(v.Data)
ret := make([]Value, l.Len())
for i := 0; i < l.Len(); i++ {
ret[i] = NewValue(l.Index(i).Interface())
}
return ret, nil
default:
return []Value{v}, nil
}
}
// MustList is the error-ignoring version of List.
func (v Value) MustList() []Value {
l, _ := v.List()
return l
}
// Map returns the underlying data as a Map.
func (v Value) Map() (*Map, error) {
return NewMap(v.Data)
}
// MustMap is the error-ignoring version of Map.
func (v Value) MustMap() *Map {
m, _ := v.Map()
return m
}