Skip to content

Commit

Permalink
fix(datastore): fix NoIndex for array property
Browse files Browse the repository at this point in the history
When converting proto to Entity with array property, excludeFromIndexes appears in the values level, not the top level.
  • Loading branch information
aaron0x committed Apr 1, 2023
1 parent 9334a1d commit 79c74f6
Show file tree
Hide file tree
Showing 2 changed files with 63 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
56 changes: 56 additions & 0 deletions datastore/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,62 @@ 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)
}

if !testutil.Equal(want, dst) {
t.Errorf("NoIndex should be correct: compare:\ngot: %#v\nwant: %#v", dst, want)
}
}

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

0 comments on commit 79c74f6

Please sign in to comment.