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

[C++] [Improvement] Support to get reference of property in Vertex/Edge #156

Merged
merged 1 commit into from
May 17, 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
10 changes: 4 additions & 6 deletions cpp/include/gar/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ class Vertex {
*/
template <typename T>
inline Result<T> property(const std::string& property) noexcept {
T ret;
if (properties_.find(property) == properties_.end()) {
return Status::KeyError("The property is not exist.");
}
try {
ret = std::any_cast<T>(properties_[property]);
T ret = std::any_cast<T>(properties_[property]);
return ret;
} catch (const std::bad_any_cast& e) {
return Status::TypeError("The property type is not match.");
}
return ret;
}

private:
Expand Down Expand Up @@ -120,16 +119,15 @@ class Edge {
*/
template <typename T>
inline Result<T> property(const std::string& property) noexcept {
T ret;
if (properties_.find(property) == properties_.end()) {
return Status::KeyError("The property is not exist.");
}
try {
ret = std::any_cast<T>(properties_[property]);
T ret = std::any_cast<T>(properties_[property]);
return ret;
} catch (const std::bad_any_cast& e) {
return Status::TypeError("The property type is not match.");
}
return ret;
}

private:
Expand Down
19 changes: 16 additions & 3 deletions cpp/test/test_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ TEST_CASE("test_vertices_collection") {
<< ", id=" << vertex.property<int64_t>("id").value()
<< ", firstName="
<< vertex.property<std::string>("firstName").value() << std::endl;
// access data reference through vertex
REQUIRE(vertex.property<int64_t>("id").value() ==
vertex.property<const int64_t&>("id").value());
REQUIRE(vertex.property<std::string>("firstName").value() ==
vertex.property<const std::string&>("firstName").value());
REQUIRE(vertex.property<const std::string&>("id").has_error());
count++;
}
auto it_last = vertices.begin() + (count - 1);
Expand Down Expand Up @@ -88,11 +94,18 @@ TEST_CASE("test_edges_collection", "[Slow]") {
size_t count = 0;
for (auto it = edges.begin(); it != end; ++it) {
// access data through iterator directly
std::cout << "src=" << it.source() << ", dst=" << it.destination()
<< std::endl;
std::cout << "src=" << it.source() << ", dst=" << it.destination() << " ";
// access data through edge
auto edge = *it;
std::cout << "src=" << edge.source() << ", dst=" << edge.destination()
REQUIRE(edge.source() == it.source());
REQUIRE(edge.destination() == it.destination());
std::cout << "creationDate="
<< edge.property<std::string>("creationDate").value()
<< std::endl;
// access data reference through edge
REQUIRE(edge.property<std::string>("creationDate").value() ==
edge.property<const std::string&>("creationDate").value());
REQUIRE(edge.property<const int64_t&>("creationDate").has_error());
count++;
}
std::cout << "edge_count=" << count << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion testing