Skip to content

Commit

Permalink
refactor: reuse callAnyFunction for callFunction
Browse files Browse the repository at this point in the history
Rather than duplicating basically identical calling logic
  • Loading branch information
alecthomas committed Nov 1, 2024
1 parent 07c8821 commit 373692a
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,19 @@ func callFunction(f reflect.Value, bindings bindings) error {
if f.Kind() != reflect.Func {
return fmt.Errorf("expected function, got %s", f.Type())
}
in := []reflect.Value{}
t := f.Type()
if t.NumOut() != 1 || !t.Out(0).Implements(callbackReturnSignature) {
return fmt.Errorf("return value of %s must implement \"error\"", t)
}
for i := 0; i < t.NumIn(); i++ {
pt := t.In(i)
if argf, ok := bindings[pt]; ok {
argv, err := argf()
if err != nil {
return err
}
in = append(in, argv)
} else {
return fmt.Errorf("couldn't find binding of type %s for parameter %d of %s(), use kong.Bind(%s)", pt, i, t, pt)
}
out, err := callAnyFunction(f, bindings)
if err != nil {
return err
}
out := f.Call(in)
if out[0].IsNil() {
ferr := out[0]
if ferrv := reflect.ValueOf(ferr); !ferrv.IsValid() || ferrv.IsNil() {
return nil
}
return out[0].Interface().(error) //nolint
return ferr.(error) //nolint:forcetypeassert
}

func callAnyFunction(f reflect.Value, bindings bindings) (out []any, err error) {
Expand Down

0 comments on commit 373692a

Please sign in to comment.