aboutsummaryrefslogtreecommitdiff
path: root/internal/database
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-01-19 18:16:30 -0500
committerJonathan Amsterdam <jba@google.com>2021-01-20 13:06:17 +0000
commit835d692f07f1f85d91ffef43d55b4d0ece4d96a0 (patch)
treeb2cf8bf52a1659696eb3de6aa3cc09a9c6d4e3e5 /internal/database
parent9c5f49ba29843c807e04ecc60250a56a0b844983 (diff)
downloadgo-x-pkgsite-835d692f07f1f85d91ffef43d55b4d0ece4d96a0.tar.xz
internal/database: fix StructScanner for byte slices
StructScanner was wrapping `[]byte` fields in pq.Array. It shouldn't. Change-Id: Id7267178d2be3c805c00f176a85a35217bd268ed Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/284237 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/database')
-rw-r--r--internal/database/reflect.go4
-rw-r--r--internal/database/reflect_test.go29
2 files changed, 19 insertions, 14 deletions
diff --git a/internal/database/reflect.go b/internal/database/reflect.go
index a1f10ecb..88491597 100644
--- a/internal/database/reflect.go
+++ b/internal/database/reflect.go
@@ -71,7 +71,9 @@ func structScannerForType(t reflect.Type) func(p interface{}) []interface{} {
p := v.Field(info.num).Addr().Interface()
switch info.kind {
case reflect.Slice:
- p = pq.Array(p)
+ if _, ok := p.(*[]byte); !ok {
+ p = pq.Array(p)
+ }
case reflect.Ptr:
p = NullPtr(p)
default:
diff --git a/internal/database/reflect_test.go b/internal/database/reflect_test.go
index 11882db9..2d4f5380 100644
--- a/internal/database/reflect_test.go
+++ b/internal/database/reflect_test.go
@@ -19,6 +19,7 @@ type testStruct struct {
Score int
Slice []int64
Ptr *int64
+ Bytes []byte
}
func TestNullPtr(t *testing.T) {
@@ -56,7 +57,8 @@ func TestStructScanner(t *testing.T) {
if err := args[3].(nullPtr).Scan(int64(9)); err != nil {
t.Fatal(err)
}
- want := testStruct{"foo", 3, []int64{1, 2, 3}, intptr(9)}
+ *args[4].(*[]byte) = []byte("abc")
+ want := testStruct{"foo", 3, []int64{1, 2, 3}, intptr(9), []byte("abc")}
if !cmp.Equal(s, want) {
t.Errorf("got %+v, want %+v", s, want)
}
@@ -72,32 +74,33 @@ func TestCollectStructs(t *testing.T) {
}
_, err := testDB.Exec(ctx, `
CREATE TABLE structs (
- name text NOT NULL,
- score integer NOT NULL,
- slice integer[],
- nullable integer
+ name text NOT NULL,
+ score integer NOT NULL,
+ slice integer[],
+ nullable integer,
+ bytes bytea
)`)
if err != nil {
t.Fatal(err)
}
- if err := testDB.BulkInsert(ctx, "structs", []string{"name", "score", "slice", "nullable"}, []interface{}{
- "A", 1, pq.Array([]int64(nil)), 7,
- "B", 2, pq.Array([]int64{1, 2}), -8,
- "C", 3, pq.Array([]int64{}), nil,
+ if err := testDB.BulkInsert(ctx, "structs", []string{"name", "score", "slice", "nullable", "bytes"}, []interface{}{
+ "A", 1, pq.Array([]int64(nil)), 7, nil,
+ "B", 2, pq.Array([]int64{1, 2}), -8, []byte("abc"),
+ "C", 3, pq.Array([]int64{}), nil, []byte("def"),
}, ""); err != nil {
t.Fatal(err)
}
- query := `SELECT name, score, slice, nullable FROM structs`
+ query := `SELECT name, score, slice, nullable, bytes FROM structs`
var got []testStruct
if err := testDB.CollectStructs(ctx, &got, query); err != nil {
t.Fatal(err)
}
sort.Slice(got, func(i, j int) bool { return got[i].Name < got[j].Name })
want := []testStruct{
- {"A", 1, nil, intptr(7)},
- {"B", 2, []int64{1, 2}, intptr(-8)},
- {"C", 3, []int64{}, nil},
+ {"A", 1, nil, intptr(7), nil},
+ {"B", 2, []int64{1, 2}, intptr(-8), []byte("abc")},
+ {"C", 3, []int64{}, nil, []byte("def")},
}
if !cmp.Equal(got, want) {
t.Errorf("got %+v, want %+v", got, want)