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 nil pointers to value implementing driver.Valuer #339

Closed
kdar opened this issue Oct 9, 2017 · 4 comments
Closed

Support nil pointers to value implementing driver.Valuer #339

kdar opened this issue Oct 9, 2017 · 4 comments

Comments

@kdar
Copy link

kdar commented Oct 9, 2017

For example, you can't do something like this:

var d *apd.Decimal
fmt.Println(pool.Exec(`INSERT INTO test (value) VALUES ($1)`, d))

Relevant discussion with the Go stdlib is here: golang/go#8415
Commit fixing this in Go stdlib is here: golang/go@0ce1d79

@kdar
Copy link
Author

kdar commented Oct 9, 2017

I'm also not sure if pgx checks properly if a type implements driver.Valuer with a pointer receiver. When I try, I get an error like this:

cannot convert {0xc4201e2120} to Numeric

Instead of calling Value() on the pointer receiver, it seems to try to coerce the pointer into Numeric.

@jackc jackc closed this as completed in 82cd87d Oct 27, 2017
@jackc
Copy link
Owner

jackc commented Oct 27, 2017

I used the same fix as was done for the standard lib. It now incurs a reflect operation for each driver.Valuer argument, but there doesn't seem to be any way around it.

@kdar
Copy link
Author

kdar commented Nov 9, 2017

Do you have any insight into the second comment I made on this issue?

Just to clarify what I'm doing, is I'm trying to avoid having to create a wrapper type to check if it's null.

type Decimal struct {...}

func (z *Decimal) Value() (driver.Value, error) {
  if z == nil {
    return nil, nil
  }
  return z.String(), nil
}

Let me know if this endeavor is inane and I should just use a NullDecimal wrapper type.

@jackc
Copy link
Owner

jackc commented Nov 11, 2017

I don't think you should need a wrapper. I have a working test in the test suite with a nil *apd.Decimal so you should be able to do the same that it does.

redbaron pushed a commit to redbaron/pgx that referenced this issue Apr 11, 2020
The test for issue jackc#339 actually can work for any custom type that
implement driver.Valuer, we did not need to import another package just
for testing it, which minimize the package dependencies.

While at it, remove invalid instruction to "go get" the package
go-version in README.
This issue was closed.
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

No branches or pull requests

2 participants