Skip to content

Commit 832fc99

Browse files
1 parent a7572af commit 832fc99

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

filter/converter.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
9393
value := filter[key]
9494

9595
switch key {
96-
case "$or", "$and":
96+
case "$or", "$and", "$nor":
9797
opConditions, ok := anyToSliceMapAny(value)
9898
if !ok {
9999
return "", nil, fmt.Errorf("invalid value for $or operator (must be array of objects): %v", value)
@@ -112,14 +112,18 @@ func (c *Converter) convertFilter(filter map[string]any, paramIndex int) (string
112112
inner = append(inner, innerConditions)
113113
values = append(values, innerValues...)
114114
}
115-
op := "AND"
116-
if key == "$or" {
117-
op = "OR"
118-
}
119-
if len(inner) > 1 {
120-
conditions = append(conditions, "("+strings.Join(inner, " "+op+" ")+")")
115+
if key == "$nor" {
116+
conditions = append(conditions, "NOT ("+strings.Join(inner, " OR ")+")")
121117
} else {
122-
conditions = append(conditions, strings.Join(inner, " "+op+" "))
118+
op := "AND"
119+
if key == "$or" {
120+
op = "OR"
121+
}
122+
if len(inner) > 1 {
123+
conditions = append(conditions, "("+strings.Join(inner, " "+op+" ")+")")
124+
} else {
125+
conditions = append(conditions, strings.Join(inner, " "+op+" "))
126+
}
123127
}
124128
default:
125129
if !isValidPostgresIdentifier(key) {

filter/converter_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ func TestConverter_Convert(t *testing.T) {
137137
nil,
138138
fmt.Errorf("$or as scalar operator not supported"),
139139
},
140+
{
141+
"$nor operator basic",
142+
nil,
143+
`{"$nor": [{"name": "John"}, {"name": "Doe"}]}`,
144+
`NOT (("name" = $1) OR ("name" = $2))`,
145+
[]any{"John", "Doe"},
146+
nil,
147+
},
140148
{
141149
"and operator basic",
142150
nil,

fuzz/fuzz_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func FuzzConverter(f *testing.F) {
2727
`{"$or": [{"org": "poki", "admin": true}, {"age": {"$gte": 18}}]}`,
2828
`{"$or": [{"$or": [{"name": "John"}, {"name": "Doe"}]}, {"name": "Jane"}]}`,
2929
`{"foo": { "$or": [ "bar", "baz" ] }}`,
30+
`{"$nor": [{"name": "John"}, {"name": "Doe"}]}`,
3031
`{"$and": [{"name": "John"}, {"version": 3}]}`,
3132
`{"$and": [{"name": "John", "version": 3}]}`,
3233
`{"name": {"$regex": "John"}}`,

integration/postgres_test.go

+6
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+
"$nor",
308+
`{"$nor": [{"name": "Alice"}, {"name": "Bob"}]}`,
309+
[]int{3, 4, 5, 6, 7, 8, 9, 10},
310+
nil,
311+
},
306312
}
307313

308314
for _, tt := range tests {

0 commit comments

Comments
 (0)