Allow more flexible callback return error type.

This commit is contained in:
Mitar
2021-12-13 00:02:56 +01:00
committed by Alec Thomas
parent deebf0b09b
commit ece1f6d8cb
2 changed files with 47 additions and 2 deletions
+2 -2
View File
@@ -79,8 +79,8 @@ func getMethod(value reflect.Value, name string) reflect.Value {
func callMethod(name string, v, f reflect.Value, bindings bindings) error {
in := []reflect.Value{}
t := f.Type()
if t.NumOut() != 1 || t.Out(0) != callbackReturnSignature {
return fmt.Errorf("return value of %T.%s() must be exactly \"error\"", v.Type(), name)
if t.NumOut() != 1 || !t.Out(0).Implements(callbackReturnSignature) {
return fmt.Errorf("return value of %T.%s() must implement \"error\"", v.Type(), name)
}
for i := 0; i < t.NumIn(); i++ {
pt := t.In(i)
+45
View File
@@ -42,6 +42,51 @@ func TestBindTo(t *testing.T) {
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 {
Called bool
Cmd bindToProviderCmd `cmd:""`