-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
130 lines (116 loc) · 3.13 KB
/
utils.go
File metadata and controls
130 lines (116 loc) · 3.13 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
package flow
import "reflect"
var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
)
type FlowError struct {
Message string
}
func (e *FlowError) Error() string {
return e.Message
}
func canConvert(from, to reflect.Type) bool {
if from == to {
return true
}
if from.AssignableTo(to) {
return true
}
if from.ConvertibleTo(to) {
return true
}
return false
}
func addArg(args *[]reflect.Value, val reflect.Value, argType reflect.Type) error {
if !val.IsValid() {
*args = append(*args, reflect.Zero(argType))
return nil
}
valType := val.Type()
if !valType.AssignableTo(argType) {
if !canConvert(valType, argType) {
return &FlowError{Message: ErrArgTypeMismatch}
}
*args = append(*args, val.Convert(argType))
} else {
*args = append(*args, val)
}
return nil
}
func prepareArgsWithType(values []reflect.Value, argTypes []reflect.Type) ([]reflect.Value, error) {
argCount := len(argTypes)
if argCount == 0 {
if len(values) > 0 {
return nil, &FlowError{Message: ErrArgCountMismatch}
}
return nil, nil
}
args := reflectValueSlicePool.Get(argCount)
if len(values) > 0 {
if argCount > 0 && len(values) == argCount {
for i := range len(values) {
if err := addArg(&args, values[i], argTypes[i]); err != nil {
reflectValueSlicePool.Put(args)
return nil, err
}
}
} else {
if argCount == 1 && argTypes[0].Kind() == reflect.Slice {
sliceType := argTypes[0]
sliceValue := reflect.MakeSlice(sliceType, len(values), len(values))
for i := range values {
elemType := sliceType.Elem()
val := values[i].Interface()
valValue := reflect.ValueOf(val)
if !valValue.IsValid() {
sliceValue.Index(i).Set(reflect.Zero(elemType))
continue
}
if !valValue.Type().AssignableTo(elemType) {
if valValue.CanConvert(elemType) {
valValue = valValue.Convert(elemType)
} else {
reflectValueSlicePool.Put(args)
return nil, &FlowError{Message: ErrArgTypeMismatch}
}
}
sliceValue.Index(i).Set(valValue)
}
args = append(args, sliceValue)
} else {
currentValue := values[0].Interface()
currentValueType := reflect.TypeOf(currentValue)
currentValueValue := reflect.ValueOf(currentValue)
switch {
case currentValueType == nil:
if argCount > 0 {
args = append(args, reflect.Zero(argTypes[0]))
}
case currentValueType.Kind() == reflect.Slice || currentValueType.Kind() == reflect.Array:
elemCount := currentValueValue.Len()
if argCount > 0 && elemCount != argCount {
reflectValueSlicePool.Put(args)
return nil, &FlowError{Message: ErrArgCountMismatch}
}
for i := range elemCount {
elem := currentValueValue.Index(i)
if elem.Kind() == reflect.Interface {
elem = elem.Elem()
}
args = append(args, elem)
}
case argCount > 0:
if err := addArg(&args, reflect.ValueOf(currentValue), argTypes[0]); err != nil {
reflectValueSlicePool.Put(args)
return nil, err
}
}
}
}
}
if len(args) != argCount {
reflectValueSlicePool.Put(args)
return nil, &FlowError{Message: ErrArgCountMismatch}
}
return args, nil
}