Skip to content

Commit 97ec3b1

Browse files
Fix serialisation of dict (#422)
* Serialisation was not working for queries which embedded dictionaries * Add document serialisation * Satisfy the linting gods * Remove print statement * Fix negation of isinstance
1 parent 0251f80 commit 97ec3b1

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

terminusdb_client/schema/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def transform_enum_dict(d):
379379
if not key.startswith("__") and not value:
380380
value = str(key)
381381
# remove this value from the undocumented member names list
382-
if type(d._member_names) == list:
382+
if isinstance(d._member_names, list):
383383
d._member_names.remove(key)
384384
else:
385385
d._member_names.pop(key)
@@ -410,7 +410,7 @@ def __new__(
410410
# definitions here, we'll have to reach into internals
411411
# like this to keep things working well.
412412
# There is probably a better way to do this.
413-
if type(classdict._member_names) == list:
413+
if isinstance(classdict._member_names, list):
414414
classdict._member_names.remove("_schema")
415415
else:
416416
classdict._member_names.pop("_schema")

terminusdb_client/scripts/scripts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def __init__(self, name, parent, script=None):
160160
else:
161161
self.parent = parent
162162
if script is None:
163-
if type(parent) == str or len(parent) == 1:
163+
if isinstance(parent, str) or len(parent) == 1:
164164
self.script = f"class {name}({parent}):\n"
165165
elif len(parent) > 1:
166166
self.script = f"class {name}({', '.join(parent)}):\n"
@@ -266,7 +266,7 @@ def add_docstring(self, obj_dict):
266266
if obj.parent is None:
267267
print_script += obj.script
268268
printed.append(obj.name)
269-
elif type(obj.parent) == str and obj.parent in printed:
269+
elif isinstance(obj.parent, str) and obj.parent in printed:
270270
print_script += obj.script
271271
printed.append(obj.name)
272272
else:

terminusdb_client/tests/test_woqlQuery.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pprint
22
import datetime as dt
33

4-
from terminusdb_client.woqlquery.woql_query import WOQLQuery
4+
from terminusdb_client.woqlquery.woql_query import WOQLQuery, Doc, Var
55

66
from .ans_cardinality import * # noqa
77
from .ans_doctype import * # noqa
@@ -707,3 +707,28 @@ def test_dot(self):
707707
},
708708
"value": {"@type": "Value", "node": "value"},
709709
}
710+
711+
def test_doc(self):
712+
result = WOQLQuery().insert_document(
713+
Doc({"@type": "Car", "wheels": 4})
714+
)
715+
assert result.to_dict() == {'@type': 'InsertDocument', 'document': {'@type': 'Value', 'dictionary': {'@type': 'DictionaryTemplate', 'data': [{'@type': 'FieldValuePair', 'field': '@type', 'value': {'@type': 'Value', 'data': {'@type': 'xsd:string', '@value': 'Car'}}}, {'@type': 'FieldValuePair', 'field': 'wheels', 'value': {'@type': 'Value', 'data': {'@type': 'xsd:integer', '@value': 4}}}]}}}
716+
717+
def test_var(self):
718+
result = WOQLQuery().insert_document(
719+
Doc({"@type": "Car", "wheels": Var("v")})
720+
)
721+
722+
assert result.to_dict() == {
723+
'@type': 'InsertDocument',
724+
'document': {'@type': 'Value',
725+
'dictionary': {'@type': 'DictionaryTemplate',
726+
'data': [{'@type': 'FieldValuePair',
727+
'field': '@type',
728+
'value': {'@type': 'Value',
729+
'data': {'@type': 'xsd:string',
730+
'@value': 'Car'}}},
731+
{'@type': 'FieldValuePair',
732+
'field': 'wheels',
733+
'value': {'@type': 'Value',
734+
'variable': 'v'}}]}}}

terminusdb_client/woqlquery/woql_core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def _copy_dict(orig, rollup=None):
156156
query = _copy_dict(part, rollup)
157157
if query:
158158
nuj[key] = query
159+
elif hasattr(part, 'to_dict'):
160+
nuj[key] = part.to_dict()
159161
else:
160162
nuj[key] = part
161163
return nuj

terminusdb_client/woqlquery/woql_query.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def __init__(self, name):
4949
def __str__(self):
5050
return self.name
5151

52+
def to_dict(self):
53+
return {"@type": "Value",
54+
"variable": self.name}
55+
5256

5357
class Doc:
5458
def __init__(self, dictionary):
@@ -58,6 +62,9 @@ def __init__(self, dictionary):
5862
def __str__(self):
5963
return str(self.dictionary)
6064

65+
def to_dict(self):
66+
return self.encoded
67+
6168
def _convert(self, obj):
6269
if type(obj) is str:
6370
return {"@type": "Value", "data": {"@type": "xsd:string", "@value": obj}}
@@ -363,9 +370,9 @@ def _clean_subject(self, obj):
363370
def _clean_predicate(self, predicate):
364371
"""Transforms whatever is passed in as the predicate (id or variable) into the appropriate json-ld form"""
365372
pred = False
366-
if type(predicate) is dict:
373+
if isinstance(predicate, dict):
367374
return predicate
368-
if type(predicate) != str:
375+
if not isinstance(predicate, str):
369376
raise ValueError("Predicate must be a URI string")
370377
return str(predicate)
371378
if ":" in predicate:
@@ -716,7 +723,7 @@ def using(self, collection, subq=None):
716723
if self._cursor.get("@type"):
717724
self._wrap_cursor_with_and()
718725
self._cursor["@type"] = "Using"
719-
if not collection or type(collection) != str:
726+
if not collection or not isinstance(collection, str):
720727
raise ValueError(
721728
"The first parameter to using must be a Collection ID (string)"
722729
)
@@ -888,7 +895,7 @@ def woql_from(self, graph, query=None):
888895
if self._cursor.get("@type"):
889896
self._wrap_cursor_with_and()
890897
self._cursor["@type"] = "From"
891-
if not graph or type(graph) != str:
898+
if not graph or not isinstance(graph, str):
892899
raise ValueError(
893900
"The first parameter to from must be a Graph Filter Expression (string)"
894901
)
@@ -914,7 +921,7 @@ def into(self, graph_descriptor, query):
914921
if self._cursor.get("@type"):
915922
self._wrap_cursor_with_and()
916923
self._cursor["@type"] = "Into"
917-
if not graph_descriptor or type(graph_descriptor) != str:
924+
if not graph_descriptor or not isinstance(graph_descriptor, str):
918925
raise ValueError(
919926
"The first parameter to from must be a Graph Filter Expression (string)"
920927
)
@@ -1257,7 +1264,7 @@ def substr(self, string, length, substring, before=0, after=0):
12571264
if not substring:
12581265
substring = length
12591266
length = len(substring) + before
1260-
if not string or not substring or type(substring) != str:
1267+
if not string or not substring or not isinstance(substring, str):
12611268
raise ValueError(
12621269
"Substr - the first and last parameters must be strings representing the full and substring variables / literals"
12631270
)

0 commit comments

Comments
 (0)