From f708348fb13644c2c33deccd3a3c1c6e9cde1e6d Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Wed, 4 Oct 2023 21:29:06 +0200 Subject: [PATCH] Added support for timestamps --- README.md | 18 ++++++-- include/rfl.hpp | 1 + include/rfl/Literal.hpp | 3 ++ include/rfl/Timestamp.hpp | 78 +++++++++++++++++++++++++++++++++++ tests/test_readme_example.hpp | 14 +++++-- tests/test_timestamp.hpp | 33 +++++++++++++++ tests/tests.cpp | 2 + 7 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 include/rfl/Timestamp.hpp create mode 100644 tests/test_timestamp.hpp diff --git a/README.md b/README.md index e4ed1310..5b845f08 100644 --- a/README.md +++ b/README.md @@ -37,16 +37,19 @@ Design principles for reflect-cpp include: struct Person { rfl::Field<"firstName", std::string> first_name; rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; rfl::Field<"children", std::vector> children; }; const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", + .birthday = "1987-04-19", .children = std::vector()}; const auto lisa = Person{ .first_name = "Lisa", .last_name = "Simpson", + .birthday = "1987-04-19", .children = rfl::default_value // same as std::vector() }; @@ -58,15 +61,24 @@ const auto maggie = const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", + .birthday = "1987-04-19", .children = std::vector({bart, lisa, maggie})}; // We can now transform this into a JSON string. const std::string json_string = rfl::json::write(homer); -// {"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]} std::cout << json_string << std::endl; +``` -// And we can parse the string to recreate the struct. +This results in the following JSON string: + +```json +{"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","children":[]}]} +``` + +We can also create structs from the string: + +``` auto homer2 = rfl::json::read(json_string).value(); // Fields can be accessed like this: @@ -78,8 +90,6 @@ homer2.first_name = "Marge"; std::cout << "Hello, my name is " << homer2.first_name() << " " << homer2.last_name() << "." << std::endl; - - ``` ## Support for containers diff --git a/include/rfl.hpp b/include/rfl.hpp index 684e57b4..e38806c0 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -8,6 +8,7 @@ #include "rfl/NamedTuple.hpp" #include "rfl/Ref.hpp" #include "rfl/TaggedUnion.hpp" +#include "rfl/Timestamp.hpp" #include "rfl/always_false.hpp" #include "rfl/as.hpp" #include "rfl/default.hpp" diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index ef6c67d0..acbd73f7 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -163,6 +163,9 @@ class Literal { return name() != _str; } + /// Alias for .name(). + std::string str() const { return name(); } + /// Returns the value actually contained in the Literal. inline ValueType value() const { return value_; } diff --git a/include/rfl/Timestamp.hpp b/include/rfl/Timestamp.hpp new file mode 100644 index 00000000..fcb179c1 --- /dev/null +++ b/include/rfl/Timestamp.hpp @@ -0,0 +1,78 @@ +#ifndef RFL_TIMESTAMP_HPP_ +#define RFL_TIMESTAMP_HPP_ + +#include +#include +#include +#include + +#include "internal/StringLiteral.hpp" +#include "rfl/Result.hpp" + +namespace rfl { + +/// For serializing and deserializing time stamps. +template +class Timestamp { + constexpr static const internal::StringLiteral format_ = _format; + + public: + using Format = rfl::Literal<_format>; + + using ReflectionType = std::string; + + Timestamp(const char* _str) { + const auto r = strptime(_str, format_.value_, &tm_); + if (r == NULL) { + throw std::runtime_error("String '" + std::string(_str) + + "' did not match format '" + + Format().str() + "'."); + } + } + + Timestamp(const std::string& _str) : Timestamp(_str.c_str()) {} + + Timestamp(const std::tm& _tm) : tm_(_tm) {} + + ~Timestamp() = default; + + /// Returns a result containing the timestamp when successful or an Error + /// otherwise. + static Result from_string(const char* _str) noexcept { + try { + return Timestamp(_str); + } catch (std::exception& e) { + return Error(e.what()); + } + } + + /// Returns a result containing the timestamp when successful or an Error + /// otherwise. + static Result from_string(const std::string& _str) { + return from_string(_str.c_str()); + } + + /// Necessary for the serialization to work. + ReflectionType reflection() const { + char outstr[200]; + strftime(outstr, 200, format_.value_, &tm_); + return std::string(outstr); + } + + /// Expresses the underlying timestamp as a string. + std::string str() const { return reflection; } + + /// Trivial accessor to the underlying time stamp. + std::tm& tm() { return tm_; } + + /// Trivial (const) accessor to the underlying time stamp. + const std::tm& tm() const { return tm_; } + + private: + /// The underlying time stamp. + std::tm tm_; +}; + +} // namespace rfl + +#endif diff --git a/tests/test_readme_example.hpp b/tests/test_readme_example.hpp index 02f9db7b..345e15bd 100644 --- a/tests/test_readme_example.hpp +++ b/tests/test_readme_example.hpp @@ -18,16 +18,19 @@ void test_readme_example() { struct Person { rfl::Field<"firstName", std::string> first_name; rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; rfl::Field<"children", std::vector> children; }; const auto bart = Person{.first_name = "Bart", .last_name = "Simpson", + .birthday = "1987-04-19", .children = std::vector()}; const auto lisa = Person{ .first_name = "Lisa", .last_name = "Simpson", + .birthday = "1987-04-19", .children = rfl::default_value // same as std::vector() }; @@ -39,12 +42,15 @@ void test_readme_example() { const auto homer = Person{.first_name = "Homer", .last_name = "Simpson", + .birthday = "1987-04-19", .children = std::vector({bart, lisa, maggie})}; write_and_read( homer, - "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{" - "\"firstName\":\"Bart\",\"lastName\":\"Simpson\",\"children\":[]},{" - "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\",\"children\":[]},{" - "\"firstName\":\"Maggie\",\"lastName\":\"Simpson\",\"children\":[]}]}"); + "{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"birthday\":\"1987-" + "04-19\",\"children\":[{\"firstName\":\"Bart\",\"lastName\":" + "\"Simpson\",\"birthday\":\"1987-04-19\",\"children\":[]},{" + "\"firstName\":\"Lisa\",\"lastName\":\"Simpson\",\"birthday\":\"1987-" + "04-19\",\"children\":[]},{\"firstName\":\"Maggie\",\"lastName\":" + "\"Simpson\",\"birthday\":\"1987-04-19\",\"children\":[]}]}"); } diff --git a/tests/test_timestamp.hpp b/tests/test_timestamp.hpp new file mode 100644 index 00000000..b5398f12 --- /dev/null +++ b/tests/test_timestamp.hpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_timestamp() { + std::cout << "test_timestamp" << std::endl; + + using TS = rfl::Timestamp<"%Y-%m-%d">; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"birthday", TS> birthday; + }; + + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read(bart, + "{\"firstName\":\"Bart\",\"lastName\":\"Simpson\"," + "\"birthday\":\"1987-04-19\"}"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index a0ec62ba..f7e1e8ce 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -9,6 +9,7 @@ #include "test_readme_example.hpp" #include "test_ref.hpp" #include "test_tagged_union.hpp" +#include "test_timestamp.hpp" #include "test_variant.hpp" int main() { @@ -24,6 +25,7 @@ int main() { test_custom_class2(); test_custom_class3(); test_array(); + test_timestamp(); return 0; }