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

database/sql: (n *NullInt64) Scan identity #35228

Closed
gerardolima opened this issue Oct 29, 2019 · 3 comments
Closed

database/sql: (n *NullInt64) Scan identity #35228

gerardolima opened this issue Oct 29, 2019 · 3 comments

Comments

@gerardolima
Copy link

@gerardolima gerardolima commented Oct 29, 2019

I expected a (n *NullInt64) Scan(value interface{}) applied to an instance of NullInt64 would yield a copy of the argument. As this func is able to scan from many types, I expected it would be able to scan from another instance of NullInt64.

What version of Go are you using (go version)?

go version go1.13.1 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOHOSTARCH="amd64"
GOHOSTOS="darwin"

What did you do?

package main

import (
	"database/sql"
	"fmt"
)

func main() {
	var n int64 = 3

	n1 := sql.NullInt64{Int64:-1, Valid:false}
	err := n1.Scan(n)
	fmt.Printf("NullInt64 scanned from int64: '%v' (err: '%v')\n", n1, err)

	n2 := sql.NullInt64{Int64:-1, Valid:false}
	err = n2.Scan(n1)
	fmt.Printf("NullInt64 scanned from NullInt64: '%v' (err: '%v')\n", n2, err)
}

https://play.golang.org/p/0lHk5pSWLrJ

What did you expect to see?

  • n2 == n1
  • err == nil

What did you see instead?

  • n1 == sql.NullInt64{Int64:3, Valid:false}
  • n2 == sql.NullInt64{Int64:-1, Valid:true}
  • err: 'converting driver.Value type sql.NullInt64 ("{3 true}") to a int64: invalid syntax'
@agnivade agnivade changed the title (n *NullInt64) Scan identity database/sql: (n *NullInt64) Scan identity Oct 29, 2019
@agnivade
Copy link
Contributor

@agnivade agnivade commented Oct 29, 2019

@odeke-em
Copy link
Member

@odeke-em odeke-em commented Nov 3, 2019

Thank you for this report @gerardolima and welcome to the Go project!
Thank you @agnivade for the triage!

@gerardolima, the Scanner interface https://golang.org/pkg/database/sql/#Scanner scans and assigns values from a database driver. src is documented as the specific types that'll be most likely the values from the database which is why we only accept a few
Screen Shot 2019-11-02 at 9 53 22 PM

and not every data type. Adding Scanning from NullInt64 could be nice but that's now over-serving the purpose which was to convert data from the wire from the database and into Go types; one can then argue we should also try reading from maps which then heavily loads the contract that the Scanner interface promised. With those reasons, I'd say let's not change what Scan promises, please follow its simple promise :)

@odeke-em odeke-em closed this Nov 3, 2019
@gerardolima
Copy link
Author

@gerardolima gerardolima commented Nov 4, 2019

hey, @odeke-em, thank you for the warm welcome!

I understand your reasoning and, me too, I do believe simple contracts is the way to go. Nevertheless, the implementation of such identity property would be really trivial and its absence, together with the lack of a way to get the value of NullX in a generic way, makes it very difficult to write A-B-A tests.

Maybe it would worth considering a (n *NullInt64) Compare, as an alternative? or should I consider this as too specific to my problem?

@golang golang locked and limited conversation to collaborators Nov 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.