Skip to content

Commit

Permalink
fix(datastore): allow non-empty values in the slice
Browse files Browse the repository at this point in the history
  • Loading branch information
bhshkh committed Aug 30, 2024
1 parent ec0cbb2 commit bc6d78f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func putMutations(keys []*Key, src interface{}) ([]*pb.Mutation, error) {
v := reflect.ValueOf(src)
var multiArgType multiArgType

// If kind is of type slice, return error
// If kind is not of type slice, return error
if kind := v.Kind(); kind != reflect.Slice {
return nil, fmt.Errorf("%w: dst: expected slice got %v", ErrInvalidEntityType, kind.String())
}
Expand Down
14 changes: 10 additions & 4 deletions datastore/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,17 @@ func saveSliceProperty(props *[]Property, name string, opts saveOpts, v reflect.
return err
}
for _, p := range elemProps {
v, ok := values[p.Name]
if !ok {
return fmt.Errorf("datastore: unexpected property %q in elem %d of slice", p.Name, i)
if len(values) != 0 {
v, ok := values[p.Name]
if !ok {
return fmt.Errorf("datastore: unexpected property %q in elem %d of slice", p.Name, i)
}
values[p.Name] = append(v, p.Value)
} else {
// This is the first non-empty element
values[p.Name] = []interface{}{p.Value}
headProps = elemProps
}
values[p.Name] = append(v, p.Value)
}
}

Expand Down
19 changes: 19 additions & 0 deletions datastore/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,25 @@ var testCases = []testCase{
"",
"",
},
{
"omit empty, fields populated, 1st, 2nd and 4th elements in slice are empty",
&Omit{
A: "a",
B: 10,
C: true,
F: []int{0, 0, 11, 0, 12, 0},
S: S{St: "string"},
},
&PropertyList{
Property{Name: "A", Value: "a", NoIndex: false},
Property{Name: "Bb", Value: int64(10), NoIndex: false},
Property{Name: "C", Value: true, NoIndex: true},
Property{Name: "F", Value: []interface{}{int64(11), int64(12)}, NoIndex: false},
Property{Name: "St", Value: "string", NoIndex: false},
},
"",
"",
},
{
"omit empty does not propagate",
&NoOmits{
Expand Down

0 comments on commit bc6d78f

Please sign in to comment.