You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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:
not that easy to find this way
some people might prefer the convenience solution as Jille said
Activity
Allow for extra parameters to the queryType
Support `:many key=group_id` to return a map instead of a slice
Allow for extra parameters to the queryType
Support `:many key=group_id` to return a map instead of a slice
Allow for extra parameters to the queryType
Allow for extra parameters to the queryType
Support `:many key=group_id` to return a map instead of a slice
Support `:many key=group_id` to return a map instead of a slice
Support `:many key=group_id` to return a map instead of a slice
Support `:many key=group_id` to return a map instead of a slice
nussjustin commentedon Sep 6, 2023
What about also adding an option to allow multiple values per key? So instead of
map[string]gendb.Userone could get amap[string][]gendb.UserWhile not as common as the case with one value per key, I still sometimes find myself implementing this.
rpstw commentedon Sep 13, 2023
Another way of solving the problem:
listXXPros:
map[int]SomeStructinstead ofmap[string]SomeStructbecause another packages wants a string column instead of a int oneCons: