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

Composite Types in Conjunction with RowToStructByName #2180

Open
jacobmikesell opened this issue Nov 26, 2024 · 0 comments
Open

Composite Types in Conjunction with RowToStructByName #2180

jacobmikesell opened this issue Nov 26, 2024 · 0 comments

Comments

@jacobmikesell
Copy link

jacobmikesell commented Nov 26, 2024

Is your feature request related to a problem? Please describe.
I'd like to be able to maintain only a stable struct mapping (with no special interfaces) even when using a composite type in the query.

Example (the names aren't great sorry):

type myOtherTable struct {
      SomeId int64
      SomeBool bool
}
type myTable struct {
     SomeField string
     TableValue []myOtherTable
}


func (x *myTable) ScanRow(rows pgx.Rows) error {
	val, err := pgx.RowToStructByName[myTable](rows)
	if err != nil {
		return err
	}
	*x = val
	return nil
}
create table my_table (pk bigserial primary key, some_field text);
create table my_other_table (pk bigserial primary key, my_fk bigint, some_bool bool);

select some_field, array_agg(mo) as table_value from
my_table inner m join my_other_table mo on m.pk = mo.my_fk

I'd like to be able to

var result myTable
pool.QueryRow("query").Scan(&myTable)

Describe the solution you'd like
I think this should be reasonable. The composite pgtype appears to have access to the column names, given that a "default" behavior (or new interface to meet) seems like it could use reflection to match to struct fields (by tag/default) instead of requiring field index matching.

Describe alternatives you've considered
I've implemented by meeting the existing interface, but it ends up creating friction when the rest of the struct maps by struct tag.

Additional context
I'm willing to put up a PR for this if the design/idea is valid and fits the ecosystem here!

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

1 participant