Version
1.13.0
What happened?
When executing a :many query that returns an empty slice, the returned slice is nil.
e.g.
With this query,
-- name: GetItems :many
SELECT * FROM items;
The following code is generated
func (q *Queries) GetItems(ctx context.Context) ([]*Item, error) {
rows, err := q.db.Query(ctx, getItems)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*Item
for rows.Next() {
var i Item
if err := rows.Scan(
&i.ID,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
func main() {
items, err := db.GetItems(context.Background())
fmt.Println("items is nil: ", items == nil) // true
fmt.Println("err is nil: ", err == nil) // true
}
IMO, an empty array should be returned instead of nil.
For the usecase, it can be an issue when you want to serialize items as JSON because it gets serialized as null instead of an empty array ([]).
As a fix, it's possible to change the following line in the generated function:
- var items []*Item
+ items := []*Item{}
I can open a PR if that sounds good to you.
Thanks!
Relevant log output
No response
Database schema
No response
SQL queries
No response
Configuration
No response
Playground URL
No response
What operating system are you using?
No response
What database engines are you using?
No response
What type of code are you generating?
No response
Version
1.13.0
What happened?
When executing a
:manyquery that returns an empty slice, the returned slice is nil.e.g.
With this query,
The following code is generated
IMO, an empty array should be returned instead of
nil.For the usecase, it can be an issue when you want to serialize
itemsas JSON because it gets serialized asnullinstead of an empty array ([]).As a fix, it's possible to change the following line in the generated function:
I can open a PR if that sounds good to you.
Thanks!
Relevant log output
No response
Database schema
No response
SQL queries
No response
Configuration
No response
Playground URL
No response
What operating system are you using?
No response
What database engines are you using?
No response
What type of code are you generating?
No response