Skip to content

Commit

Permalink
fixed/manipmongo: consider null to be false in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
primalmotion committed Mar 14, 2019
1 parent d12fd4f commit 861961b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
20 changes: 19 additions & 1 deletion manipmongo/internal/compiler/filter.go
Expand Up @@ -56,7 +56,25 @@ func CompileFilter(f *manipulate.Filter) bson.M {
switch f.Comparators()[i] {

case manipulate.EqualComparator:
items = append(items, bson.M{k: bson.M{"$eq": massageValue(f.Values()[i][0])}})
v := f.Values()[i][0]
switch b := v.(type) {
case bool:
if b {
items = append(items, bson.M{k: bson.M{"$eq": v}})
} else {
items = append(
items,
bson.M{
"$or": []bson.M{
bson.M{k: bson.M{"$eq": v}},
bson.M{k: bson.M{"$exists": false}},
},
},
)
}
default:
items = append(items, bson.M{k: bson.M{"$eq": massageValue(v)}})
}

case manipulate.NotEqualComparator:
items = append(items, bson.M{k: bson.M{"$ne": massageValue(f.Values()[i][0])}})
Expand Down
28 changes: 28 additions & 0 deletions manipmongo/internal/compiler/filter_test.go
Expand Up @@ -25,6 +25,34 @@ func TestUtils_compiler(t *testing.T) {
})
})

Convey("Given I have a simple manipulate.Filter with boolean set to true", t, func() {

f := manipulate.NewFilterComposer().WithKey("bool").Equals(true).Done()

Convey("When I compile the filter", func() {

b, _ := bson.MarshalJSON(CompileFilter(f))

Convey("Then the bson should be correct", func() {
So(strings.Replace(string(b), "\n", "", 1), ShouldEqual, `{"$and":[{"bool":{"$eq":true}}]}`)
})
})
})

Convey("Given I have a simple manipulate.Filter with boolean set to false", t, func() {

f := manipulate.NewFilterComposer().WithKey("bool").Equals(false).Done()

Convey("When I compile the filter", func() {

b, _ := bson.MarshalJSON(CompileFilter(f))

Convey("Then the bson should be correct", func() {
So(strings.Replace(string(b), "\n", "", 1), ShouldEqual, `{"$and":[{"$or":[{"bool":{"$eq":false}},{"bool":{"$exists":false}}]}]}`)
})
})
})

Convey("Given I have a simple manipulate.Filter with dots", t, func() {

f := manipulate.NewFilterComposer().WithKey("X.TOTO.Titu").Equals(1).Done()
Expand Down

0 comments on commit 861961b

Please sign in to comment.