-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.go
205 lines (171 loc) · 5.15 KB
/
call.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
package gosh
import (
"fmt"
"reflect"
"strconv"
"testing"
"github.com/mumoshu/gosh/context"
)
func CallFunc(ctx context.Context, name string, fun interface{}, funArgs ...interface{}) ([]reflect.Value, error) {
fv := reflect.ValueOf(fun)
x := reflect.TypeOf(fun)
args, err := getArgs(ctx, name, x, funArgs)
if err != nil {
return nil, err
}
// fmt.Fprintf(os.Stderr, "%v\n", args)
// for o := 0; o < numOut; o++ {
// returnV := x.Out(0)
// return_Kind := returnV.Kind()
// fmt.Printf("\nParameter OUT: "+strconv.Itoa(o)+"\nKind: %v\nName: %v\n", return_Kind, returnV.Name())
// }
panicked := true
defer func() {
if panicked {
fmt.Fprintf(context.Stderr(ctx), "Panicked while running %q with %v\n", name, args)
}
}()
values := fv.Call(args)
panicked = false
if len(values) > 0 {
last := values[len(values)-1]
err, ok := last.Interface().(error)
if ok {
return values, err
}
}
return values, nil
}
func CallMethod(ctx context.Context, name string, m reflect.Value, funArgs ...interface{}) ([]reflect.Value, error) {
args, err := getArgs(ctx, name, m.Type(), funArgs)
if err != nil {
return nil, err
}
values := m.Call(args)
if len(values) > 0 {
last := values[len(values)-1]
err, ok := last.Interface().(error)
if ok {
return values, err
}
}
return values, nil
}
type testingTKey struct{}
func getArgs(ctx context.Context, cmdName string, x reflect.Type, funArgs []interface{}) ([]reflect.Value, error) {
numIn := x.NumIn()
// numOut := x.NumOut()
// funcName := x.String()
isVariadic := x.IsVariadic()
// pkgPath := x.PkgPath()
// fmt.Fprintf(os.Stderr, "gosh.Call: funcName=%s, numIn=%d, isVariadic=%v, pkgPath=%s, funArgs=%v\n", funcName, numIn, isVariadic, pkgPath, funArgs)
args := make([]reflect.Value, numIn)
// https://coderwall.com/p/b5dpwq/fun-with-the-reflection-package-to-analyse-any-function
FOR:
for i, j := 0, 0; i < numIn; i++ {
inV := x.In(i)
in_Kind := inV.Kind() //func
in_typeName := inV.String()
reflectTypeContext := reflect.TypeOf(ctx)
reflectTypeTestingT := reflect.TypeOf(&testing.T{})
// fmt.Fprintf(os.Stderr, "i=%d, type=%v, kind=%v\n", i, inV, in_Kind)
switch in_Kind {
case reflect.Ptr:
if reflectTypeTestingT.AssignableTo(inV) {
v := ctx.Value(testingTKey{})
if v == nil {
panic("Missing *testing.T in context. Probably you tried to export a function that takes *testing.T outside of a go test?")
}
args[i] = reflect.ValueOf(v)
} else {
return nil, fmt.Errorf("parameter %v at %d is not supported", in_typeName, i)
}
case reflect.Interface:
// if inV != reflectTypeContext {
// panic(fmt.Errorf("param %d is interface but not %v", i, reflectTypeContext))
// }
if !reflectTypeContext.AssignableTo(inV) {
return nil, fmt.Errorf("param %d is interface %v but not assignable from %v", i, in_Kind, reflectTypeContext)
}
args[i] = reflect.ValueOf(ctx)
case reflect.String:
if len(funArgs)-1 < j {
panic(fmt.Errorf("missing argument for required parameter %v at %d", in_Kind, j))
}
a := funArgs[j]
j++
args[i] = reflect.ValueOf(a)
case reflect.Bool:
var v interface{}
var err error
switch a := funArgs[j].(type) {
case string:
v, err = strconv.ParseBool(a)
if err != nil {
panic(err)
}
default:
v = a
}
j++
args[i] = reflect.ValueOf(v)
case reflect.Int:
if len(funArgs)-1 < j {
panic(fmt.Errorf("missing argument for required parameter %v at %d", in_Kind, j))
}
var v interface{}
switch a := funArgs[j].(type) {
case string:
intv, err := strconv.ParseInt(a, 10, 0)
if err != nil {
panic(err)
}
v = int(intv)
default:
v = a
}
j++
args[i] = reflect.ValueOf(v)
case reflect.Slice:
if i == numIn-1 && isVariadic {
args = args[:i]
for _, v := range funArgs[j:] {
args = append(args, reflect.ValueOf(v))
}
break FOR
}
switch inV.Elem().Kind() {
case reflect.String:
var strArgs []string
for _, v := range funArgs[j:] {
strArgs = append(strArgs, v.(string))
}
args[i] = reflect.ValueOf(strArgs)
default:
panic(fmt.Errorf("slice of %v is not yet supported", inV.Elem().Kind()))
}
break FOR
case reflect.Map:
args[i] = reflect.ValueOf(funArgs[j])
case reflect.Struct:
f := &structFieldsReflector{
TagToEnvName: defaultFilter,
TagToUsage: defaultFilter,
FieldToFlagName: defaultFilter,
}
// This returns a pointer to the value of the type i.e. new(foo), &foo{}, instead of foo{}.
v := reflect.New(inV)
flagArgs := funArgs[j:]
if err := f.SetStruct(cmdName, v, funArgs[j:]); err != nil {
return nil, fmt.Errorf("failed to map args to %v, for args starting at %d, %v: %v", inV.Name(), j, flagArgs, err)
}
// And that's why you need to take the Elem, which is the underlying value the pointer points.
// Otherwise you get errors like `Call using *gosh_test.Opts as type gosh_test.Opts`
args[i] = v.Elem()
break FOR
default:
panic(fmt.Sprintf("call: unsupported func parameter name=%v type=%v kind=%v while trying to match argument: %v", inV.Name(), in_typeName, in_Kind, funArgs[j]))
}
}
return args, nil
}