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): allow non-empty values in the slice #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
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
Loading