-
Notifications
You must be signed in to change notification settings - Fork 838
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
pgtype: driver.Valuer support for Array or Map #1662
Comments
This comment was marked as outdated.
This comment was marked as outdated.
The following generic version seems to meet most of my demands. Perhaps something similar can be done to the pgtype package? I'm willing to send a PR.
|
I don't think this is feasible. The generic approach for
I haven't investigated to see if this would be practical, but at first glance I think it is a good idea. And I like the symmetry with I think it would also be possible to use |
Yes, the race detector already slapped me on the wrist for that.
I read in some issue that v4 used a generator. What tool did you use for generation? It might be worth to port it for v5 array types, building on |
I'm a long time Ruby developer so I was using A few thoughts on this though. 1. If we add concrete types I think it would only make sense in the |
This is a great demo code, thanks for the tip.
Please note that the pgtype.Map instance is not safe for concurrent use and it will almost immediately fire a data race violation if used concurrently since the Map uses memoizedScanPlans plans like one here: Line 223 in d626dfe
To make the map concurrency safe, one may need to either add RWLock around these plans or wrap the whole Map instance with a plain Lock for all access which would make things slow. |
Yes, I noticed also but I didn't update above snippet as I want to implement a better more permanent solution that can be merged with upstream. |
@jackc I wouldn't mind sending PRs against the stlib package. I would probably first commit to a |
👍 It be interesting to experiment with a few different approaches before committing to any one. I still don't see any alternative to an array type for each underlying type, but it bothers me that we can't find a generic approach. ... actually, I wonder if this would be feasible? type Element interface {
sql.Scanner
driver.Valuer
}
type Array[T Element] []T I think that should be implementable for any |
Guys, can you please stop from hijacking this thread? The topic is to discuss a new feature request and any code I posted is just for understanding the concept and for the sake of discussion. It is not provided so that people can use it and then start asking questions why it doesn't work as they thought it would. Thanks. |
Hello! I was wondering if there's been any more progress on this? I think that even an |
Candidate solution: #2020 |
Is your feature request related to a problem? Please describe.
We use
stdlib
pgx in our project. For unit tests we use sqlmock for unit tests. We want to migrate tov5
and the relatedv5/pgtype
sub-package from the stand-alonepgtype
package.sqlmock doesn't like slice types such as
[]int
,[]string
etc. As a solution we have some wrapper types like:I'm aware that
pgx/stdlib
support direct passing of a slice as parameter value, butsqlmock
does not. Hence we need to implement thedriver.Valuer
interface like above. The newpgtype.Array
does not implementdriver.Valuer
anymore.I've read the conversation in #1458. It pointed out, the
Map
type can be used for scanning into arrays, which is fine for that case.Describe the solution you'd like
It would be awesome if one of the following were to be implemented:
pgtype.Array
gets aValue()
method.pgtype.Map
gets aSQLValuer()
or similar method.Describe alternatives you've considered
I'm aware this is not a
pgx
issue necessarily, as we are the ones swapping to another (mock) driver.sqlmock
provides the possibility to use adriver.ValueConverter
instead. As some discussions over there suggest, the driver might already export aValueConverter
which then can be used. However, I was not able to find anything in the pgx project.ValueConverter
somewhere and I missed it, I will gladly use it.2 Implement my own
ValueConverter
as a last resort.Additional context
none
The text was updated successfully, but these errors were encountered: