Add implementation of << operator for mgp::Value (#1127)

This commit is contained in:
imilinovic 2023-08-01 19:30:23 +02:00 committed by GitHub
parent 50a1d1abb3
commit 2877c343e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -1203,6 +1203,8 @@ class Value {
bool operator<(const Value &other) const;
friend std::ostream &operator<<(std::ostream &os, const mgp::Value &value);
private:
mgp_value *ptr_;
};
@ -3491,6 +3493,54 @@ inline bool Value::operator<(const Value &other) const {
}
}
inline std::ostream &operator<<(std::ostream &os, const mgp::Value &value) {
switch (value.Type()) {
case mgp::Type::Null:
return os << "null";
case mgp::Type::Any:
return os << "any";
case mgp::Type::Bool:
return os << (value.ValueBool() ? "true" : "false");
case mgp::Type::Int:
return os << std::to_string(value.ValueInt());
case mgp::Type::Double:
return os << std::to_string(value.ValueDouble());
case mgp::Type::String:
return os << std::string(value.ValueString());
case mgp::Type::List:
throw mgp::ValueException("Printing mgp::List type currently not supported.");
case mgp::Type::Map:
throw mgp::ValueException("Printing mgp::Map type currently not supported.");
case mgp::Type::Node:
return os << "Node[" + std::to_string(value.ValueNode().Id().AsInt()) + "]";
case mgp::Type::Relationship:
return os << "Relationship[" + std::to_string(value.ValueRelationship().Id().AsInt()) + "]";
case mgp::Type::Path:
throw mgp::ValueException("Printing mgp::Path type currently not supported.");
case mgp::Type::Date: {
const auto date{value.ValueDate()};
return os << std::to_string(date.Year()) + "-" + std::to_string(date.Month()) + "-" + std::to_string(date.Day());
}
case mgp::Type::LocalTime: {
const auto localTime{value.ValueLocalTime()};
return os << std::to_string(localTime.Hour()) + ":" + std::to_string(localTime.Minute()) + ":" +
std::to_string(localTime.Second()) + "," + std::to_string(localTime.Millisecond()) +
std::to_string(localTime.Microsecond());
}
case mgp::Type::LocalDateTime: {
const auto localDateTime = value.ValueLocalDateTime();
return os << std::to_string(localDateTime.Year()) + "-" + std::to_string(localDateTime.Month()) + "-" +
std::to_string(localDateTime.Day()) + "T" + std::to_string(localDateTime.Hour()) + ":" +
std::to_string(localDateTime.Minute()) + ":" + std::to_string(localDateTime.Second()) + "," +
std::to_string(localDateTime.Millisecond()) + std::to_string(localDateTime.Microsecond());
}
case mgp::Type::Duration:
return os << std::to_string(value.ValueDuration().Microseconds()) + "ms";
default:
throw mgp::ValueException("Unknown value type");
}
}
inline std::ostream &operator<<(std::ostream &os, const mgp::Type &type) {
switch (type) {
case mgp::Type::Null:

View File

@ -560,3 +560,28 @@ TYPED_TEST(CppApiTestFixture, TestMapErase) {
map.Erase("2");
ASSERT_EQ(map.Size(), 0);
}
TYPED_TEST(CppApiTestFixture, TestValuePrint) {
std::string string_1{"abc"};
int64_t int_1{4};
mgp::Date date_1{"2020-12-12"};
mgp::Value string_value{string_1};
mgp::Value int_value{int_1};
mgp::Value date_value{date_1};
std::ostringstream oss_str;
oss_str << string_value;
std::string str_test = oss_str.str();
ASSERT_EQ(string_1, str_test);
std::ostringstream oss_int;
oss_int << int_value;
std::string int_test = oss_int.str();
ASSERT_EQ("4", int_test);
std::ostringstream oss_date;
oss_date << date_value;
std::string date_test = oss_date.str();
ASSERT_EQ("2020-12-12", date_test);
}