Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning any from callbacks #1046

Merged
merged 4 commits into from May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions callback.go
Expand Up @@ -353,13 +353,27 @@ func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error {
return nil
}

func callbackRetAny(ctx *C.sqlite3_context, v reflect.Value) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should be callbackRetGeneric for consistency with callbackArgGeneric. Or rename the other one to callbackArgAny.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted, thanks!

cb, err := callbackRet(v.Elem().Type())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that the callback returned nil, in which case v.Elem() will return the invalid value. Presumably that needs to translate to SQLite NULL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean I should have a check after the err check like

if cb == nil {
  return nil
}

?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be this:

if v.IsNil() {
    C.sqlite3_result_null(ctx)
    return nil
}

ve := v.Elem()
cb, err := callbackRet(ve.Type())
if err != nil {
    return err
}
return cb(ctx, ve)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied, thank you!

if err != nil {
return err
}

return cb(ctx, v.Elem())
}

func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
switch typ.Kind() {
case reflect.Interface:
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
if typ.Implements(errorInterface) {
return callbackRetNil, nil
}

if typ.NumMethod() == 0 {
return callbackRetAny, nil
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for this part. But I'm not sure how to add a unit test for the callbackRetAny (I tested it manually end-to-end).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write a test that registers and uses such an aggregator, similar to this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a test.


fallthrough
case reflect.Slice:
if typ.Elem().Kind() != reflect.Uint8 {
Expand Down
12 changes: 12 additions & 0 deletions callback_test.go
Expand Up @@ -102,3 +102,15 @@ func TestCallbackConverters(t *testing.T) {
}
}
}

func TestCallbackReturnAny(t *testing.T) {
udf := func() interface{} {
return 1
}

typ := reflect.TypeOf(udf)
_, err := callbackRet(typ.Out(0))
if err != nil {
t.Errorf("Expected valid callback for any return type, got: %s", err)
}
}