-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconvert_func.go
More file actions
276 lines (232 loc) · 7.23 KB
/
Copy pathconvert_func.go
File metadata and controls
276 lines (232 loc) · 7.23 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package converter
import (
"errors"
"fmt"
"reflect"
"slices"
)
type FuncChain interface {
AddConverter(converter ...any) FuncChain
AutoPackageConverter(fromPkg, toPkg any) FuncChain
AllowImplicit() FuncChain
Convert(from any, to any) error
}
type funcChain struct {
allowImplicitConversion bool
funcs map[reflect.Type]map[reflect.Type]func(from reflect.Value, to reflect.Value) error
}
func NewFuncChain(converters ...any) FuncChain {
out := funcChain{
funcs: map[reflect.Type]map[reflect.Type]func(from reflect.Value, to reflect.Value) error{},
}
return out.AddConverter(converters...)
}
func (c *funcChain) AllowImplicit() FuncChain {
c.allowImplicitConversion = true
return c
}
func (c *funcChain) AutoPackageConverter(fromPkg, toPkg any) FuncChain {
fromTypes := map[string]reflect.Type{}
toTypes := map[string]reflect.Type{}
fromName := pkgName(fromPkg)
toName := pkgName(toPkg)
if fromName == "" || toName == "" {
panic("invalid auto package type; should be struct")
}
for t := range listAllBaseTypes() {
if t.PkgPath() == fromPkg {
fromTypes[t.Name()] = t
}
if t.PkgPath() == toPkg {
toTypes[t.Name()] = t
}
}
for name, fromT := range fromTypes {
toT, ok := toTypes[name]
if !ok {
continue
}
// this does nothing other than inform the types
c.AddConvertFunc(fromT, toT, func(_ reflect.Value, _ reflect.Value) error {
return nil
})
}
return c
}
func (c *funcChain) Convert(from any, to any) error {
fromValue := reflect.ValueOf(from)
fromType := fromValue.Type()
baseFromType := baseType(fromType)
toValue := reflect.ValueOf(to)
toType := toValue.Type()
baseToType := baseType(toType)
// build the shortest path between types
chain := c.shortestChain(baseFromType, baseToType)
// no explicit conversions
if len(chain) == 0 {
return fmt.Errorf("no conversion path found from %s to %s", typeName(baseFromType), typeName(baseToType))
}
cnv := conversion{
chain: c,
}
// iterate, creating any intermediary structs for the migration
last := fromValue
for i, step := range chain {
var next reflect.Value
if i == len(chain)-1 {
next = toValue
} else {
next = reflect.New(step.targetType)
}
cnv.convert(last, next)
last = next
}
return errors.Join(cnv.errors...)
}
func (c *funcChain) AddConverter(converters ...any) FuncChain {
for _, converter := range converters {
c.addConverter(converter)
}
return c
}
func (c *funcChain) addConverter(converter any) {
convertFunc := reflect.ValueOf(converter)
convertFuncType := convertFunc.Type()
if validationError := validateConvertFunc(convertFuncType); validationError != nil {
panic(fmt.Errorf(`converter must be a function of one of the following forms:
func(from *Type1, to *Type2)
func(from *Type1, to *Type2) error
func(chain %v, from *Type1, to *Type2)
func(chain %v, from *Type1, to *Type2) error
got: %+v
err: %v
`, chainType, chainType, convertFuncType, validationError))
}
// seems to be a valid function, create a handler function for it
returnsError := convertFuncType.NumOut() > 0
hasChainParam := false
fromType := convertFuncType.In(0)
toType := convertFuncType.In(1)
if convertFuncType.NumIn() > 2 {
hasChainParam = true
fromType = convertFuncType.In(1)
toType = convertFuncType.In(2)
}
c.AddConvertFunc(fromType, toType, func(from reflect.Value, to reflect.Value) error {
// setup matching args, from and to should already be set up properly
var args []reflect.Value
if hasChainParam {
args = []reflect.Value{reflect.ValueOf(c), from, to}
} else {
args = []reflect.Value{from, to}
}
// invoke the function
out := convertFunc.Call(args)
// return errors if the function does
if returnsError && !out[0].IsNil() {
return out[0].Interface().(error)
}
return nil
})
}
func (c *funcChain) AddConvertFunc(fromType, toType reflect.Type, fn func(from reflect.Value, to reflect.Value) error) {
baseFromType := baseType(fromType)
baseToType := baseType(toType)
convertFuncs := c.funcs[baseFromType]
if convertFuncs == nil {
convertFuncs = map[reflect.Type]func(from reflect.Value, to reflect.Value) error{}
c.funcs[baseFromType] = convertFuncs
}
if convertFuncs[baseToType] != nil {
panic(fmt.Errorf("convert from: %s -> %s defined multiple times; %+v", typeName(baseFromType), typeName(baseToType), reflect.TypeFor[func(from reflect.Value, to reflect.Value) error]()))
}
convertFuncs[baseToType] = fn
}
func (c *funcChain) shortestChain(fromType reflect.Type, targetType reflect.Type, visited ...reflect.Type) []reflectConvertStep {
var shortest []reflectConvertStep
for toType, convertFunc := range c.funcs[fromType] {
if slices.Contains(visited, toType) {
continue
}
if toType == targetType {
return []reflectConvertStep{{toType, convertFunc}}
}
chain := c.shortestChain(toType, targetType, append(visited, fromType)...)
if chain == nil {
continue
}
// this is a viable conversion chain, use it if it's shorter or we haven't found any yet
chain = append([]reflectConvertStep{{toType, convertFunc}}, chain...)
if shortest == nil || len(chain) < len(shortest) {
shortest = chain
}
}
// no explicit conversions, try a direct conversion
if len(shortest) == 0 && c.allowImplicitConversion {
return []reflectConvertStep{{fromType, func(_ reflect.Value, _ reflect.Value) error {
return nil
}}}
}
return shortest
}
var chainType = reflect.TypeFor[FuncChain]()
var errorInterface = reflect.TypeFor[error]()
func typeName(t reflect.Type) string {
return fmt.Sprintf("<%s>.%s", t.PkgPath(), t.Name())
}
func validateConvertFunc(t reflect.Type) error {
if t.Kind() != reflect.Func {
return fmt.Errorf("not a function")
}
// need to have 2 or 3 args, optionally with funcChain as the first one
if t.NumIn() < 2 || t.NumIn() > 3 {
return fmt.Errorf("must have 2 or 3 arguments")
}
fromType := t.In(0)
toType := t.In(1)
if t.NumIn() > 2 {
if t.In(0) != chainType {
return fmt.Errorf("when using 3 arguments, %+v must the first", chainType)
}
fromType = t.In(1)
toType = t.In(2)
} else if t.In(0) == chainType {
return fmt.Errorf("if %+v is the first argument, there must be 2 more arguments to convert", chainType)
}
// it doesn't make sense to convert from a type to the same type
if baseType(fromType) == baseType(toType) {
return fmt.Errorf("convert should be between different types")
}
// toType must be a pointer, which will be provided by the convert function
if !isPtr(toType) {
return fmt.Errorf("second convert argument, the target/destination must be a pointer; got: %+v", toType)
}
// return type is either error or nothing
if t.NumOut() > 1 {
return fmt.Errorf("too many return values, must return error or have no return value")
}
if t.NumOut() > 0 && !t.Out(0).Implements(errorInterface) {
return fmt.Errorf("must return error or have no return value")
}
return nil
}
func pkgName(pkg any) string {
switch p := pkg.(type) {
case string:
return p
case reflect.Type:
return p.PkgPath()
}
return baseType(reflect.TypeOf(pkg)).PkgPath()
}
func baseType(t reflect.Type) reflect.Type {
for isPtr(t) {
t = t.Elem()
}
return t
}
type reflectConvertFunc func(from reflect.Value, to reflect.Value) error
type reflectConvertStep struct {
targetType reflect.Type
convertFunc reflectConvertFunc
}