Skip to content

Commit 617dc03

Browse files
committed
refactor type_in
1 parent 9877c83 commit 617dc03

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

django_mongodb/fields/json_field.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,19 @@ def key_transform(self, compiler, connection):
107107
return result
108108

109109

110+
def _expr_type_in(mql, types, negation=False):
111+
result = {"$in": [{"$type": mql}, types]}
112+
if negation:
113+
result = {"$not": result}
114+
return result
115+
116+
110117
def key_transform_in(self, compiler, connection):
111118
lhs_mql = process_lhs(self, compiler, connection)
112119
value = process_rhs(self, compiler, connection)
113120
expr = connection.mongo_operators[self.lookup_name](lhs_mql, value)
114-
return {"$and": [expr, {"$not": {"$in": [{"$type": lhs_mql}, ["missing", "null"]]}}]}
121+
type_in = _expr_type_in(lhs_mql, ["missing", "null"], True)
122+
return {"$and": [expr, type_in]}
115123

116124

117125
def key_transform_isnull(self, compiler, connection):
@@ -123,8 +131,8 @@ def key_transform_isnull(self, compiler, connection):
123131
lhs_mql = process_lhs(self, compiler, connection)
124132
rhs_mql = process_rhs(self, compiler, connection)
125133
root_column = _key_transform_root(self, compiler, connection)
126-
127-
result = {"$or": [{"$in": [{"$type": lhs_mql}, ["missing"]]}, {"$eq": [root_column, None]}]}
134+
type_in = _expr_type_in(lhs_mql, ["missing"])
135+
result = {"$or": [type_in, {"$eq": [root_column, None]}]}
128136
if not rhs_mql:
129137
result = {"$not": result}
130138
return result
@@ -133,7 +141,8 @@ def key_transform_isnull(self, compiler, connection):
133141
def key_transform_numeric_lookup_mixin(self, compiler, connection):
134142
expr = builtin_lookup(self, compiler, connection)
135143
lhs = process_lhs(self, compiler, connection)
136-
return {"$and": [expr, {"$not": {"$in": [{"$type": lhs}, ["missing", "null"]]}}]}
144+
type_in = _expr_type_in(lhs, ["missing", "null"], True)
145+
return {"$and": [expr, type_in]}
137146

138147

139148
def register_fields():

0 commit comments

Comments
 (0)