Skip to content

Commit ada9642

Browse files
committed
uuid: Handle NULL values when scanning from Spanner
Fixes NULL handling when scanning from a Spanner BYTES value to NullUUID using github.com/googleapis/go-sql-spanner Add test for NULL handling a Spanner BYTES column value Change-Id: I8e48333b9f8df4076a41f4f111c6dd3c615f60d1
1 parent 26b5378 commit ada9642

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

uuid/db.go

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ func (n *NullUUID) Scan(value interface{}) error {
7474
return nil
7575
}
7676

77+
// a NULL BYTES value gets returned from Spanner as an empty []byte
78+
if v, ok := value.([]byte); ok {
79+
if v == nil {
80+
n.UUID, n.Valid = UUID{}, false
81+
return nil
82+
}
83+
}
84+
7785
n.Valid = true
7886
return n.UUID.Scan(value)
7987
}

uuid/db_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package uuid_test
66
import (
77
"testing"
88

9+
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011

1112
"storj.io/common/uuid"
@@ -77,4 +78,52 @@ func TestNullUUID_SpannerEncoding(t *testing.T) {
7778
require.Equal(t, original, res)
7879
})
7980

81+
t.Run("null bytes decoding", func(t *testing.T) {
82+
// a NULL BYTES column is returned as an empty uninitialized byte slice
83+
tests := []struct {
84+
name string
85+
input any
86+
want uuid.NullUUID
87+
wantErr bool
88+
}{
89+
{
90+
name: "nil succeeds and is invalid",
91+
input: nil,
92+
want: uuid.NullUUID{
93+
UUID: uuid.UUID{},
94+
Valid: false,
95+
},
96+
wantErr: false,
97+
},
98+
{
99+
name: "empty instantiated byte slice fails and errors",
100+
input: []byte{},
101+
want: uuid.NullUUID{
102+
UUID: uuid.UUID{},
103+
Valid: true,
104+
},
105+
wantErr: true,
106+
},
107+
{
108+
name: "instantiated byte slice with nil succeeds and errors",
109+
input: []byte{},
110+
want: uuid.NullUUID{
111+
UUID: uuid.UUID{},
112+
Valid: true,
113+
},
114+
wantErr: true,
115+
},
116+
}
117+
118+
for _, tt := range tests {
119+
t.Run(tt.name, func(t *testing.T) {
120+
var n uuid.NullUUID
121+
err := n.DecodeSpanner(tt.input)
122+
if (err != nil) != tt.wantErr {
123+
t.Errorf("DecodeSpanner() error = %v, wantErr %v", err, tt.wantErr)
124+
}
125+
assert.Equal(t, tt.want, n)
126+
})
127+
}
128+
})
80129
}

0 commit comments

Comments
 (0)