Skip to content

Commit

Permalink
fix(datastore): Handling nil slices in save and query (#8043)
Browse files Browse the repository at this point in the history
* fix(datastore): Handling nil slices in save and query

* fix(datastore): Handling nil slices in save & query
  • Loading branch information
bhshkh committed Jun 12, 2023
1 parent ea232b1 commit 36f01e9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions datastore/load.go
Expand Up @@ -161,6 +161,11 @@ func (l *propertyLoader) loadOneElement(codec fields.List, structValue reflect.V
structValue = v
}

if v.Kind() == reflect.Slice && p.Value == nil {
v.Set(reflect.Zero(v.Type()))
return ""
}

// If the element is a slice, we need to accommodate it.
if v.Kind() == reflect.Slice && v.Type() != typeOfByteSlice {
if l.m == nil {
Expand Down
12 changes: 12 additions & 0 deletions datastore/load_test.go
Expand Up @@ -316,6 +316,18 @@ func TestLoadEntityNested(t *testing.T) {
A: []Simple{{I: 3}, {I: 4}},
},
},
{
desc: "nested simple with nil slice",
src: &pb.Entity{
Properties: map[string]*pb.Value{
"A": {ValueType: &pb.Value_NullValue{}},
},
},

want: &NestedSliceOfSimple{
A: nil,
},
},
{
desc: "nested with multiple anonymous fields",
src: &pb.Entity{
Expand Down
5 changes: 5 additions & 0 deletions datastore/save.go
Expand Up @@ -265,6 +265,11 @@ func (s structPLS) key(v reflect.Value) (*Key, error) {
func saveSliceProperty(props *[]Property, name string, opts saveOpts, v reflect.Value) error {
// Easy case: if the slice is empty, we're done.
if v.Len() == 0 {
*props = append(*props, Property{
Name: name,
Value: nil,
NoIndex: opts.noIndex,
})
return nil
}
// Work out the properties generated by the first element in the slice. This will
Expand Down

0 comments on commit 36f01e9

Please sign in to comment.