Allow more flexible callback return error type.
This commit is contained in:
+2
-2
@@ -79,8 +79,8 @@ func getMethod(value reflect.Value, name string) reflect.Value {
|
|||||||
func callMethod(name string, v, f reflect.Value, bindings bindings) error {
|
func callMethod(name string, v, f reflect.Value, bindings bindings) error {
|
||||||
in := []reflect.Value{}
|
in := []reflect.Value{}
|
||||||
t := f.Type()
|
t := f.Type()
|
||||||
if t.NumOut() != 1 || t.Out(0) != callbackReturnSignature {
|
if t.NumOut() != 1 || !t.Out(0).Implements(callbackReturnSignature) {
|
||||||
return fmt.Errorf("return value of %T.%s() must be exactly \"error\"", v.Type(), name)
|
return fmt.Errorf("return value of %T.%s() must implement \"error\"", v.Type(), name)
|
||||||
}
|
}
|
||||||
for i := 0; i < t.NumIn(); i++ {
|
for i := 0; i < t.NumIn(); i++ {
|
||||||
pt := t.In(i)
|
pt := t.In(i)
|
||||||
|
|||||||
@@ -42,6 +42,51 @@ func TestBindTo(t *testing.T) {
|
|||||||
require.Equal(t, "foo", saw)
|
require.Equal(t, "foo", saw)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInvalidCallback(t *testing.T) {
|
||||||
|
type iface interface {
|
||||||
|
Method()
|
||||||
|
}
|
||||||
|
|
||||||
|
saw := ""
|
||||||
|
method := func(i iface) string {
|
||||||
|
saw = string(i.(impl))
|
||||||
|
return saw
|
||||||
|
}
|
||||||
|
|
||||||
|
var cli struct{}
|
||||||
|
|
||||||
|
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
|
||||||
|
require.EqualError(t, err, `return value of *reflect.rtype.method() must implement "error"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
type zrror struct{}
|
||||||
|
|
||||||
|
func (*zrror) Error() string {
|
||||||
|
return "error"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCallbackCustomError(t *testing.T) {
|
||||||
|
type iface interface {
|
||||||
|
Method()
|
||||||
|
}
|
||||||
|
|
||||||
|
saw := ""
|
||||||
|
method := func(i iface) *zrror {
|
||||||
|
saw = string(i.(impl))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var cli struct{}
|
||||||
|
|
||||||
|
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
|
||||||
|
require.NoError(t, err)
|
||||||
|
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "foo", saw)
|
||||||
|
}
|
||||||
|
|
||||||
type bindToProviderCLI struct {
|
type bindToProviderCLI struct {
|
||||||
Called bool
|
Called bool
|
||||||
Cmd bindToProviderCmd `cmd:""`
|
Cmd bindToProviderCmd `cmd:""`
|
||||||
|
|||||||
Reference in New Issue
Block a user