-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Update ConvertValue() to match the database/sql/driver implementation except for uint64 #760
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
Conversation
This could be simplified, as far as I can see, to the following diff below, at line https://github.com/go-sql-driver/mysql/pull/760/files#diff-9a45f603848db9e865d7a2a24011038cL140
Firstly, |
statement.go
Outdated
// still use nil pointers to those types to mean nil/NULL, just like | ||
// string/*string. | ||
// | ||
// This function is copied from the database/sql package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also describe what changes where made / why it was copied and how it should be synchronized with database/sql
This simply copies recent changes to ConvertValue from database/sql/driver to ensure that our behaviour only differs for uint64. Fixes go-sql-driver#739
3b7ac93
to
b8dcdf7
Compare
@julienschmidt - I've added additional comments - let me know if those work for you. @tdewolff - I prefer to more closely mirror the form of these functions in |
driver_go18_test.go
Outdated
if r := recover(); r != nil { | ||
dbt.Errorf("Failed to check typed nil before calling valuer with value receiver") | ||
} | ||
}() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this recover really necessary?
I don't like it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can leave the test to panic on failure instead - like below. If you prefer that I will alter the PR.
func TestValuerWithValueReceiverGivenNilValue(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {
dbt.mustExec("CREATE TABLE test (value VARCHAR(255))")
dbt.db.Exec("INSERT INTO test VALUES (?)", (*testValuer)(nil))
// the insert will panic if typed nil is not handled before calling a Valuer with a value receiver
})
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please. It is much informative than manual recovery:
$ go test .
--- FAIL: TestPanic (0.00s)
panic: runtime error: index out of range [recovered]
panic: runtime error: index out of range
goroutine 5 [running]:
testing.tRunner.func1(0xc4200900f0)
/usr/local/go/src/testing/testing.go:711 +0x2d2
panic(0x1110920, 0x11d96f0)
/usr/local/go/src/runtime/panic.go:491 +0x283
_/Users/inada-n/tmp/go-panic-test.TestPanic(0xc4200900f0)
/Users/inada-n/tmp/go-panic-test/panic_test.go:9 +0x1b
testing.tRunner(0xc4200900f0, 0x113cb30)
/usr/local/go/src/testing/testing.go:746 +0xd0
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:789 +0x2de
FAIL _/Users/inada-n/tmp/go-panic-test 0.009s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM now.
I've modified the test as requested by @methane and checked it still passes/fails as expected with/without the code changes. |
Description
This addresses #739 by copying across more recent changes to
ConvertValue()
from the reference implementation indatabase/sql/driver
here. The important change is the use ofcallValuerValue
which checks fornil
before calling a Valuer implemented with value receiver. This was added todatabase/sql/driver
for go 1.8 in this commitThis PR leaves the only difference from the reference implementation being the handling of
uint64
with their high bit set.The test added to
driver_go18_test.go
will fail for go 1.9 and 1.10 without this change. The code is not exercised for go 1.8 as it pre-dates #690. The test cannot pass for go 1.7 as that pre-dates the relevant changes indatabase/sql
- which is why the test is added todriver_go18_test.go
.Checklist