Skip to content

Implement $nin #18

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

Merged
merged 1 commit into from
Jun 15, 2024
Merged
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
9 changes: 7 additions & 2 deletions filter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,16 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
return "", nil, fmt.Errorf("$and as scalar operator not supported")
case "$not":
return "", nil, fmt.Errorf("$not as scalar operator not supported")
case "$in":
case "$in", "$nin":
if !isScalarSlice(v[operator]) {
return "", nil, fmt.Errorf("invalid value for $in operator (must array of primatives): %v", v[operator])
}
inner = append(inner, fmt.Sprintf("(%s = ANY($%d))", c.columnName(key), paramIndex))
neg := ""
if operator == "$nin" {
// `column != ANY(...)` does not work, so we need to do `NOT column = ANY(...)` instead.
neg = "NOT "
}
inner = append(inner, fmt.Sprintf("(%s%s = ANY($%d))", neg, c.columnName(key), paramIndex))
paramIndex++
if c.arrayDriver != nil {
v[operator] = c.arrayDriver(v[operator])
Expand Down
14 changes: 11 additions & 3 deletions filter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,31 @@ func TestConverter_Convert(t *testing.T) {
nil,
},
{
"in-array operator invalid value",
"simple $in",
nil,
`{"status": {"$in": [{"hacker": 1}, "OPEN"]}}`,
``,
nil,
fmt.Errorf("invalid value for $in operator (must array of primatives): [map[hacker:1] OPEN]"),
},
{
"in-array operator scalar value",
"simple $nin",
nil,
`{"status": {"$nin": ["NEW", "OPEN"]}}`,
`(NOT "status" = ANY($1))`,
[]any{[]any{"NEW", "OPEN"}},
nil,
},
{
"$in scalar value",
nil,
`{"status": {"$in": "text"}}`,
``,
nil,
fmt.Errorf("invalid value for $in operator (must array of primatives): text"),
},
{
"in-array operator with null value",
"$in with null value",
nil,
`{"status": {"$in": ["guest", null]}}`,
`("status" = ANY($1))`,
Expand Down
1 change: 1 addition & 0 deletions fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func FuzzConverter(f *testing.F) {
`{"b": 1, "c": 2, "a": 3}`,
`{"status": {"$in": ["NEW", "OPEN"]}}`,
`{"status": {"$in": [{"hacker": 1}, "OPEN"]}}`,
`{"status": {"$nin": ["NEW", "OPEN"]}}`,
`{"status": {"$in": "text"}}`,
`{"status": {"$in": ["guest", null]}}`,
`{"$or": [{"name": "John"}, {"name": "Doe"}]}`,
Expand Down
12 changes: 12 additions & 0 deletions integration/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ func TestIntegration_BasicOperators(t *testing.T) {
[]int{1, 2, 3, 4, 5, 6, 7, 8, 10},
nil,
},
{
"$in",
`{"level": {"$in": [20, 30, 40]}}`,
[]int{2, 3, 4},
nil,
},
{
"$nin",
`{"level": {"$nin": [20, 30, 40]}}`,
[]int{1, 5, 6, 7, 8, 9, 10},
nil,
},
}

for _, tt := range tests {
Expand Down
Loading