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

fix(datastore): Handle loading nil values #8544

Merged
merged 7 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions datastore/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,15 @@ func setVal(v reflect.Value, p Property) (s string) {
return fmt.Sprintf("%v is unsettable", v.Type())
}

rpValue := reflect.ValueOf(pValue)
if !rpValue.Type().AssignableTo(v.Type()) {
return fmt.Sprintf("%q is not assignable to %q", rpValue.Type(), v.Type())
if pValue == nil {
v.Set(reflect.Zero(v.Type()))
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
} else {
rpValue := reflect.ValueOf(pValue)
if !rpValue.Type().AssignableTo(v.Type()) {
return fmt.Sprintf("%q is not assignable to %q", rpValue.Type(), v.Type())
}
v.Set(rpValue)
}
v.Set(rpValue)

case reflect.Ptr:
// v must be a pointer to either a Key, an Entity, or one of the supported basic types.
Expand Down
17 changes: 17 additions & 0 deletions datastore/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,23 @@ func TestLoadNull(t *testing.T) {
}
}

func TestLoadNilInterface(t *testing.T) {
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
type WithAny struct {
AnyField interface{}
}

withAny1 := &WithAny{}
err := loadEntityProto(withAny1, &pb.Entity{
Key: keyToProto(testKey0),
Properties: map[string]*pb.Value{
"AnyField": {ValueType: &pb.Value_NullValue{}},
},
})
if err != nil {
t.Fatalf("got: %v, want: nil", err)
}
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
}

type KeyLoaderEnt struct {
A int
K *Key
Expand Down