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

docstore/all - add support for boolean filter #3446

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docstore/awsdynamodb/create_tables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ aws dynamodb create-table \
AttributeName=Player,AttributeType=S \
AttributeName=Score,AttributeType=N \
AttributeName=Time,AttributeType=S \
AttributeName=WithGlitch,AttributeType=BOOL \
--key-schema AttributeName=Game,KeyType=HASH AttributeName=Player,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--local-secondary-indexes \
Expand Down
27 changes: 17 additions & 10 deletions docstore/drivertest/drivertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ type HighScore struct {
Player string
Score int
Time time.Time
WithGlitch bool
DocstoreRevision interface{}
}

Expand Down Expand Up @@ -1316,14 +1317,14 @@ const (
)

var highScores = []*HighScore{
{game1, "pat", 49, date(3, 13), nil},
{game1, "mel", 60, date(4, 10), nil},
{game1, "andy", 81, date(2, 1), nil},
{game1, "fran", 33, date(3, 19), nil},
{game2, "pat", 120, date(4, 1), nil},
{game2, "billie", 111, date(4, 10), nil},
{game2, "mel", 190, date(4, 18), nil},
{game2, "fran", 33, date(3, 20), nil},
{game1, "pat", 49, date(3, 13), false, nil},
{game1, "mel", 60, date(4, 10), false, nil},
{game1, "andy", 81, date(2, 1), false, nil},
{game1, "fran", 33, date(3, 19), false, nil},
{game2, "pat", 120, date(4, 1), true, nil},
{game2, "billie", 111, date(4, 10), false, nil},
{game2, "mel", 190, date(4, 18), true, nil},
{game2, "fran", 33, date(3, 20), false, nil},
}

func addHighScores(t *testing.T, coll *docstore.Collection) {
Expand Down Expand Up @@ -1485,6 +1486,11 @@ func testGetQuery(t *testing.T, _ Harness, coll *docstore.Collection) {
q: coll.Query().Where("Player", "not-in", []string{"pat", "billie"}),
want: func(h *HighScore) bool { return h.Player != "pat" && h.Player != "billie" },
},
{
name: "WithGlitch",
q: coll.Query().Where("WithGlitch", "=", true),
want: func(h *HighScore) bool { return h.WithGlitch },
},
{
name: "AllByPlayerAsc",
q: coll.Query().OrderBy("Player", docstore.Ascending),
Expand Down Expand Up @@ -1522,13 +1528,14 @@ func testGetQuery(t *testing.T, _ Harness, coll *docstore.Collection) {
want: func(h *HighScore) bool {
h.Score = 0
h.Time = time.Time{}
h.WithGlitch = false
return true
},
},
{
name: "AllWithScore",
q: coll.Query(),
fields: []docstore.FieldPath{"Game", "Player", "Score", docstore.FieldPath(docstore.DefaultRevisionField)},
fields: []docstore.FieldPath{"Game", "Player", "Score", "WithGlitch", docstore.FieldPath(docstore.DefaultRevisionField)},
want: func(h *HighScore) bool {
h.Time = time.Time{}
return true
Expand Down Expand Up @@ -2118,7 +2125,7 @@ func testAs(t *testing.T, coll *docstore.Collection, st AsTest) {
}

// ErrorCheck
doc := &HighScore{game3, "steph", 24, date(4, 25), nil}
doc := &HighScore{game3, "steph", 24, date(4, 25), false, nil}
if err := coll.Create(ctx, doc); err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 11 additions & 0 deletions docstore/gcpfirestore/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ func evaluateFilter(f driver.Filter, doc driver.Document) bool {
return applyComparison(f.Op, strings.Compare(lhs.String(), rhs.String()))
}

if lhs.Kind() == reflect.Bool {
if rhs.Kind() != reflect.Bool {
return false
}
cmp := 0
if lhs.Bool() != rhs.Bool() {
cmp = -1
}
return applyComparison(f.Op, cmp)
}

cmp, err := driver.CompareNumbers(lhs, rhs)
if err != nil {
return false
Expand Down
6 changes: 6 additions & 0 deletions docstore/memdocstore/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ func compare(x1, x2 interface{}) (int, bool) {
return driver.CompareTimes(t1, t2), true
}
}
if v1.Kind() == reflect.Bool && v2.Kind() == reflect.Bool {
if v1.Bool() == v2.Bool() {
return 0, true
}
return -1, true
}
return 0, false
}

Expand Down
16 changes: 13 additions & 3 deletions docstore/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Collection) Query() *Query {

// Where expresses a condition on the query.
// Valid ops are: "=", ">", "<", ">=", "<=, "in", "not-in".
// Valid values are strings, integers, floating-point numbers, and time.Time values.
// Valid values are strings, integers, floating-point numbers, time.Time and boolean (only for "=", "in" and "not-in") values.
func (q *Query) Where(fp FieldPath, op string, value interface{}) *Query {
if q.err != nil {
return q
Expand Down Expand Up @@ -66,7 +66,7 @@ func (q *Query) Where(fp FieldPath, op string, value interface{}) *Query {
type valueValidator func(interface{}) bool

var validOp = map[string]valueValidator{
"=": validFilterValue,
"=": validEqualValue,
">": validFilterValue,
"<": validFilterValue,
">=": validFilterValue,
Expand All @@ -75,6 +75,16 @@ var validOp = map[string]valueValidator{
"not-in": validFilterSlice,
}

func validEqualValue(v interface{}) bool {
if v == nil {
return false
}
if reflect.TypeOf(v).Kind() == reflect.Bool {
return true
}
return validFilterValue(v)
}

func validFilterValue(v interface{}) bool {
if v == nil {
return false
Expand Down Expand Up @@ -102,7 +112,7 @@ func validFilterSlice(v interface{}) bool {
}
vv := reflect.ValueOf(v)
for i := 0; i < vv.Len(); i++ {
if !validFilterValue(vv.Index(i).Interface()) {
if !validEqualValue(vv.Index(i).Interface()) {
return false
}
}
Expand Down
Loading