From c276428bca79702245d422849af6472bb2e74171 Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Wed, 29 Dec 2021 17:06:09 +0900 Subject: [PATCH] feat(spanner): Add ReadRowWithOptions method (#5240) Co-authored-by: rahul2393 --- spanner/client_test.go | 15 +++++++++++++++ spanner/transaction.go | 10 +++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spanner/client_test.go b/spanner/client_test.go index 3e79710f1b3..e8c780fecee 100644 --- a/spanner/client_test.go +++ b/spanner/client_test.go @@ -2860,6 +2860,21 @@ func TestClient_Single_Read_WithNumericKey(t *testing.T) { } } +func TestClient_Single_ReadRowWithOptions(t *testing.T) { + t.Parallel() + + _, client, teardown := setupMockedTestServer(t) + defer teardown() + ctx := context.Background() + row, err := client.Single().ReadRowWithOptions(ctx, "Albums", Key{"foo"}, []string{"SingerId", "AlbumId", "AlbumTitle"}, &ReadOptions{RequestTag: "foo/bar"}) + if err != nil { + t.Fatalf("Unexpected error for read row with options: %v", err) + } + if row == nil { + t.Fatal("ReadRowWithOptions did not return a row") + } +} + func TestClient_CloseWithUnresponsiveBackend(t *testing.T) { t.Parallel() diff --git a/spanner/transaction.go b/spanner/transaction.go index b9da4f401fe..4ab2e76a212 100644 --- a/spanner/transaction.go +++ b/spanner/transaction.go @@ -212,7 +212,15 @@ func errMultipleRowsFound(table string, key Key, index string) error { // If no row is present with the given key, then ReadRow returns an error where // spanner.ErrCode(err) is codes.NotFound. func (t *txReadOnly) ReadRow(ctx context.Context, table string, key Key, columns []string) (*Row, error) { - iter := t.Read(ctx, table, key, columns) + return t.ReadRowWithOptions(ctx, table, key, columns, nil) +} + +// ReadRowWithOptions reads a single row from the database. Pass a ReadOptions to modify the read operation. +// +// If no row is present with the given key, then ReadRowWithOptions returns an error where +// spanner.ErrCode(err) is codes.NotFound. +func (t *txReadOnly) ReadRowWithOptions(ctx context.Context, table string, key Key, columns []string, opts *ReadOptions) (*Row, error) { + iter := t.ReadWithOptions(ctx, table, key, columns, opts) defer iter.Stop() row, err := iter.Next() switch err {