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

custom int type conversion: unsupported type #10

Closed
0xh0b0 opened this issue Dec 27, 2023 · 3 comments · Fixed by #11
Closed

custom int type conversion: unsupported type #10

0xh0b0 opened this issue Dec 27, 2023 · 3 comments · Fixed by #11
Labels
bug Something isn't working

Comments

@0xh0b0
Copy link

0xh0b0 commented Dec 27, 2023

type TestEnum int

const (
    TestEnumA TestEnum = iota
    TestEnumB
)
type TestModel struct {
    ID    int
    Field gonull.Nullable[TestEnum]
}

Scan fails with the following error

sql: Scan error on column index 1, name "field": unsupported type conversion
@LukaGiorgadze
Copy link
Owner

LukaGiorgadze commented Dec 27, 2023

Hey @0xh0b0 Thanks for your feedback!

I just updated/added a test covering your case: 882c001

Screenshot 2023-12-27 at 21 25 31

Are you sure that db returns the number/int?
Can you provide more details?

@0xh0b0
Copy link
Author

0xh0b0 commented Dec 28, 2023

@LukaGiorgadze
I think you have incorrect assertions in your unit test.
the unit test should be

	type TestEnum float32

	const (
		TestEnumA TestEnum = iota
		TestEnumB
	)

	type TestModel struct {
		ID    int
		Field gonull.Nullable[TestEnum]
	}

	// Simulate the scenario where the SQL driver returns an int64
	// This is common as database integer types are usually scanned as int64 in Go
	//
	// sqlReturnedValue (int64(0)) is convertible to float32.
	// The converted value 0 (as float32) matches TestEnumA, which is also 0 when converted to float32.
	sqlReturnedValue := int64(0)

	model := TestModel{ID: 1, Field: gonull.NewNullable(TestEnumA)}

	err := model.Field.Scan(sqlReturnedValue)
	assert.NoError(t, err, "Scan failed with unsupported type conversion")
	assert.Equal(t, TestEnumA, model.Field.Val, "Scanned value does not match expected enum value")

@LukaGiorgadze
Copy link
Owner

@0xh0b0 Oh yeh, my bad.
Here's a fix #11 which covers your case.
Thank you!

@LukaGiorgadze LukaGiorgadze added the bug Something isn't working label Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants