Skip to content

Commit e9a94c2

Browse files
authored
feat(bigquery): improve error when reading null values (#6566)
Improve error message when reading NULL values from a query and trying to fit into a struct field. Resolves #2612
1 parent 6b0ac0c commit e9a94c2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

β€Žbigquery/value.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ func runOps(ops []structLoaderOp, vstruct reflect.Value, values []Value) error {
455455
err = setRepeated(field, values[op.valueIndex].([]Value), op.setFunc)
456456
} else {
457457
err = op.setFunc(field, values[op.valueIndex])
458+
if errors.Is(err, errNoNulls) {
459+
f := vstruct.Type().FieldByIndex(op.fieldIndex)
460+
err = fmt.Errorf("bigquery: NULL cannot be assigned to field `%s` of type %s", f.Name, f.Type.Name())
461+
}
458462
}
459463
if err != nil {
460464
return err

β€Žbigquery/value_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ package bigquery
1616

1717
import (
1818
"encoding/base64"
19+
"errors"
1920
"fmt"
2021
"math"
2122
"math/big"
23+
"reflect"
2224
"testing"
2325
"time"
2426

@@ -1261,14 +1263,21 @@ func TestStructLoaderErrors(t *testing.T) {
12611263
S string
12621264
D civil.Date
12631265
}
1266+
vstruct := reflect.ValueOf(s{}).Type()
1267+
fieldNames := []string{"I", "F", "B", "S", "D"}
12641268
vals := []Value{int64(0), 0.0, false, "", testDate}
12651269
mustLoad(t, &s{}, schema, vals)
12661270
for i, e := range vals {
12671271
vals[i] = nil
12681272
got := load(&s{}, schema, vals)
1269-
if got != errNoNulls {
1273+
if errors.Is(got, errNoNulls) {
12701274
t.Errorf("#%d: got %v, want %v", i, got, errNoNulls)
12711275
}
1276+
f, _ := vstruct.FieldByName(fieldNames[i])
1277+
expectedError := fmt.Sprintf("bigquery: NULL cannot be assigned to field `%s` of type %s", f.Name, f.Type.Name())
1278+
if got.Error() != expectedError {
1279+
t.Errorf("#%d: got %v, want %v", i, got, expectedError)
1280+
}
12721281
vals[i] = e
12731282
}
12741283

0 commit comments

Comments
 (0)