Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add STRUCT binding for Python API #1527

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()