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 saving nested byte slice #8540

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 additions & 0 deletions datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,75 @@ func TestPutInvalidEntity(t *testing.T) {
})
}

func TestPutNestedArray(t *testing.T) {
type InnerByte struct {
ArrByte []byte
}

type OuterByte struct {
InnerArr []InnerByte `datastore:"arrbyte,flatten"`
}

type InnerStr struct {
ArrStr []string
}

type OuterStr struct {
InnerArr []InnerStr `datastore:"arrstr,flatten"`
}

ctx := context.Background()
client := &Client{
client: &fakeDatastoreClient{
commit: func(req *pb.CommitRequest) (*pb.CommitResponse, error) {
return &pb.CommitResponse{}, nil
},
},
}

testcases := []struct {
desc string
src interface{}
key *Key
wantFailure bool
wantErrMsg string
}{
{
desc: "Nested byte slice should pass",
src: &OuterByte{
InnerArr: []InnerByte{
{
ArrByte: []byte("Test string"),
},
},
},
key: NameKey("OuterByte", "OuterByte1", nil),
},
{
desc: "Nested slice not of byte type should fail",
src: &OuterStr{
InnerArr: []InnerStr{
{
ArrStr: []string{"a", "b"},
},
},
},
key: NameKey("OuterStr", "OuterStr1", nil),
wantFailure: true,
wantErrMsg: "flattening nested structs leads to a slice of slices",
},
}

for _, tc := range testcases {
_, gotErr := client.Put(ctx, tc.key, tc.src)
gotFailure := gotErr != nil
if gotFailure != tc.wantFailure ||
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
(gotErr != nil && !strings.Contains(gotErr.Error(), tc.wantErrMsg)) {
t.Errorf("%q: Mismatch in error got: %v, want: %q", tc.desc, gotErr, tc.wantErrMsg)
}
}
}

func TestDeferred(t *testing.T) {
type Ent struct {
A int
Expand Down
2 changes: 1 addition & 1 deletion datastore/prop.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func validateChildType(t reflect.Type, fieldName string, flatten, prevSlice bool

switch t.Kind() {
case reflect.Slice:
if flatten && prevSlice {
if flatten && prevSlice && t.Elem().Kind() != reflect.Uint8 {
bhshkh marked this conversation as resolved.
Show resolved Hide resolved
kolea2 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("datastore: flattening nested structs leads to a slice of slices: field %q", fieldName)
}
return validateChildType(t.Elem(), fieldName, flatten, true, prevTypes)
Expand Down
Loading