Skip to content

Commit

Permalink
fix: return error when value conversion is missing (#311)
Browse files Browse the repository at this point in the history
Updates #309
  • Loading branch information
egonelbre authored Nov 13, 2024
1 parent 29e69c7 commit a0bc939
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package spannerdriver

import (
"database/sql/driver"
"fmt"
"io"
"sync"

Expand Down Expand Up @@ -265,7 +266,11 @@ func (r *rows) Next(dest []driver.Value) error {
return err
}
dest[i] = v
default:
return fmt.Errorf("unsupported element type ARRAY<%v>", col.Type.ArrayElementType.Code)
}
default:
return fmt.Errorf("unsupported element type %v", col.Type.Code)
}
// TODO: Implement struct
}
Expand Down
38 changes: 38 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"cloud.google.com/go/spanner"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/protobuf/types/known/structpb"
)

type testIterator struct {
Expand Down Expand Up @@ -81,6 +82,9 @@ func TestRows_Next(t *testing.T) {
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
row := make([]driver.Value, 3)
copy(row, dest)
values = append(values, row)
Expand Down Expand Up @@ -109,3 +113,37 @@ func TestRows_Next(t *testing.T) {
}
}
}

func TestRows_Next_Unsupported(t *testing.T) {
unspecifiedType := &sppb.Type{Code: sppb.TypeCode_TYPE_CODE_UNSPECIFIED}

it := testIterator{
metadata: &sppb.ResultSetMetadata{
RowType: &sppb.StructType{
Fields: []*sppb.StructType_Field{
{Name: "COL1", Type: unspecifiedType},
},
},
},
rows: []*spanner.Row{
newRow(t, []string{"COL1"}, []any{
spanner.GenericColumnValue{
Type: unspecifiedType,
Value: &structpb.Value{},
},
}),
},
}

rows := rows{it: &it}

dest := make([]driver.Value, 1)
err := rows.Next(dest)
if err == nil {
t.Fatal("expected an error, but got nil")
}
const expectedError = "unsupported element type TYPE_CODE_UNSPECIFIED"
if err.Error() != expectedError {
t.Fatalf("expected error %q, but got %q", expectedError, err.Error())
}
}

0 comments on commit a0bc939

Please sign in to comment.