Skip to content

Commit

Permalink
Add STRUCT binding for Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
mewim committed May 10, 2023
1 parent 6f72c7a commit ba5460b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/python_api/src_cpp/py_query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ py::object PyQueryResult::convertValueToPyObject(const Value& value) {
}
return std::move(list);
}
case STRUCT: {
auto structTypeInfo = reinterpret_cast<StructTypeInfo*>(dataType.getExtraTypeInfo());
auto childrenNames = structTypeInfo->getChildrenNames();
py::dict dict;
auto& structVals = value.getListValReference();
for (auto i = 0u; i < structVals.size(); ++i) {
auto key = py::str(childrenNames[i]);
auto val = convertValueToPyObject(*structVals[i]);
dict[key] = val;
}
return dict;
}
case NODE: {
auto nodeVal = value.getValue<NodeVal>();
auto dict = PyQueryResult::getPyDictFromProperties(nodeVal.getProperties());
Expand Down
1 change: 1 addition & 0 deletions tools/python_api/src_py/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ class Type(Enum):
NODE = "NODE"
REL = "REL"
NODE_ID = "NODE_ID"
STRUCT = "STRUCT"
18 changes: 18 additions & 0 deletions tools/python_api/test/test_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,21 @@ def test_rel(establish_connection):
assert (r['_dst'] == o['_id'])
assert not result.has_next()
result.close()


def test_struct(establish_connection):
conn, db = establish_connection
result = conn.execute(
'MATCH (m:movies) WHERE m.name="Roma" RETURN m.description')
assert result.has_next()
n = result.get_next()
assert (len(n) == 1)
description = n[0]
print(description)
assert (description['RATING'] == 1223)
assert (description['VIEWS'] == 10003)
assert (description['RELEASE'] ==
datetime.datetime(2011, 2, 11, 16, 44, 22))
assert (description['FILM'] == datetime.date(2013, 2, 22))
assert not result.has_next()
result.close()

0 comments on commit ba5460b

Please sign in to comment.