diff --git a/bigquery/value.go b/bigquery/value.go index 8077aee5bf3..d1dffa91ca7 100644 --- a/bigquery/value.go +++ b/bigquery/value.go @@ -455,6 +455,10 @@ func runOps(ops []structLoaderOp, vstruct reflect.Value, values []Value) error { err = setRepeated(field, values[op.valueIndex].([]Value), op.setFunc) } else { err = op.setFunc(field, values[op.valueIndex]) + if errors.Is(err, errNoNulls) { + f := vstruct.Type().FieldByIndex(op.fieldIndex) + err = fmt.Errorf("bigquery: NULL cannot be assigned to field `%s` of type %s", f.Name, f.Type.Name()) + } } if err != nil { return err diff --git a/bigquery/value_test.go b/bigquery/value_test.go index 3bd79615864..6b051366a6f 100644 --- a/bigquery/value_test.go +++ b/bigquery/value_test.go @@ -16,9 +16,11 @@ package bigquery import ( "encoding/base64" + "errors" "fmt" "math" "math/big" + "reflect" "testing" "time" @@ -1261,14 +1263,21 @@ func TestStructLoaderErrors(t *testing.T) { S string D civil.Date } + vstruct := reflect.ValueOf(s{}).Type() + fieldNames := []string{"I", "F", "B", "S", "D"} vals := []Value{int64(0), 0.0, false, "", testDate} mustLoad(t, &s{}, schema, vals) for i, e := range vals { vals[i] = nil got := load(&s{}, schema, vals) - if got != errNoNulls { + if errors.Is(got, errNoNulls) { t.Errorf("#%d: got %v, want %v", i, got, errNoNulls) } + f, _ := vstruct.FieldByName(fieldNames[i]) + expectedError := fmt.Sprintf("bigquery: NULL cannot be assigned to field `%s` of type %s", f.Name, f.Type.Name()) + if got.Error() != expectedError { + t.Errorf("#%d: got %v, want %v", i, got, expectedError) + } vals[i] = e }