Skip to content

ScanStruct support for structs with Interface types#399

Draft
arkamar wants to merge 2 commits into
doug-martin:masterfrom
arkamar:interface-test
Draft

ScanStruct support for structs with Interface types#399
arkamar wants to merge 2 commits into
doug-martin:masterfrom
arkamar:interface-test

Conversation

@arkamar

@arkamar arkamar commented Dec 20, 2023

Copy link
Copy Markdown

This PR serves both as an issue report and a preliminary attempt to fix a problem in the ScanStruct method of goqu. The method encounters errors when dealing with structs that contain interface types, specifically those implementing the sql.Scanner interface.

The ScanStruct method fails with errors like the following when it encounters an interface type within a struct:

sql: Scan error on column index 1, name "value": unsupported Scan, storing driver.Value type string into type *sql.Scanner

This issue arises because the newColumnMap function assigns the interface type to GoType rather than the concrete type of the object in use.
I've made an attempt to resolve this by detecting if a struct field's type is Interface and then replacing f.Type with concreteType. This approach seems to work, but it introduces a potential issue where the scanner might reuse the same type for subsequent scans, when the interface could be implemented by different objects (the second testcase with NullInt32 object).

Well, the question is, if I am not overthinking it :)

So, what do you think?

This commit adds a currently failing test case demonstrating an issue
when a struct containing an item of type sql.Scanner is used. The test
fails with the error:

  sql: Scan error on column index 0, name "value": unsupported Scan, storing driver.Value type string into type *sql.Scanner
@arkamar arkamar marked this pull request as draft December 20, 2023 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant