Added support for timestamps

This commit is contained in:
Patrick Urbanke
2023-10-04 21:29:06 +02:00
parent 679021d030
commit f708348fb1
7 changed files with 141 additions and 8 deletions
+14 -4
View File
@@ -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<Person>> children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
.children = std::vector<Person>()};
const auto lisa = Person{
.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.children = rfl::default_value // same as std::vector<Person>()
};
@@ -58,15 +61,24 @@ const auto maggie =
const auto homer =
Person{.first_name = "Homer",
.last_name = "Simpson",
.birthday = "1987-04-19",
.children = std::vector<Person>({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<Person>(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
+1
View File
@@ -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"
+3
View File
@@ -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_; }
+78
View File
@@ -0,0 +1,78 @@
#ifndef RFL_TIMESTAMP_HPP_
#define RFL_TIMESTAMP_HPP_
#include <ctime>
#include <iterator>
#include <stdexcept>
#include <string>
#include "internal/StringLiteral.hpp"
#include "rfl/Result.hpp"
namespace rfl {
/// For serializing and deserializing time stamps.
template <internal::StringLiteral _format>
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<Timestamp> 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<Timestamp> 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
+10 -4
View File
@@ -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<Person>> children;
};
const auto bart = Person{.first_name = "Bart",
.last_name = "Simpson",
.birthday = "1987-04-19",
.children = std::vector<Person>()};
const auto lisa = Person{
.first_name = "Lisa",
.last_name = "Simpson",
.birthday = "1987-04-19",
.children = rfl::default_value // same as std::vector<Person>()
};
@@ -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<Person>({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\":[]}]}");
}
+33
View File
@@ -0,0 +1,33 @@
#include <ctime>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#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\"}");
}
+2
View File
@@ -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;
}