-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert.go
53 lines (44 loc) · 1.68 KB
/
convert.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package argmapper
import "reflect"
// Convert converts the input arguments to the given target type. Convert will
// use any of the available arguments and converters to reach the given target
// type.
func Convert(target reflect.Type, opts ...Arg) (interface{}, error) {
out, err := convertMulti([]reflect.Type{target}, opts...)
if err != nil {
return nil, err
}
return out[0].Interface(), nil
}
// convertMulti is the same as Convert but converts multiple types at one
// time. This is not currently exposed publicly since it is a slightly more
// complicated interfaces and callers can call Convert multiple times. But
// we use this internally for Redefine and FilterOutput.
func convertMulti(target []reflect.Type, opts ...Arg) ([]reflect.Value, error) {
// The way we get convert to work is that we make a dynamic function
// that takes the target type as input, and then call Call on it. This
// lets our DI system automatically determine our conversion.
f, err := convertFunc(target)
if err != nil {
return nil, err
}
// Call it
result := f.Call(opts...)
if err := result.Err(); err != nil {
return nil, err
}
// Our result is the first result
return result.out, nil
}
// convertFunc constructs a function that takes values of the given target
// and returns them directly. We use this for conversion.
func convertFunc(target []reflect.Type) (*Func, error) {
funcType := reflect.FuncOf(target, target, false)
funcVal := reflect.MakeFunc(funcType, func(args []reflect.Value) []reflect.Value {
return args
})
// Create our "Func" type from our newly created function.
return NewFunc(funcVal.Interface())
}