Skip to content

Commit 1ffe04f

Browse files
Implement $not
1 parent a827073 commit 1ffe04f

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

filter/converter.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
117117
} else {
118118
conditions = append(conditions, strings.Join(inner, " "+op+" "))
119119
}
120+
case "$not":
121+
vv, ok := value.(map[string]any)
122+
if !ok {
123+
return "", nil, fmt.Errorf("invalid value for $not operator (must be object): %v", value)
124+
}
125+
innerConditions, innerValues, err := c.convertFilter(vv, paramIndex)
126+
if err != nil {
127+
return "", nil, err
128+
}
129+
paramIndex += len(innerValues)
130+
// Just putting a NOT around the condition is not enough, a non existing jsonb field will for example
131+
// make the whole inner condition NULL. And NOT NULL is still a falsy value, so we need to check for NULL explicitly.
132+
conditions = append(conditions, fmt.Sprintf("(NOT %s OR %s IS NULL)", innerConditions, innerConditions))
133+
values = append(values, innerValues...)
120134
default:
121135
if !isValidPostgresIdentifier(key) {
122136
return "", nil, fmt.Errorf("invalid column name: %s", key)
@@ -140,6 +154,8 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
140154
return "", nil, fmt.Errorf("$or as scalar operator not supported")
141155
case "$and":
142156
return "", nil, fmt.Errorf("$and as scalar operator not supported")
157+
case "$not":
158+
return "", nil, fmt.Errorf("$not as scalar operator not supported")
143159
case "$in":
144160
if !isScalarSlice(v[operator]) {
145161
return "", nil, fmt.Errorf("invalid value for $in operator (must array of primatives): %v", v[operator])

filter/converter_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ func TestConverter_Convert(t *testing.T) {
216216
nil,
217217
fmt.Errorf("invalid column name: \"bla = 1 --"),
218218
},
219+
{
220+
"$not operator",
221+
nil,
222+
`{"$not": {"name": "John"}}`,
223+
`(NOT ("name" = $1) OR ("name" = $1) IS NULL)`,
224+
[]any{"John"},
225+
nil,
226+
},
227+
{
228+
"$not in the wrong place",
229+
nil,
230+
`{"name": {"$not": {"$eq": "John"}}}`,
231+
``,
232+
nil,
233+
fmt.Errorf("$not as scalar operator not supported"),
234+
},
219235
}
220236

221237
for _, tt := range tests {

fuzz/fuzz_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func FuzzConverter(f *testing.F) {
3333
`{"name": {}}`,
3434
`{"$or": []}`,
3535
`{"status": {"$in": []}}`,
36+
`{"$not": {"name": "John"}}`,
37+
`{"name": {"$not": {"$eq": "John"}}}`,
3638
}
3739
for _, tc := range tcs {
3840
f.Add(tc)

integration/postgres_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ func TestIntegration_BasicOperators(t *testing.T) {
303303
[]int{7, 8, 9, 10},
304304
nil,
305305
},
306+
{
307+
`jsonb equal to null`,
308+
`{"$not": {"pet": "cat"}}`,
309+
[]int{1, 3, 5, 7, 9, 10},
310+
nil,
311+
},
306312
}
307313

308314
for _, tt := range tests {

0 commit comments

Comments
 (0)