Skip to content

Composite Types in Conjunction with RowToStructByName #2180

@jacobmikesell

Description

@jacobmikesell

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions