Skip to content

Commit 50442ee

Browse files
authored
Merge pull request #609 from fantix/fix_json_path
fix JSONPathType bind processor for asyncpg
2 parents 3109577 + 515062f commit 50442ee

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

gino/dialects/asyncpg.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
CreateEnumType,
1010
DropEnumType,
1111
JSON,
12-
JSONB
12+
JSONB,
13+
json,
1314
)
1415
from sqlalchemy.dialects.postgresql.base import (
1516
ENUM,
@@ -333,6 +334,20 @@ def result_processor(self, dialect, coltype):
333334
return super().result_processor(dialect, coltype)
334335

335336

337+
class AsyncpgJSONPathType(json.JSONPathType):
338+
def bind_processor(self, dialect):
339+
super_proc = self.string_bind_processor(dialect)
340+
341+
def process(value):
342+
assert isinstance(value, util.collections_abc.Sequence)
343+
if super_proc:
344+
return [super_proc(util.text_type(elem)) for elem in value]
345+
else:
346+
return [util.text_type(elem) for elem in value]
347+
348+
return process
349+
350+
336351
# noinspection PyAbstractClass
337352
class AsyncpgDialect(PGDialect, base.AsyncDialectMixin):
338353
driver = 'asyncpg'
@@ -350,6 +365,7 @@ class AsyncpgDialect(PGDialect, base.AsyncDialectMixin):
350365
ENUM: AsyncEnum,
351366
sqltypes.Enum: AsyncEnum,
352367
sqltypes.NullType: GinoNullType,
368+
sqltypes.JSON.JSONPathType: AsyncpgJSONPathType,
353369
}
354370
)
355371

tests/test_json.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,20 @@ class PropsTest(db.Model):
233233
assert custom_profile2 == 123
234234
finally:
235235
await PropsTest.gino.drop()
236+
237+
238+
async def test_json_path(bind):
239+
from gino.dialects.asyncpg import JSONB
240+
241+
class PathTest(db.Model):
242+
__tablename__ = 'path_test_json_path'
243+
data = db.Column(JSONB())
244+
245+
await PathTest.gino.create()
246+
try:
247+
t1 = await PathTest.create(data=dict(a=dict(b='c')))
248+
t2 = await PathTest.query.where(
249+
PathTest.data[('a', 'b')].astext == 'c').gino.first()
250+
assert t1.data == t2.data
251+
finally:
252+
await PathTest.gino.drop()

0 commit comments

Comments
 (0)