Skip to content

Commit

Permalink
fix(datastore): fix NoIndex for array property (#7674)
Browse files Browse the repository at this point in the history
* fix(datastore): fix NoIndex for array property
When converting proto to Entity with array property, excludeFromIndexes appears in the values level, not the top level.

* fix test

---------

Co-authored-by: meredithslota <meredithslota@google.com>
Co-authored-by: Baha Aiman <bahaaiman@google.com>
  • Loading branch information
3 people committed Aug 8, 2023
1 parent 7a4137a commit 01951e6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion datastore/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,16 @@ func protoToEntity(src *pb.Entity) (*Entity, error) {
if err != nil {
return nil, err
}
noIndex := val.ExcludeFromIndexes
if array := val.GetArrayValue(); array != nil {
if values := array.GetValues(); len(values) > 0 {
noIndex = values[0].ExcludeFromIndexes
}
}
props = append(props, Property{
Name: name,
Value: v,
NoIndex: val.ExcludeFromIndexes,
NoIndex: noIndex,
})
}
var key *Key
Expand Down
63 changes: 63 additions & 0 deletions datastore/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"cloud.google.com/go/civil"
"cloud.google.com/go/internal/testutil"
"github.com/google/go-cmp/cmp/cmpopts"
pb "google.golang.org/genproto/googleapis/datastore/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)
Expand Down Expand Up @@ -602,6 +603,68 @@ func TestTimezone(t *testing.T) {
}
}

func TestLoadArrayIndex(t *testing.T) {
src := &pb.Entity{
Key: keyToProto(testKey0),
Properties: map[string]*pb.Value{
"indexed": {
ValueType: &pb.Value_ArrayValue{
ArrayValue: &pb.ArrayValue{
Values: []*pb.Value{
{
ValueType: &pb.Value_StringValue{StringValue: "1"},
ExcludeFromIndexes: false,
},
{
ValueType: &pb.Value_StringValue{StringValue: "2"},
ExcludeFromIndexes: false,
},
},
},
},
},
"non-indexed": {
ValueType: &pb.Value_ArrayValue{
ArrayValue: &pb.ArrayValue{
Values: []*pb.Value{
{
ValueType: &pb.Value_StringValue{StringValue: "3"},
ExcludeFromIndexes: true,
},
{
ValueType: &pb.Value_StringValue{StringValue: "4"},
ExcludeFromIndexes: true,
},
},
},
},
},
},
}
want := &Entity{
Key: testKey0,
Properties: []Property{
{Name: "indexed", Value: []interface{}{"1", "2"}, NoIndex: false},
{Name: "non-indexed", Value: []interface{}{"3", "4"}, NoIndex: true},
},
}

dst, err := protoToEntity(src)
if err != nil {
t.Fatalf("protoToEntity: %v", err)
}

cmpProperties := func(p1, p2 Property) bool {
return p1.Name < p2.Name
}
if !testutil.Equal(want.Properties, dst.Properties, cmpopts.SortSlices(cmpProperties)) {
t.Errorf("NoIndex should be correct: Property:\ngot: %#v\nwant: %#v", dst, want)
}
if !testutil.Equal(want.Key, dst.Key) {
t.Errorf("NoIndex should be correct: Key:\ngot: %#v\nwant: %#v", dst, want)
}
}

func TestAlreadyPopulatedDst(t *testing.T) {
testCases := []struct {
desc string
Expand Down

0 comments on commit 01951e6

Please sign in to comment.