Skip to content

Commit

Permalink
Support get reference of property in Vertex/Edge
Browse files Browse the repository at this point in the history
  • Loading branch information
lixueclaire committed May 16, 2023
1 parent 8ebf14c commit 7f338f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
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

0 comments on commit 7f338f2

Please sign in to comment.