Repository navigation
Expand file tree
/
Copy pathset.go
More file actions
196 lines (177 loc) · 4.82 KB
/
Copy pathset.go
File metadata and controls
196 lines (177 loc) · 4.82 KB
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
package structwalker
import (
"encoding"
"errors"
"fmt"
"math"
"reflect"
"strconv"
"time"
)
var (
textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
durationType = reflect.TypeFor[time.Duration]()
)
// SetValue sets the field to v, converting where that can be done without
// losing anything.
//
// A value of the field's own type is stored as is. Numbers convert between
// numeric types as long as they fit. A string is parsed into whatever the
// field is: a number, a bool, a time.Duration, a []byte or any type that
// implements encoding.TextUnmarshaler, which covers time.Time, net/netip.Addr
// and most ID types. A pointer field is allocated and pointed at the result.
// nil resets the field to its zero value.
//
// SetValue returns ErrNotSettable if the field cannot be changed.
func (fc FieldContext) SetValue(v any) error {
if !fc.Value.CanSet() {
return fmt.Errorf("field %s: %w", fc.Path, ErrNotSettable)
}
if err := assign(fc.Value, v); err != nil {
return fmt.Errorf("field %s: %w", fc.Path, err)
}
return nil
}
func assign(dst reflect.Value, v any) error {
if v == nil {
dst.SetZero()
return nil
}
src := reflect.ValueOf(v)
switch {
case src.Type().AssignableTo(dst.Type()):
dst.Set(src)
return nil
case dst.Kind() == reflect.Pointer:
target := reflect.New(dst.Type().Elem())
if err := assign(target.Elem(), v); err != nil {
return err
}
dst.Set(target)
return nil
case src.Kind() == reflect.String:
return assignString(dst, src.String())
case isNumeric(src.Kind()) && isNumeric(dst.Kind()):
return assignNumber(dst, src)
case !isNumeric(src.Kind()) && !isNumeric(dst.Kind()) && src.Type().ConvertibleTo(dst.Type()):
dst.Set(src.Convert(dst.Type()))
return nil
}
return fmt.Errorf("cannot assign %s to %s", src.Type(), dst.Type())
}
func assignString(dst reflect.Value, s string) error {
if dst.CanAddr() && dst.Addr().Type().Implements(textUnmarshalerType) {
if err := dst.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(s)); err != nil {
return parseError(s, dst, err)
}
return nil
}
if dst.Type() == durationType {
d, err := time.ParseDuration(s)
if err != nil {
return parseError(s, dst, err)
}
dst.SetInt(int64(d))
return nil
}
switch {
case dst.Kind() == reflect.String:
dst.SetString(s)
case dst.CanInt():
n, err := strconv.ParseInt(s, 10, dst.Type().Bits())
if err != nil {
return parseError(s, dst, err)
}
dst.SetInt(n)
case dst.CanUint():
n, err := strconv.ParseUint(s, 10, dst.Type().Bits())
if err != nil {
return parseError(s, dst, err)
}
dst.SetUint(n)
case dst.CanFloat():
n, err := strconv.ParseFloat(s, dst.Type().Bits())
if err != nil {
return parseError(s, dst, err)
}
dst.SetFloat(n)
case dst.Kind() == reflect.Bool:
b, err := strconv.ParseBool(s)
if err != nil {
return parseError(s, dst, err)
}
dst.SetBool(b)
case dst.Kind() == reflect.Slice && dst.Type().Elem().Kind() == reflect.Uint8:
dst.SetBytes([]byte(s))
default:
return fmt.Errorf("cannot parse a string into %s", dst.Type())
}
return nil
}
func parseError(s string, dst reflect.Value, err error) error {
if numErr, ok := errors.AsType[*strconv.NumError](err); ok {
err = numErr.Err
}
return fmt.Errorf("cannot parse %q as %s: %w", s, dst.Type(), err)
}
// assignNumber converts between numeric kinds and refuses anything that would
// change the value: overflow, a negative into an unsigned or a dropped fraction.
func assignNumber(dst, src reflect.Value) error {
fits := false
switch {
case dst.CanInt():
var n int64
switch {
case src.CanInt():
n, fits = src.Int(), true
case src.CanUint():
n, fits = int64(src.Uint()), src.Uint() <= math.MaxInt64
default:
f := src.Float()
n, fits = int64(f), f == math.Trunc(f) && f >= math.MinInt64 && f < math.MaxInt64
}
if fits = fits && !dst.OverflowInt(n); fits {
dst.SetInt(n)
}
case dst.CanUint():
var n uint64
switch {
case src.CanInt():
n, fits = uint64(src.Int()), src.Int() >= 0
case src.CanUint():
n, fits = src.Uint(), true
default:
f := src.Float()
n, fits = uint64(f), f == math.Trunc(f) && f >= 0 && f < math.MaxUint64
}
if fits = fits && !dst.OverflowUint(n); fits {
dst.SetUint(n)
}
default:
var f float64
switch {
case src.CanInt():
f = float64(src.Int())
case src.CanUint():
f = float64(src.Uint())
default:
f = src.Float()
}
if fits = !dst.OverflowFloat(f); fits {
dst.SetFloat(f)
}
}
if !fits {
return fmt.Errorf("%v does not fit in %s", src, dst.Type())
}
return nil
}
func isNumeric(k reflect.Kind) bool {
switch k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64:
return true
}
return false
}