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

[proposal] :map key=somefield query annotation #2353

Open
Jille opened this issue Jun 21, 2023 · 2 comments
Open

[proposal] :map key=somefield query annotation #2353

Jille opened this issue Jun 21, 2023 · 2 comments
Labels
enhancement New feature or request proposal

Comments

@Jille
Copy link
Contributor

Jille commented Jun 21, 2023

What do you want to change?

I frequently find myself converting the results from a query to a map, keyed by one of the fields. The code looks like this:

m := map[string]gendb.User{}
users, err := gendb.QueryUsers(ctx)
for _, u := range users {
  m[u.Username] = u
}

I've been holding off on this feature request hoping that generics would cleanly solve this, but solutions with generics aren't as pretty as I'd hoped. (Using a Map plus a lambda to return a field.)

Given that sqlc has all the information needed for this, it would be a neat convenience if sqlc could generate this boilerplate for me :)

I'm willing to send a PR if approved.

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

@Jille Jille added enhancement New feature or request triage New issues that hasn't been reviewed labels Jun 21, 2023
@kyleconroy kyleconroy added proposal and removed triage New issues that hasn't been reviewed labels Jun 21, 2023
Jille added a commit to Jille/sqlc that referenced this issue Jun 25, 2023
Like `-- name: InsertMulti :exec multiple` and `-- name: Select :many key=group_id`

issue sqlc-dev#2353
Jille added a commit to Jille/sqlc that referenced this issue Jun 25, 2023
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Like `-- name: InsertMulti :exec multiple` and `-- name: Select :many key=group_id`

issue sqlc-dev#2353
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Like `-- name: InsertMulti :exec multiple` and `-- name: Select :many key=group_id`

issue sqlc-dev#2353
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Like `-- name: InsertMulti :exec multiple` and `-- name: Select :many key=group_id`

issue sqlc-dev#2353
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
Jille added a commit to Jille/sqlc that referenced this issue Jun 28, 2023
@nussjustin
Copy link

What about also adding an option to allow multiple values per key? So instead of map[string]gendb.User one could get a map[string][]gendb.User

While not as common as the case with one value per key, I still sometimes find myself implementing this.

@rpstw
Copy link

rpstw commented Sep 13, 2023

Another way of solving the problem:

  1. generate a unexported method listXX
-- name: listXX :many
select * from xx
func (q *Queries) listXX(ctx context.Context) ([]SomeStruct, error)
  1. manually write a exported one
func (q *Queries) ListXX(ctx context.Context) (map[string]SomeStruct, error) {
   s, _ := q.listXX(ctx)
   m := make(map[string]SomeStruct)
  for _, u := range s {
   m[u.SomeColumn] = u
 }

return m, nil
}

Pros:

  1. less abstraction
  2. being able to do many housekeeping stuff if needed, e.g, return a map[int]SomeStruct instead of map[string]SomeStruct because another packages wants a string column instead of a int one

Cons:

  1. not that easy to find this way
  2. some people might prefer the convenience solution as Jille said

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request proposal
Projects
None yet
Development

No branches or pull requests

4 participants