-
I am using https://github.com/oapi-codegen/nullable to define a struct where some fields may be explicitly set to null in the DB. My struct: type MyThing struct {
Id int `db:"id"`
FirstThing nullable.Nullable[string] `db:"first"`
NextThing nullable.Nullable[[]string] `db:"next"` https://pkg.go.dev/github.com/oapi-codegen/nullable#Nullable It's a really useful convenience when unmarshalling JSON structs. This is my first time trying to use I then try to pull out the data with: q, err := conn.Query(ctx, "SELECT * FROM mytable")
if err != nil {...}
results, err := pgx.CollectRows(q, pgx.RowToStructByName[MyThing]) The
Am I just trying to do something incompatible with pgx? It doesn't look like #2188 or #1434 are similar enough to what I'm trying to do, but I may not be understanding. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Those types do not implement any database/sql or pgx interfaces so pgx has no way of knowing what to do with them. It's not just Presumably, you cannot modify that library so implementing the proper interfaces is not an option. However, pgx does have a way to support 3rd party types that cannot be modified. See https://github.com/jackc/pgx-shopspring-decimal for an example of how to support a 3rd party type. It can be a bit of work though. The adapter for shopspring/decimal is about 300 LOC. Start with the Also, I doubt it can be done generically, so you would need to write the adapter code for each concrete type (e.g. |
Beta Was this translation helpful? Give feedback.
Those types do not implement any database/sql or pgx interfaces so pgx has no way of knowing what to do with them. It's not just
RowToStructByName
, it wouldn't work with a normalQuery
/Scan
either.Presumably, you cannot modify that library so implementing the proper interfaces is not an option. However, pgx does have a way to support 3rd party types that cannot be modified. See https://github.com/jackc/pgx-shopspring-decimal for an example of how to support a 3rd party type. It can be a bit of work though. The adapter for shopspring/decimal is about 300 LOC. Start with the
Register
function if you want to explore the code.Also, I doubt it can be done generically, so you would need to …