Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): improve error when reading null values #6566

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions bigquery/value.go
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A thing to keep an eye on for the future is whether we should have a custom error type. My suspicion is this isn't a frequent enough failure to warrant custom errors at this point, nor is it super actionable unless you're dynamically rewriting things.

}
}
if err != nil {
return err
Expand Down
11 changes: 10 additions & 1 deletion bigquery/value_test.go
Expand Up @@ -16,9 +16,11 @@ package bigquery

import (
"encoding/base64"
"errors"
"fmt"
"math"
"math/big"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -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
}

Expand Down