googleapis / googleapis/google-cloud-go

spanner: support scanning into slice of structs []Entry

Open
#11,090 1 comment 0 reactions 1 assignee Claimed by @harshachinta View on GitHub
api: spanner triage me
Dominant language
Go
Stars
4.5k
Forks
1.6k
Avg merge
1d 13h
Merged PRs (30d)
109

Description

## Is your feature request related to a problem? Please describe.

Currently it's possibly to scan into a slice of pointers to structs, however not to a slice of structs:

``` Go
client, err := spanner.NewClient(ctx, "projects/"+ProjectID+"/instances/"+InstanceID+"/databases/"+DatabaseName)
if err != nil {
panic(err)
}
defer client.Close()

type Entry struct {
Name string
Value int64
}

input := []Entry{
{Name: "Hello", Value: 1000},
{Name: "World", Value: 2000},
}

rows := client.ReadOnlyTransaction().Query(ctx, spanner.Statement{
SQL: `SELECT ARRAY(SELECT AS STRUCT Name, Value FROM UNNEST(@input)) AS entries`,
Params: map[string]any{
"input": input,
},
})
err = rows.Do(func(row *spanner.Row) error {
var output []Entry
if err := row.Columns(&output); err != nil {
return err
}
for _, entry := range output {
fmt.Println(entry)
}
return nil
})
fmt.Fprintln(os.Stderr, err)
```

Fails with:

```
spanner: code = "InvalidArgument", desc = "failed to decode column 0, error = "
```

## Describe the solution you'd like

I would like that using the following would work:

```
var output []Entry
if err := row.Columns(&output); err != nil {
fmt.Fprintln(os.Stderr, err)
}
```

## Describe alternatives you've considered

It is possible to use:

```
var output []*Entry
if err := row.Columns(&output); err != nil {
fmt.Fprintln(os.Stderr, err)
}
```

And then later copy all the scanned values to a `[]Entry` afterwards, however it does add more unnecessary code and it might allocate more.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.