From daf2efa3137769714e3347a1d95ac00f6ed7a6e9 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 23 Dec 2014 21:58:48 -0600 Subject: [PATCH] Tweak handling of reading null as raw bytes. * NULL maps to nil not empty slice * Handle NULL in Scan not ReadBytes --- query.go | 6 +++++- query_test.go | 8 ++++---- value_reader.go | 3 ++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/query.go b/query.go index 8f7e672a..591303ce 100644 --- a/query.go +++ b/query.go @@ -227,7 +227,11 @@ func (rows *Rows) Scan(dest ...interface{}) (err error) { if vr.Type().DataType == ByteaOid { *d = decodeBytea(vr) } else { - *d = vr.ReadBytes(vr.Len()) + if vr.Len() != -1 { + *d = vr.ReadBytes(vr.Len()) + } else { + *d = nil + } } case *int64: *d = decodeInt8(vr) diff --git a/query_test.go b/query_test.go index 6cb384df..f5688b11 100644 --- a/query_test.go +++ b/query_test.go @@ -817,8 +817,8 @@ func TestReadingNullByteArray(t *testing.T) { t.Fatalf("conn.QueryRow failed: ", err) } - if len(a) != 0 { - t.Errorf("Expected 'a' to have length 0, but it was: ", len(a)) + if a != nil { + t.Errorf("Expected 'a' to be nil, but it was: %v", a) } } @@ -838,8 +838,8 @@ func TestReadingNullByteArrays(t *testing.T) { if err := rows.Scan(&a); err != nil { t.Fatalf("failed to scan row", err) } - if len(a) != 0 { - t.Errorf("Expected 'a' to have length 0, but it was: ", len(a)) + if a != nil { + t.Errorf("Expected 'a' to be nil, but it was: %v", a) } } if count != 2 { diff --git a/value_reader.go b/value_reader.go index 3b388825..359f2f7e 100644 --- a/value_reader.go +++ b/value_reader.go @@ -113,7 +113,8 @@ func (r *ValueReader) ReadBytes(count int32) []byte { return nil } - if count == -1 { + if count < 0 { + r.Fatal(errors.New("count must not be negative")) return nil }