mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-04 17:26:17 +08:00
Merge branch 'f/field_name_retrieval'
This commit is contained in:
@@ -4,12 +4,12 @@
|
||||
[](https://GitHub.com/Naereen/StrapDown.js/graphs/commit-activity)
|
||||
[](https://shields.io/)
|
||||
[](https://shields.io/)
|
||||
[](https://shields.io/)
|
||||
[](https://shields.io/)
|
||||
[](https://shields.io/)
|
||||
|
||||

|
||||
|
||||
**reflect-cpp** is a C++-20 library for **fast serialization, deserialization and validation** using compile-time reflection, similar to [pydantic](https://github.com/pydantic/pydantic) in Python, [serde](https://github.com/serde-rs) in Rust, [encoding](https://github.com/golang/go/tree/master/src/encoding) in Go or [aeson](https://github.com/haskell/aeson/tree/master) in Haskell.
|
||||
**reflect-cpp** is a C++-20 library for **fast serialization, deserialization and validation** using reflection, similar to [pydantic](https://github.com/pydantic/pydantic) in Python, [serde](https://github.com/serde-rs) in Rust, [encoding](https://github.com/golang/go/tree/master/src/encoding) in Go or [aeson](https://github.com/haskell/aeson/tree/master) in Haskell.
|
||||
|
||||
As the aforementioned libraries are among the most widely used in the respective languages, reflect-cpp fills an important gap in C++ development. It reduces boilerplate code and increases code safety.
|
||||
|
||||
@@ -21,7 +21,7 @@ Design principles for reflect-cpp include:
|
||||
- Simple installation: If no JSON support is required, reflect-cpp is header-only. For JSON support, only a single source file needs to be compiled.
|
||||
- Simple extendability to other serialization formats
|
||||
- Simple extendability to custom classes
|
||||
- No macros
|
||||
- Standard C++ only, no compiler-specific macros
|
||||
|
||||
## Why do we need this?
|
||||
|
||||
@@ -29,7 +29,7 @@ Suppose your C++ program has complex data structures it needs to save and load.
|
||||
|
||||
reflect-cpp is not just a reflection library, it is for **serialization, deserialization and validation** through reflection.
|
||||
|
||||
That means that you can encode your assumptions about the input data in the type system and have them validated upfront. This is why the library also includes algebraic data types like tagged unions and numerous validation routines. Having your assumptions encoded in the type system is the most reliable way of ensuring they are fullfilled. It also makes it a lot easier for anyone reading your code. If your assumptions are violated, the user of your software gets a very clear error message.
|
||||
That means that you can encode your requirements about the input data in the type system and have them validated upfront. This is why the library also includes algebraic data types like tagged unions and numerous validation routines. Having your requirements encoded in the type system is the most reliable way of ensuring they are met. It also makes it a lot easier for anyone reading your code. If your requirements are not met, the user of your software gets a very clear error message.
|
||||
|
||||
This increases user experience and developer experience, it makes your code safer (fewer bugs) and more secure (less prone to malicious attacks).
|
||||
|
||||
@@ -38,7 +38,35 @@ For a more in-depth theoretical discussions of these topics, the following books
|
||||
- *Category Theory for Programmers* by Bartosz Milewski (https://github.com/hmemcpy/milewski-ctfp-pdf/releases)
|
||||
- *Domain Modeling Made Functional* by Scott Wlaschin
|
||||
|
||||
## Basic Example
|
||||
## Simple Example
|
||||
|
||||
```cpp
|
||||
#include <rfl/json.hpp>
|
||||
#include <rfl.hpp>
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.age = 45};
|
||||
|
||||
// We can now write into and read from a JSON string.
|
||||
const std::string json_string = rfl::json::write(homer);
|
||||
auto homer2 = rfl::json::read<Person>(json_string).value();
|
||||
```
|
||||
|
||||
The resulting JSON string looks like this:
|
||||
|
||||
```json
|
||||
{"first_name":"Homer","last_name":"Simpson","age":45}
|
||||
```
|
||||
|
||||
## More Comprehensive Example
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
@@ -49,49 +77,37 @@ For a more in-depth theoretical discussions of these topics, the following books
|
||||
// be validated automatically.
|
||||
using Age = rfl::Validator<int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
// "firstName", "lastName" and "children" are the field names
|
||||
// as they will appear in the JSON. The C++ standard is
|
||||
// snake case, the JSON standard is camel case, so the names
|
||||
// will not always be identical.
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::string town = "Springfield";
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com",
|
||||
.children = std::vector<Person>()};
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{
|
||||
.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com",
|
||||
.children = rfl::default_value // same as std::vector<Person>()
|
||||
};
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
// Returns a deep copy of the original object,
|
||||
// replacing first_name, email and age.
|
||||
const auto maggie =
|
||||
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")),
|
||||
rfl::make_field<"email">(std::string("maggie@simpson.com")),
|
||||
rfl::make_field<"age">(0));
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
Person{.first_name = "Homer",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.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);
|
||||
@@ -104,7 +120,7 @@ rfl::json::write(homer, std::cout) << std::endl;
|
||||
This results in the following JSON string:
|
||||
|
||||
```json
|
||||
{"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]}
|
||||
{"firstName":"Homer","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"firstName":"Bart","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"firstName":"Lisa","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"firstName":"Maggie","lastName":"Simpson","town":"Springfield","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]}
|
||||
```
|
||||
|
||||
We can also create structs from the string:
|
||||
@@ -129,7 +145,7 @@ reflect-cpp returns clear and comprehensive error messages:
|
||||
|
||||
```cpp
|
||||
const std::string faulty_json_string =
|
||||
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987","age":145,"email":"homer(at)simpson.com"})";
|
||||
R"({"firstName":"Homer","lastName":12345,"town":"Springfield","birthday":"04/19/1987","age":145,"email":"homer(at)simpson.com"})";
|
||||
const auto result = rfl::json::read<Person>(faulty_json_string);
|
||||
```
|
||||
|
||||
@@ -144,81 +160,24 @@ Found 5 errors:
|
||||
5) Field named 'children' not found.
|
||||
```
|
||||
|
||||
## Anonymous fields
|
||||
|
||||
`rfl::Field` is only necessary if you want to serialize field names. If you don't (possibly because you are using a binary format), you do not need to annotate your fields:
|
||||
|
||||
```cpp
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 10,
|
||||
.email = "bart@simpson.com"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 8,
|
||||
.email = "lisa@simpson.com"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 0,
|
||||
.email = "maggie@simpson.com"};
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.birthday = "1987-04-19",
|
||||
.age = 45,
|
||||
.email = "homer@simpson.com",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
```
|
||||
|
||||
This results in the following JSON string:
|
||||
|
||||
```json
|
||||
["Homer","Simpson","1987-04-19",45,"homer@simpson.com",[["Bart","Simpson","1987-04-19",10,"bart@simpson.com",[]],["Lisa","Simpson","1987-04-19",8,"lisa@simpson.com",[]],["Maggie","Simpson","1987-04-19",0,"maggie@simpson.com",[]]]]
|
||||
```
|
||||
|
||||
## Algebraic data types
|
||||
|
||||
reflect-cpp supports Pydantic-style tagged unions, which allow you to form algebraic data types:
|
||||
|
||||
```cpp
|
||||
// All alternatives must contain a field named "shape". The type
|
||||
// of the field must be an rfl::Literal.
|
||||
struct Circle {
|
||||
rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value;
|
||||
rfl::Field<"radius", double> radius;
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"shape", rfl::Literal<"Rectangle">> shape =
|
||||
rfl::default_value;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value;
|
||||
rfl::Field<"width", double> width;
|
||||
double width;
|
||||
};
|
||||
|
||||
// Now you tell rfl::TaggedUnion that you want it to look for the field "shape".
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
@@ -272,6 +231,7 @@ In addition, it supports the following custom containers:
|
||||
- `rfl::Result`: Allows for exception-free programming.
|
||||
- `rfl::TaggedUnion`: Similar to `std::variant`, but with explicit tags that make parsing more efficient.
|
||||
- `rfl::Validator`: Allows for automatic input validation.
|
||||
- `rfl::Variant`: An alternative to `std::variant`.
|
||||
|
||||
### Custom classes
|
||||
|
||||
@@ -302,7 +262,7 @@ At the moment, this is work-in-progress. Here are some things that are still mis
|
||||
|
||||
The following compilers are supported:
|
||||
- GCC 11.4 or higher
|
||||
- Clang 14.0 or higher
|
||||
- Clang 16.0 or higher
|
||||
- MSVC (TODO)
|
||||
|
||||
### Option 1: Header-only
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
|
||||
1.3) [Struct flattening](https://github.com/getml/reflect-cpp/blob/main/docs/flatten_structs.md) - For making struct A "inherit" the fields of struct B.
|
||||
|
||||
1.4) [Anonymous fields](https://github.com/getml/reflect-cpp/blob/main/docs/anonymous_fields.md) - For structs without field names or `rfl::Field`.
|
||||
1.4) [The rfl::Field-syntax](https://github.com/getml/reflect-cpp/blob/main/docs/field_syntax.md) - Describes an alternative syntax which requires slightly more effort, but allows for some powerful functionalities.
|
||||
|
||||
1.5) [String literals](https://github.com/getml/reflect-cpp/blob/main/docs/literals.md) - For representing strings that can only assume a limited number of enumerated values.
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
`rfl::as` allows you to cast structs as other structs. This is useful if the fields in `B` are a subset of the fields in `A`
|
||||
or we need the fields in `A` and `B` to produce `C`.
|
||||
|
||||
This only works for the `rfl::Field`-syntax.
|
||||
|
||||
For example:
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -6,9 +6,9 @@ For the purpose of this section, any field of type `std::optional`, `std::shared
|
||||
|
||||
Please refer to the section on optional fields for details.
|
||||
|
||||
## Structs with `rfl::Field` and `rfl::NamedTuple`
|
||||
## Rules
|
||||
|
||||
For structs with `rfl::Field` and `rfl::NamedTuple`, the following rules apply:
|
||||
The following rules apply:
|
||||
|
||||
- You *can* add optional fields.
|
||||
- You *can* remove any optional or required fields, if they are no longer needed.
|
||||
@@ -17,15 +17,6 @@ For structs with `rfl::Field` and `rfl::NamedTuple`, the following rules apply:
|
||||
- You *can not* add any required fields.
|
||||
- You *can not* change the field names of any required fields.
|
||||
|
||||
## Structs without `rfl::Field` (anonymous fields) and `std::tuple`
|
||||
|
||||
For structs without `rfl::Field` and `std::tuple`, the following rules apply:
|
||||
|
||||
- You *can* add optional fields at the end of your struct or tuple.
|
||||
- You *can not* remove any optional or required fields, even if they are no longer needed.
|
||||
- You *can not* change the order of any existing fields.
|
||||
- You *can not* add any required fields.
|
||||
|
||||
## API versioning
|
||||
|
||||
If you find it difficult to follow the rules as stated above or you cannot guarantee that you
|
||||
@@ -35,12 +26,12 @@ You can either use `rfl::TaggedUnion` or externally tagged variants:
|
||||
|
||||
```cpp
|
||||
struct API_v_1_0 {
|
||||
rfl::Field<"version", rfl::Literal<"v1.0">> version;
|
||||
using Tag = rfl::Literal<"v1.0">;
|
||||
...
|
||||
};
|
||||
|
||||
struct API_v_1_1 {
|
||||
rfl::Field<"version", rfl::Literal<"v1.1">> version;
|
||||
using Tag = rfl::Literal<"v1.1">;
|
||||
...
|
||||
};
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ using Age = rfl::Validator<int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
Age age;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ If you absolutely do not want to make any changes to your original class, you ca
|
||||
|
||||
```cpp
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
class Person {
|
||||
@@ -107,7 +107,7 @@ initialized or face runtime errors.
|
||||
|
||||
```cpp
|
||||
struct PersonImpl {
|
||||
// ... same as in Example 2
|
||||
// ... same as in Example 1 or 2
|
||||
};
|
||||
|
||||
class Person {
|
||||
|
||||
@@ -53,9 +53,9 @@ You can then write a helper struct:
|
||||
|
||||
```cpp
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
|
||||
// 1) Static method that takes your original class as an input and
|
||||
// returns the helper struct.
|
||||
@@ -70,7 +70,7 @@ struct PersonImpl {
|
||||
// into your original class.
|
||||
// In this case, the `to_class` method is actually optional, because
|
||||
// you can directly create Person from the field values.
|
||||
Person to_class() const { return Person(first_name(), last_name(), age()); }
|
||||
Person to_class() const { return Person(first_name(), last_name(), age); }
|
||||
};
|
||||
```
|
||||
|
||||
@@ -100,9 +100,9 @@ As we have noted, in this particular example, the `Person` class can be construc
|
||||
|
||||
```cpp
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
# The `rfl::Field`-syntax
|
||||
|
||||
As we have shown, reflect-cpp can automatically detect field names from structs.
|
||||
|
||||
However, it also possible to annotate the fields using `rfl::Field`, like this:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
```
|
||||
|
||||
Even though this requires slightly more effort from the programmer, it has an important
|
||||
advantage: These field names are known at *compile time*, whereas the field names which
|
||||
are retrieved automatically are only known at *runtime*. Even though the runtime overhead
|
||||
should be negligible in most cases, this allows for compile-time checks
|
||||
which are otherwise impossible. It also allows for functionalities like `rfl::replace`,
|
||||
`rfl::as` or interactions with `rfl::NamedTuple` which we will see later in the documentation.
|
||||
|
||||
There are a two things you have to keep in mind:
|
||||
|
||||
1) If you annotate *some* of the fields using `rfl::Field`, then you must annotate *all* of them.
|
||||
2) If you are combining structs using `rfl::Flatten`, then they structs combined like this must either be annotated or not.
|
||||
|
||||
You can initilize your struct like this:
|
||||
|
||||
```cpp
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.children = std::vector<Person>()};
|
||||
```
|
||||
|
||||
Note that all fields must be explicitly set. This is on purpose, because it prevents you from accidentally forgetting fields,
|
||||
which can lead to tricky runtime errors.
|
||||
|
||||
You can initialize a field using `rfl::default_value`:
|
||||
|
||||
```cpp
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::default_value};
|
||||
```
|
||||
|
||||
You can also initialize fields in the struct declaration itself:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
```
|
||||
|
||||
Because you now have explicitly set those fields, you do not have to do this every time:
|
||||
|
||||
```cpp
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
|
||||
const auto homer = Person{.first_name = "Homer",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
```
|
||||
|
||||
You can write this to a JSON string:
|
||||
|
||||
```cpp
|
||||
const auto json_string = rfl::json::write(homer);
|
||||
```
|
||||
|
||||
```json
|
||||
{"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]}
|
||||
```
|
||||
|
||||
And you can parse it back into a struct:
|
||||
|
||||
```cpp
|
||||
const auto homer = rfl::json::read<Person>(json_string).value();
|
||||
```
|
||||
|
||||
+32
-1
@@ -9,7 +9,38 @@ all fields will be flattened to a single JSON object.
|
||||
|
||||
If there are duplicate field names, you will get a compile-time error.
|
||||
|
||||
## Example: Every employee is a person.
|
||||
## Example: Every employee is a person
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
std::string employer;
|
||||
float salary;
|
||||
};
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
.age = 45},
|
||||
.employer = "Mr. Burns",
|
||||
.salary = 60000.0};
|
||||
|
||||
const auto json_str = rfl::json::write(employee);
|
||||
```
|
||||
|
||||
This flattens all fields into a single JSON object:
|
||||
|
||||
```json
|
||||
{"first_name":"Homer","last_name":"Simpson","age":45,"salary":60000.0}
|
||||
```
|
||||
|
||||
## Example, using the `rfl::Field`-syntax.
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
|
||||
+3
-61
@@ -146,67 +146,9 @@ builder.Finish(orc); // Serialize the root of the object.
|
||||
|
||||
I think it should be fairly obvious that using reflect-cpp on top drastically reduces the amount of boilerplate code.
|
||||
|
||||
## Adding field names
|
||||
|
||||
But what it more, unlike "normal" flatbuffers, flexbuffers also supports field names. Field names make it a lot easier to maintain backwards compatability.
|
||||
|
||||
```cpp
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", std::vector<Vec3>> path;
|
||||
};
|
||||
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
|
||||
const auto weapons = std::vector<Weapon>({sword, axe});
|
||||
|
||||
const auto position = Vec3{1.0f, 2.0f, 3.0f};
|
||||
|
||||
const auto inventory =
|
||||
std::vector<std::uint8_t>({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
|
||||
|
||||
const auto orc = Monster{.pos = position,
|
||||
.mana = 150,
|
||||
.hp = 80,
|
||||
.name = "MyMonster",
|
||||
.inventory = inventory,
|
||||
.color = Color::make<"Red">(),
|
||||
.weapons = weapons,
|
||||
.equipped = rfl::make_field<"weapon">(axe),
|
||||
.path = rfl::default_value};
|
||||
|
||||
const auto bytes = rfl::flexbuf::write(orc);
|
||||
|
||||
const auto res = rfl::flexbuf::read<Monster>(bytes);
|
||||
```
|
||||
|
||||
## Reading and writing
|
||||
|
||||
Suppose you have a struct like this:
|
||||
@@ -279,9 +221,9 @@ In your header file you can write something like this:
|
||||
|
||||
```cpp
|
||||
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::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
|
||||
using InputVarType = typename rfl::flexbuf::Reader::InputVarType;
|
||||
static rfl::Result<Person> from_flexbuf(const InputVarType& _obj);
|
||||
|
||||
+7
-7
@@ -8,10 +8,10 @@ Suppose you have a struct like this:
|
||||
|
||||
```cpp
|
||||
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;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -72,9 +72,9 @@ In your header file you can write something like this:
|
||||
|
||||
```cpp
|
||||
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::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
|
||||
using JSONVar = typename rfl::json::Reader::InputVarType;
|
||||
static rfl::Result<Person> from_json_obj(const JSONVar& _obj);
|
||||
|
||||
+3
-4
@@ -57,10 +57,9 @@ using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
rfl::Rename<"firstName", FirstName> first_name;
|
||||
rfl::Rename<"lastName", LastName> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
// Leads to a runtime error, if the field "lastName" is not "Simpson"
|
||||
|
||||
+1
-33
@@ -125,26 +125,6 @@ using Shapes = std::variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"square", Square>>;
|
||||
```
|
||||
|
||||
They can also be inserted into `rfl::TaggedUnion`:
|
||||
|
||||
```cpp
|
||||
using Circle = rfl::NamedTuple<
|
||||
rfl::Field<"shape", rfl::Literal<"Circle">>,
|
||||
rfl::Field<"radius", double>>;
|
||||
|
||||
using Rectangle = rfl::NamedTuple<
|
||||
rfl::Field<"shape", rfl::Literal<"Rectangle">>,
|
||||
rfl::Field<"height", double>,
|
||||
rfl::Field<"width", double>>;
|
||||
|
||||
using Square rfl::NamedTuple<
|
||||
rfl::Field<"shape", rfl::Literal<"Square">>,
|
||||
rfl::Field<"width", double>>;
|
||||
|
||||
// Tell rfl::TaggedUnion that you want it to look for the field "shape".
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
```
|
||||
|
||||
### `rfl::replace`
|
||||
|
||||
`rfl::replace` works for `rfl::NamedTuple` as well:
|
||||
@@ -214,7 +194,7 @@ const auto employee = Employee(
|
||||
|
||||
## Transforming structs to named tuples and vice versa
|
||||
|
||||
You can transform structs to named tuples and vice versa:
|
||||
You can transform structs to named tuples and vice versa (this will only work with the `rfl::Field`-syntax:
|
||||
|
||||
```cpp
|
||||
auto bart = Person{.first_name = "Bart",
|
||||
@@ -236,15 +216,3 @@ const auto bart_struct = rfl::from_named_tuple<Person>(bart_nt);
|
||||
const auto bart_struct = rfl::from_named_tuple<Person>(std::move(bart_nt));
|
||||
```
|
||||
|
||||
## rfl::NamedTuple is central to reflect-cpp
|
||||
|
||||
`rfl::NamedTuple` is basically at the heart of how the reflect-cpp reflection
|
||||
is actually implemented. If you look at how reflect-cpp is implemented you will find that
|
||||
structs are transformed from and to `rfl::NamedTuple`s
|
||||
all the time (using zero-copy operations wherever possible).
|
||||
That is because `rfl::NamedTuple` actually support reflection,
|
||||
where as structs don't.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -19,11 +19,11 @@ So this is how we could rewrite the previous example:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
|
||||
// Indicates to the library that the field is optional.
|
||||
rfl::Field<"children", std::optional<std::vector<Person>>> children = rfl::default_value;
|
||||
std::optional<std::vector<Person>> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
+3
-2
@@ -1,7 +1,8 @@
|
||||
# `rfl::replace`
|
||||
|
||||
`rfl::replace` creates a deep copy of the original struct or moves the original struct, replacing one or several
|
||||
fields in the process.
|
||||
`rfl::replace` creates a deep copy of the original struct or moves the original struct, replacing one or several fields in the process.
|
||||
|
||||
This only works for the `rfl::Field`-syntax.
|
||||
|
||||
In some cases, we really only want to change one or a few fields, to get from one struct to another:
|
||||
|
||||
|
||||
+30
-30
@@ -4,9 +4,9 @@ In previous sections, we have defined the `Person` class recursively:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -15,9 +15,9 @@ This works, because `std::vector` contains a pointer under-the-hood. But what wo
|
||||
```cpp
|
||||
// WILL NOT COMPILE
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"child", Person> child;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
Person child;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -33,15 +33,15 @@ A naive implementation might look like this:
|
||||
// WILL NOT COMPILE
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"prediction", double> prediction;
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double prediction;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"lesser", DecisionTree> lesser;
|
||||
rfl::Field<"greater", DecisionTree> greater;
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
DecisionTree lesser;
|
||||
DecisionTree greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
@@ -58,15 +58,15 @@ A possible solution might be to use `std::unique_ptr`:
|
||||
// Will compile, but not an ideal design.
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"prediction", double> prediction;
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double prediction;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"lesser", std::unique_ptr<DecisionTree>> lesser;
|
||||
rfl::Field<"greater", std::unique_ptr<DecisionTree>> greater;
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
std::unique_ptr<DecisionTree> lesser;
|
||||
std::unique_ptr<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
@@ -90,15 +90,15 @@ still have code that compiles? By using `rfl::Box` instead of `std::unique_ptr`:
|
||||
```cpp
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"prediction", double> prediction;
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double prediction;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"lesser", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"greater", rfl::Box<DecisionTree>> greater;
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Box<DecisionTree> lesser;
|
||||
rfl::Box<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
@@ -144,15 +144,15 @@ If you want to use reference-counted pointers, instead of unique pointers, you c
|
||||
```cpp
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Ref<DecisionTree> lesser;
|
||||
rfl::Ref<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
+16
-18
@@ -1,14 +1,12 @@
|
||||
# Structs
|
||||
|
||||
In order to setup a struct, all of its fields need to be an `rfl::Field`. The `rfl::Field` contains the type and the name.
|
||||
The name is a compile-time string and won't have any runtime overhead. The compile-time string is what will eventually
|
||||
appear in the JSON (or any other format).
|
||||
Structs can be set up as follows:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -20,28 +18,28 @@ const auto bart = Person{.first_name = "Bart",
|
||||
.children = std::vector<Person>()};
|
||||
```
|
||||
|
||||
Note that all fields must be explicitly set. This is on purpose, because it prevents you from accidentally forgetting fields,
|
||||
which can lead to tricky runtime errors.
|
||||
|
||||
You can initialize a field using `rfl::default_value`:
|
||||
JSON uses Hungarian case, but C++ uses snake case, so you might want to rename your fields:
|
||||
|
||||
```cpp
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::default_value};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
You can also initialize fields in the struct declaration itself:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
Because you now have explicitly set those fields, you do not have to do this every time:
|
||||
Because you now have explicitly set this field, you do not have to do this every time:
|
||||
|
||||
```cpp
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
@@ -78,7 +76,7 @@ const auto homer = rfl::json::read<Person>(json_string).value();
|
||||
reflect-cpp needs to be able to create the structs like this:
|
||||
|
||||
```cpp
|
||||
Person{rfl::Field<"firstName", std::string>("Bart"), rfl::Field<"lastName", std::string>("Simpson"), ...};
|
||||
Person{"Bart", "Simpson", ...};
|
||||
```
|
||||
|
||||
But if you create a custom constructor, then C++ will no longer allow this kind of constructions.
|
||||
|
||||
@@ -4,25 +4,23 @@
|
||||
|
||||
Sometimes you know that the JSON object can be one of several alternatives. For example,
|
||||
you might have several shapes like `Circle`, `Rectangle` or `Square`. For these kind of
|
||||
cases, the C++ standard library contains `std::variant`:
|
||||
cases, the C++ standard library contains `std::variant`, even though we we recommend you to use `rfl::Variant` instead:
|
||||
|
||||
```cpp
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
double width;
|
||||
};
|
||||
|
||||
// NOTE: If you are using anonymous fields,
|
||||
// you would probably have to use `rfl::Variant` instead.
|
||||
using Shapes = std::variant<Circle, Rectangle, Square>;
|
||||
using Shapes = rfl::Variant<Circle, Rectangle, Square>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
@@ -38,9 +36,90 @@ several problems with this:
|
||||
2) It leads to confusing error messages: If none of the alternatives can be matched, you will get an error message telling you why each of the alternatives couldn't be matched. Such error messages are very long-winding and hard to read.
|
||||
3) It is dangerous. Imagine we had written `std::variant<Circle, Square, Rectangle>` instead of `std::variant<Circle, Rectangle, Square>`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs.
|
||||
|
||||
## `rfl::TaggedUnion` (internally tagged)
|
||||
|
||||
One way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
|
||||
|
||||
TaggedUnions use the name of the struct as an identifying tag. It will then try to take that field from the JSON object, match it to the correct alternative and then only parse the correct alternative.
|
||||
|
||||
We will now rewrite the example from above using `rfl::TaggedUnion`:
|
||||
|
||||
```cpp
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
// Now you tell rfl::TaggedUnion that you want it to write the name
|
||||
// of the struct into an extra field called "shape".
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
const auto json_string = rfl::json::write(r);
|
||||
|
||||
const auto r2 = rfl::json::read<Shapes>(json_string);
|
||||
```
|
||||
|
||||
The resulting JSON looks like this:
|
||||
```json
|
||||
{"shape":"Rectangle","height":10.0,"width":5.0}
|
||||
```
|
||||
|
||||
Because the tag is inside the JSON object, this is called *internally tagged*.
|
||||
It is the standard in Python's [pydantic](https://docs.pydantic.dev/latest/api/standard_library_types/#union).
|
||||
|
||||
It is also possible to set the tag explicitly:
|
||||
|
||||
```cpp
|
||||
struct Circle {
|
||||
using Tag = rfl::Literal<"circle", "Circle">;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
using Tag = rfl::Literal<"rectangle", "Rectangle">;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
using Tag = rfl::Literal<"square", "Square">;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
const auto json_string = rfl::json::write(r);
|
||||
|
||||
const auto r2 = rfl::json::read<Shapes>(json_string);
|
||||
```
|
||||
|
||||
The JSON generated by `rfl::json::write` looks like this:
|
||||
|
||||
```json
|
||||
{"shape":"rectangle","height":10.0,"width":5.0}
|
||||
```
|
||||
|
||||
However, `rfl::json::read` would also accept this:
|
||||
|
||||
```json
|
||||
{"shape":"Rectangle","height":10.0,"width":5.0}
|
||||
```
|
||||
|
||||
## `std::variant` or `rfl::Variant` (externally tagged)
|
||||
|
||||
From a functional programming point-of-view, the most straightforward way to handle these problems is to add tags.
|
||||
Another approach is to add external tags.
|
||||
|
||||
You can do that using `rfl::Field`:
|
||||
|
||||
@@ -55,7 +134,6 @@ We can rewrite the example from above:
|
||||
|
||||
```cpp
|
||||
// Circle, Rectangle and Square are the same as above.
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", Square>>;
|
||||
@@ -75,62 +153,15 @@ The resulting JSON looks like this:
|
||||
|
||||
Because the tag is external, this is called *externally tagged*. It is the standard in Rust's [serde-json](https://serde.rs/enum-representations.html).
|
||||
|
||||
## `rfl::TaggedUnion` (internally tagged)
|
||||
|
||||
But sometimes you also want the tag to be inside the class itself. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
|
||||
|
||||
TaggedUnions require some kind of identifying field on your struct, which must be expressed as an `rfl::Literal`. It will then try to take that field from the JSON object, match it to the correct alternative and then only parse the correct alternative.
|
||||
|
||||
We will now rewrite the example from above using `rfl::TaggedUnion`:
|
||||
|
||||
```cpp
|
||||
struct Circle {
|
||||
// All alternatives must contain a field named "shape". The type
|
||||
// of the field must be an rfl::Literal. Since that Literal usually
|
||||
// only contains one string (even though it doesn't have to), you can
|
||||
// assign it to its default value.
|
||||
rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"shape", rfl::Literal<"Rectangle">> shape =
|
||||
rfl::default_value;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
// Now you tell rfl::TaggedUnion that you want it to look for the field "shape".
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
const Shapes r = Rectangle{.height = 10, .width = 5};
|
||||
|
||||
const auto json_string = rfl::json::write(r);
|
||||
|
||||
const auto r2 = rfl::json::read<Shapes>(json_string);
|
||||
```
|
||||
|
||||
The resulting JSON looks like this:
|
||||
```json
|
||||
{"shape":"Rectangle","height":10.0,"width":5.0}
|
||||
```
|
||||
|
||||
Because the tag is inside the JSON object, this is called *internally tagged*.
|
||||
It is the standard in Python's [pydantic](https://docs.pydantic.dev/latest/api/standard_library_types/#union).
|
||||
|
||||
## The visitor pattern
|
||||
|
||||
In C++, the idiomatic way to handle `std::variant` and `rfl::TaggedUnion` is the [visitor pattern](https://en.cppreference.com/w/cpp/utility/variant/visit).
|
||||
|
||||
For instance, the externally tagged `std::variant` from the example above could be handled like this:
|
||||
For instance, the externally tagged `rfl::Variant` from the example above could be handled like this:
|
||||
|
||||
```cpp
|
||||
using Shapes = std::variant<rfl::Field<"circle", Circle>,
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", Square>>;
|
||||
|
||||
@@ -152,10 +183,10 @@ const auto handle_shapes = [](const auto& field) {
|
||||
}
|
||||
};
|
||||
|
||||
std::visit(handle_shapes, my_shape);
|
||||
std::visit(handle_shapes, my_shape.variant());
|
||||
```
|
||||
|
||||
Likewise, `rfl::TaggedUnion` is just an `std::variant` under-the-hood. The original variant can be
|
||||
Likewise, `rfl::TaggedUnion` contains `std::variant` under-the-hood. The original variant can be
|
||||
retrieved using `.variant()`:
|
||||
|
||||
```cpp
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "rfl/Pattern.hpp"
|
||||
#include "rfl/PatternValidator.hpp"
|
||||
#include "rfl/Ref.hpp"
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/Size.hpp"
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "rfl/Timestamp.hpp"
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
#ifndef RFL_RENAME_HPP_
|
||||
#define RFL_RENAME_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Literal.hpp"
|
||||
#include "rfl/default.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
/// Used to define a field in the NamedTuple.
|
||||
template <internal::StringLiteral _name, class T>
|
||||
struct Rename {
|
||||
/// The underlying type.
|
||||
using Type = T;
|
||||
|
||||
/// The name of the field.
|
||||
using Name = rfl::Literal<_name>;
|
||||
|
||||
Rename() : value_(Type()) {}
|
||||
|
||||
Rename(const Type& _value) : value_(_value) {}
|
||||
|
||||
Rename(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Rename(Rename<_name, T>&& _field) noexcept = default;
|
||||
|
||||
Rename(const Rename<_name, Type>& _field) = default;
|
||||
|
||||
template <class U>
|
||||
Rename(const Rename<_name, U>& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class U>
|
||||
Rename(Rename<_name, U>&& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Rename(const U& _value) : value_(_value) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Rename(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
|
||||
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
Rename(const Rename<_name, U>& _field) : value_(_field.value()) {}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>,
|
||||
bool>::type = true>
|
||||
Rename(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Rename() = default;
|
||||
|
||||
/// The name of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral name_ = _name;
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(Type&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const U& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class U = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<U>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Rename<_name, T>& operator=(const Rename<_name, T>& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Rename<_name, T>& operator=(Rename<_name, T>&& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(const Rename<_name, U>& _field) {
|
||||
value_ = _field.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U>
|
||||
auto& operator=(Rename<_name, U>&& _field) {
|
||||
value_ = std::forward<T>(_field.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(Type&& _value) { value_ = std::move(_value); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& value() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& value() const { return value_; }
|
||||
|
||||
/// The underlying value.
|
||||
Type value_;
|
||||
};
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_INTERNAL_FIELD_TUPLE_T_HPP_
|
||||
#define RFL_INTERNAL_FIELD_TUPLE_T_HPP_
|
||||
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
#include "rfl/internal/copy_to_field_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
using field_tuple_t =
|
||||
typename std::invoke_result<decltype(copy_to_field_tuple<T>), T>::type;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef RFL_INTERNAL_FIELDS_HPP_
|
||||
#define RFL_INTERNAL_FIELDS_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <int N>
|
||||
struct Fields {
|
||||
std::array<std::string, N> names_;
|
||||
|
||||
std::unordered_map<std::string_view, std::int16_t> indices_;
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef RFL_INTERNAL_FAKEOBJECT_HPP_
|
||||
#define RFL_INTERNAL_FAKEOBJECT_HPP_
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
/// This is required to automatically retrieve the names.
|
||||
template <class T>
|
||||
extern T fake_object;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,41 @@
|
||||
#ifndef RFL_INTERNAL_GETSTRUCTNAME_HPP_
|
||||
#define RFL_INTERNAL_GETSTRUCTNAME_HPP_
|
||||
|
||||
#include <iostream>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
std::string remove_namespaces(const std::string& _name) {
|
||||
const size_t pos = _name.find_last_of(":");
|
||||
if (pos == std::string::npos) {
|
||||
return _name;
|
||||
}
|
||||
return _name.substr(pos + 1);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::string get_struct_name_impl() {
|
||||
const std::string func_name = std::source_location::current().function_name();
|
||||
const size_t begin = func_name.find("T = ") + 4;
|
||||
if (begin == std::string::npos) {
|
||||
return "AnonymousStruct";
|
||||
}
|
||||
const size_t end = func_name.find_first_of("])};", begin);
|
||||
if (end == std::string::npos) {
|
||||
return "AnonymousStruct";
|
||||
}
|
||||
return remove_namespaces(func_name.substr(begin, end - begin));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::string get_struct_name() {
|
||||
return get_struct_name_impl<std::decay_t<std::remove_pointer_t<T>>>();
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef RFL_HASTAGV_HPP_
|
||||
#define RFL_HASTAGV_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class Wrapper>
|
||||
class HasTag {
|
||||
private:
|
||||
template <class U>
|
||||
static std::int64_t foo(...);
|
||||
|
||||
template <class U>
|
||||
static std::int32_t foo(typename U::Tag*);
|
||||
|
||||
public:
|
||||
static constexpr bool value =
|
||||
sizeof(foo<Wrapper>(nullptr)) == sizeof(std::int32_t);
|
||||
};
|
||||
|
||||
/// Used for tagged unions - determines whether a struct as a Tag.
|
||||
template <typename Wrapper>
|
||||
constexpr bool has_tag_v = HasTag<Wrapper>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef RFL_INTERNAL_ISRENAME_HPP_
|
||||
#define RFL_INTERNAL_ISRENAME_HPP_
|
||||
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T>
|
||||
class is_rename;
|
||||
|
||||
template <class T>
|
||||
class is_rename : public std::false_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_rename<Rename<_name, Type>> : public std::true_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_rename<Rename<_name, Type>*> : public std::true_type {};
|
||||
|
||||
template <StringLiteral _name, class Type>
|
||||
class is_rename<const Rename<_name, Type>*> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
constexpr bool is_rename_v = is_rename<T>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
+2287
-2365
File diff suppressed because it is too large
Load Diff
@@ -16,47 +16,49 @@ namespace internal {
|
||||
|
||||
template <class PtrFieldTuple, class... Args>
|
||||
auto flatten_ptr_field_tuple(const PtrFieldTuple& _t, Args&&... _args) {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
constexpr auto i = sizeof...(Args);
|
||||
if constexpr (i == std::tuple_size_v<std::decay_t<PtrFieldTuple>>) {
|
||||
return std::tuple_cat(std::forward<Args>(_args)...);
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
const auto subtuple =
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(subtuple));
|
||||
} else {
|
||||
using T = std::tuple_element_t<i, std::decay_t<PtrFieldTuple>>;
|
||||
if constexpr (internal::is_flatten_field<T>::value) {
|
||||
const auto subtuple =
|
||||
internal::to_ptr_field_tuple(*std::get<i>(_t).get());
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
flatten_ptr_field_tuple(subtuple));
|
||||
} else {
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
return flatten_ptr_field_tuple(_t, std::forward<Args>(_args)...,
|
||||
std::make_tuple(std::get<i>(_t)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class PtrFieldTuple>
|
||||
auto field_tuple_to_named_tuple(const PtrFieldTuple& _ptr_field_tuple) {
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
const auto ft_to_nt = []<class... Fields>(const Fields&... _fields) {
|
||||
return make_named_tuple(_fields...);
|
||||
};
|
||||
|
||||
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
|
||||
} else {
|
||||
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
|
||||
return std::apply(ft_to_nt, flattened_tuple);
|
||||
}
|
||||
if constexpr (!has_flatten_fields<std::decay_t<PtrFieldTuple>>()) {
|
||||
return std::apply(ft_to_nt, std::move(_ptr_field_tuple));
|
||||
} else {
|
||||
const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple);
|
||||
return std::apply(ft_to_nt, flattened_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a named tuple that contains pointers to the original values in
|
||||
/// the struct.
|
||||
template <class T>
|
||||
auto to_ptr_named_tuple(const T& _t) {
|
||||
if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
return field_tuple_to_named_tuple(ptr_field_tuple);
|
||||
}
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
return to_ptr_named_tuple(*_t);
|
||||
} else if constexpr (is_named_tuple_v<std::decay_t<T>>) {
|
||||
return nt_to_ptr_named_tuple(_t);
|
||||
} else {
|
||||
const auto ptr_field_tuple = to_ptr_field_tuple(_t);
|
||||
return field_tuple_to_named_tuple(ptr_field_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -38,7 +38,9 @@ with open("generated_code4.cpp", "w", encoding="utf-8") as codefile:
|
||||
|
||||
template <class T>
|
||||
auto to_ptr_tuple(const T& _t) {
|
||||
if constexpr (has_n_fields<T, 0>) {
|
||||
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
|
||||
return to_ptr_tuple(*_t);
|
||||
} else if constexpr (has_n_fields<T, 0>) {
|
||||
return std::make_tuple();
|
||||
} else if constexpr (has_n_fields<T, 1>) {
|
||||
auto& [f1] = _t;
|
||||
|
||||
+55
-56
@@ -19,77 +19,76 @@ namespace rfl {
|
||||
namespace json {
|
||||
|
||||
struct Writer {
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputObject {
|
||||
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
struct YYJSONOutputObject {
|
||||
YYJSONOutputObject(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
struct YYJSONOutputVar {
|
||||
YYJSONOutputVar(yyjson_mut_val* _val) : val_(_val) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
YYJSONOutputVar(YYJSONOutputArray _arr) : val_(_arr.val_) {}
|
||||
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
YYJSONOutputVar(YYJSONOutputObject _obj) : val_(_obj.val_) {}
|
||||
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
yyjson_mut_val* val_;
|
||||
};
|
||||
|
||||
using OutputArrayType = YYJSONOutputArray;
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
using OutputArrayType = YYJSONOutputArray;
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
|
||||
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
|
||||
~Writer() = default;
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
}
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str()));
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return OutputVarType(yyjson_mut_bool(doc_, _var));
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
return OutputVarType(
|
||||
yyjson_mut_real(doc_, static_cast<double>(_var)));
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
return OutputVarType(yyjson_mut_int(doc_, static_cast<int>(_var)));
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
}
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
yyjson_mut_doc* doc_;
|
||||
yyjson_mut_doc* doc_;
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
|
||||
+285
-16
@@ -1,6 +1,8 @@
|
||||
#ifndef RFL_PARSING_PARSER_HPP_
|
||||
#define RFL_PARSING_PARSER_HPP_
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
@@ -29,13 +31,18 @@
|
||||
#include "rfl/always_false.hpp"
|
||||
#include "rfl/field_type.hpp"
|
||||
#include "rfl/from_named_tuple.hpp"
|
||||
#include "rfl/internal/Fields.hpp"
|
||||
#include "rfl/internal/Memoization.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
#include "rfl/internal/all_fields.hpp"
|
||||
#include "rfl/internal/flattened_ptr_tuple_t.hpp"
|
||||
#include "rfl/internal/flattened_tuple_t.hpp"
|
||||
#include "rfl/internal/get_field_names.hpp"
|
||||
#include "rfl/internal/get_struct_name.hpp"
|
||||
#include "rfl/internal/has_fields.hpp"
|
||||
#include "rfl/internal/has_reflection_method_v.hpp"
|
||||
#include "rfl/internal/has_reflection_type_v.hpp"
|
||||
#include "rfl/internal/has_tag_v.hpp"
|
||||
#include "rfl/internal/is_basic_type.hpp"
|
||||
#include "rfl/internal/move_from_tuple.hpp"
|
||||
#include "rfl/internal/no_duplicate_field_names.hpp"
|
||||
@@ -44,6 +51,7 @@
|
||||
#include "rfl/internal/tuple_t.hpp"
|
||||
#include "rfl/named_tuple_t.hpp"
|
||||
#include "rfl/parsing/AreReaderAndWriter.hpp"
|
||||
#include "rfl/parsing/TaggedUnionWrapper.hpp"
|
||||
#include "rfl/parsing/is_forward_list.hpp"
|
||||
#include "rfl/parsing/is_map_like.hpp"
|
||||
#include "rfl/parsing/is_map_like_not_multimap.hpp"
|
||||
@@ -62,6 +70,219 @@ struct Parser;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Used for any structs that are aggregates.
|
||||
template <class R, class W, class T>
|
||||
struct StructParser {
|
||||
using InputObjectType = typename R::InputObjectType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using PtrTupleType = internal::flattened_ptr_tuple_t<T>;
|
||||
using TupleType = internal::flattened_tuple_t<T>;
|
||||
|
||||
static constexpr auto tuple_size = std::tuple_size_v<TupleType>;
|
||||
|
||||
public:
|
||||
/// Generates a NamedTuple from a JSON Object.
|
||||
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto& indices = field_indices();
|
||||
|
||||
const auto fct = [&](const std::string_view& _str) -> std::int16_t {
|
||||
const auto it = indices.find(_str);
|
||||
return it != indices.end() ? it->second : static_cast<std::uint16_t>(-1);
|
||||
};
|
||||
|
||||
const auto to_fields_array = [&](auto _obj) {
|
||||
return _r.template to_fields_array<tuple_size>(fct, _obj);
|
||||
};
|
||||
|
||||
const auto build_tuple = [&](auto _fields_vec) -> Result<TupleType> {
|
||||
return build_tuple_recursively(_r, _fields_vec);
|
||||
};
|
||||
|
||||
const auto to_struct = [](TupleType&& _t) -> T {
|
||||
return internal::move_from_tuple<T, TupleType>(std::move(_t));
|
||||
};
|
||||
|
||||
return _r.to_object(_var)
|
||||
.transform(to_fields_array)
|
||||
.and_then(build_tuple)
|
||||
.transform(to_struct);
|
||||
}
|
||||
|
||||
/// Transforms a NamedTuple into a JSON object.
|
||||
static OutputVarType write(const W& _w, const T& _var) noexcept {
|
||||
const auto ptr_tuple = internal::to_flattened_ptr_tuple(_var);
|
||||
|
||||
auto obj = _w.new_object();
|
||||
|
||||
build_object_recursively(_w, ptr_tuple, &obj);
|
||||
|
||||
return OutputVarType(obj);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Builds the tuple field by field containing the values for the structs
|
||||
/// recursively.
|
||||
template <class... Args>
|
||||
static Result<TupleType> build_tuple_recursively(
|
||||
const R& _r,
|
||||
const std::array<std::optional<InputVarType>, tuple_size>& _fields_arr,
|
||||
Args&&... _args) noexcept {
|
||||
constexpr auto i = sizeof...(Args);
|
||||
|
||||
if constexpr (i == std::tuple_size_v<TupleType>) {
|
||||
return std::make_tuple(std::move(_args)...);
|
||||
} else {
|
||||
using ValueType =
|
||||
std::decay_t<typename std::tuple_element<i, TupleType>::type>;
|
||||
|
||||
const auto& f = std::get<i>(_fields_arr);
|
||||
|
||||
const auto key = std::get<i>(field_names());
|
||||
|
||||
if (!f) {
|
||||
if constexpr (is_required<ValueType>()) {
|
||||
return collect_errors<i + 1>(
|
||||
_r, _fields_arr,
|
||||
std::vector<Error>(
|
||||
{Error("Field named '" + key + "' not found!")}));
|
||||
} else {
|
||||
return build_tuple_recursively(_r, _fields_arr, std::move(_args)...,
|
||||
ValueType());
|
||||
}
|
||||
}
|
||||
|
||||
const auto handle_error = [&](Error&& _error) {
|
||||
return collect_errors<i + 1>(_r, _fields_arr,
|
||||
std::vector<Error>({std::move(_error)}));
|
||||
};
|
||||
|
||||
const auto build_tuple = [&](auto&& _value) {
|
||||
return build_tuple_recursively(_r, _fields_arr, std::move(_args)...,
|
||||
std::move(_value));
|
||||
};
|
||||
|
||||
return get_value<ValueType>(_r, *f, key)
|
||||
.or_else(handle_error)
|
||||
.and_then(build_tuple);
|
||||
}
|
||||
}
|
||||
|
||||
/// If something went wrong, we want to collect all of the errors - it's
|
||||
/// just good UX.
|
||||
template <int _i>
|
||||
static Error collect_errors(
|
||||
const R& _r,
|
||||
const std::array<std::optional<InputVarType>, tuple_size>& _fields_arr,
|
||||
std::vector<Error> _errors) noexcept {
|
||||
if constexpr (_i == tuple_size) {
|
||||
if (_errors.size() == 1) {
|
||||
return std::move(_errors[0]);
|
||||
} else {
|
||||
std::string msg =
|
||||
"Found " + std::to_string(_errors.size()) + " errors:";
|
||||
for (size_t i = 0; i < _errors.size(); ++i) {
|
||||
msg += "\n" + std::to_string(i + 1) + ") " + _errors.at(i).what();
|
||||
}
|
||||
return Error(msg);
|
||||
}
|
||||
} else {
|
||||
using ValueType =
|
||||
std::decay_t<typename std::tuple_element<_i, TupleType>::type>;
|
||||
|
||||
const auto& f = std::get<_i>(_fields_arr);
|
||||
|
||||
const auto key = std::get<_i>(field_names());
|
||||
|
||||
if (!f) {
|
||||
if constexpr (is_required<ValueType>()) {
|
||||
_errors.emplace_back(Error("Field named '" + key + "' not found."));
|
||||
}
|
||||
return collect_errors<_i + 1>(_r, _fields_arr, std::move(_errors));
|
||||
}
|
||||
|
||||
const auto add_error_if_applicable =
|
||||
[&](Error&& _error) -> Result<ValueType> {
|
||||
_errors.emplace_back(std::move(_error));
|
||||
return _error;
|
||||
};
|
||||
|
||||
get_value<ValueType>(_r, *f, key).or_else(add_error_if_applicable);
|
||||
|
||||
return collect_errors<_i + 1>(_r, _fields_arr, std::move(_errors));
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the object field by field.
|
||||
template <int _i = 0>
|
||||
static void build_object_recursively(const W& _w, const PtrTupleType& _tup,
|
||||
OutputObjectType* _ptr) noexcept {
|
||||
if constexpr (_i >= tuple_size) {
|
||||
return;
|
||||
} else {
|
||||
using ValueType =
|
||||
std::decay_t<typename std::tuple_element<_i, PtrTupleType>::type>;
|
||||
|
||||
auto value = Parser<R, W, ValueType>::write(_w, std::get<_i>(_tup));
|
||||
|
||||
const auto& key = std::get<_i>(field_names());
|
||||
|
||||
if constexpr (!is_required<ValueType>()) {
|
||||
if (!_w.is_empty(value)) {
|
||||
_w.set_field(key, value, _ptr);
|
||||
}
|
||||
} else {
|
||||
_w.set_field(key, value, _ptr);
|
||||
}
|
||||
|
||||
return build_object_recursively<_i + 1>(_w, _tup, _ptr);
|
||||
}
|
||||
}
|
||||
|
||||
/// Uses a memoization pattern to retrieve the field indices.
|
||||
/// There are some objects that we are likely to parse many times,
|
||||
/// so we only calculate these indices once.
|
||||
static const auto& field_indices() noexcept {
|
||||
return fields_.value(make_fields).indices_;
|
||||
}
|
||||
|
||||
/// Uses a memoization pattern to retrieve the field names.
|
||||
/// There are some objects that we are likely to parse many times,
|
||||
/// so we only calculate these indices once.
|
||||
static const auto& field_names() noexcept {
|
||||
return fields_.value(make_fields).names_;
|
||||
}
|
||||
|
||||
/// Retrieves the value from the object. This is mainly needed to
|
||||
/// generate a better error message.
|
||||
template <class ValueType>
|
||||
static auto get_value(const R& _r, const InputVarType _var,
|
||||
const std::string& _key) noexcept {
|
||||
const auto embellish_error = [&](const Error& _e) {
|
||||
return Error("Failed to parse field '" + _key + "': " + _e.what());
|
||||
};
|
||||
return Parser<R, W, ValueType>::read(_r, _var).or_else(embellish_error);
|
||||
}
|
||||
|
||||
/// Builds the object field by field.
|
||||
static void make_fields(internal::Fields<tuple_size>* _fields) noexcept {
|
||||
_fields->names_ = internal::get_field_names<T>();
|
||||
for (size_t i = 0; i < tuple_size; ++i) {
|
||||
_fields->indices_[_fields->names_[i]] = static_cast<std::int16_t>(i);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// Maps each of the field names to an index signifying their order and
|
||||
/// vice-versa.
|
||||
static inline internal::Memoization<internal::Fields<tuple_size>> fields_;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Default case - anything that cannot be explicitly matched.
|
||||
template <class R, class W, class T>
|
||||
requires AreReaderAndWriter<R, W, T>
|
||||
@@ -97,11 +318,7 @@ struct Parser {
|
||||
return Parser<R, W, NamedTupleType>::read(_r, _var).and_then(
|
||||
to_struct);
|
||||
} else {
|
||||
using TupleType = internal::flattened_tuple_t<T>;
|
||||
const auto to_struct = [](TupleType&& _t) -> T {
|
||||
return internal::move_from_tuple<T, TupleType>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, TupleType>::read(_r, _var).transform(to_struct);
|
||||
return StructParser<R, W, T>::read(_r, _var);
|
||||
}
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _r.template to_basic_type<std::decay_t<T>>(_var);
|
||||
@@ -131,9 +348,7 @@ struct Parser {
|
||||
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
|
||||
return Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple);
|
||||
} else {
|
||||
const auto ptr_tuple = internal::to_flattened_ptr_tuple(_var);
|
||||
using PtrTupleType = std::decay_t<decltype(ptr_tuple)>;
|
||||
return Parser<R, W, PtrTupleType>::write(_w, ptr_tuple);
|
||||
return StructParser<R, W, T>::write(_w, _var);
|
||||
}
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _w.from_basic_type(_var);
|
||||
@@ -633,6 +848,28 @@ struct Parser<R, W, Ref<T>> {
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class R, class W, class T, internal::StringLiteral _name>
|
||||
requires AreReaderAndWriter<R, W, Rename<_name, T>>
|
||||
struct Parser<R, W, Rename<_name, T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<Rename<_name, T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_rename = [](auto&& _t) {
|
||||
return Rename<_name, T>(std::move(_t));
|
||||
};
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_rename);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const Rename<_name, T>& _rename) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _rename.value());
|
||||
}
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <class R, class W, class T>
|
||||
requires AreReaderAndWriter<R, W, Result<T>>
|
||||
struct Parser<R, W, Result<T>> {
|
||||
@@ -768,9 +1005,10 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
static OutputVarType write(
|
||||
const W& _w, const TaggedUnion<_discriminator, AlternativeTypes...>&
|
||||
_tagged_union) noexcept {
|
||||
using VariantType =
|
||||
typename TaggedUnion<_discriminator, AlternativeTypes...>::VariantType;
|
||||
return Parser<R, W, VariantType>::write(_w, _tagged_union.variant_);
|
||||
const auto handle = [&](const auto& _val) -> OutputVarType {
|
||||
return write_wrapped(_w, _val);
|
||||
};
|
||||
return std::visit(handle, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -831,13 +1069,44 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
template <class T>
|
||||
static inline bool contains_disc_value(
|
||||
const std::string& _disc_value) noexcept {
|
||||
if constexpr (!internal::has_reflection_type_v<T>) {
|
||||
using LiteralType = field_type_t<_discriminator, T>;
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return contains_disc_value<typename T::ReflectionType>(_disc_value);
|
||||
} else if constexpr (internal::has_tag_v<T>) {
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::contains(_disc_value);
|
||||
return LiteralType::contains(_disc_value);
|
||||
} else {
|
||||
using LiteralType =
|
||||
field_type_t<_discriminator, typename T::ReflectionType>;
|
||||
return LiteralType::contains(_disc_value);
|
||||
return _disc_value == internal::get_struct_name<T>();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static inline std::string make_tag() noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return make_tag<typename T::ReflectionType>();
|
||||
} else if constexpr (internal::has_tag_v<T>) {
|
||||
using LiteralType = typename T::Tag;
|
||||
return LiteralType::template name_of<0>().str();
|
||||
} else {
|
||||
return internal::get_struct_name<T>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a wrapped version of the original object, which contains the tag.
|
||||
template <class T>
|
||||
static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept {
|
||||
const auto tag = make_tag<T>();
|
||||
using TagType = std::decay_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::decay_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
|
||||
#define RFL_PARSING_TAGGEDUNIONWRAPPER_HPP_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "rfl/Flatten.hpp"
|
||||
#include "rfl/Rename.hpp"
|
||||
#include "rfl/internal/StringLiteral.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T, class LiteralType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperNoFields {
|
||||
rfl::Rename<_discriminator, std::string> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
template <class T, class LiteralType, internal::StringLiteral _discriminator>
|
||||
struct TaggedUnionWrapperWithFields {
|
||||
rfl::Field<_discriminator, std::string> tag;
|
||||
rfl::Flatten<const T*> fields;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,13 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_all_of() {
|
||||
std::cout << "test_all_of" << std::endl;
|
||||
namespace test_all_of {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
@@ -23,3 +26,4 @@ void test_all_of() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_all_of
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_anonymous_fields() {
|
||||
std::cout << "test_anonymous_fields" << std::endl;
|
||||
namespace test_anonymous_fields {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
@@ -49,3 +52,4 @@ void test_anonymous_fields() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_anonymous_fields
|
||||
|
||||
@@ -2,32 +2,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_box() {
|
||||
std::cout << "test_box" << std::endl;
|
||||
namespace test_box {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
@@ -41,3 +42,4 @@ void test_box() {
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_box
|
||||
|
||||
@@ -2,36 +2,39 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_custom_class1() {
|
||||
std::cout << "test_custom_class1" << std::endl;
|
||||
namespace test_custom_class1 {
|
||||
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
|
||||
using ReflectionType = PersonImpl;
|
||||
using ReflectionType = PersonImpl;
|
||||
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
|
||||
const auto bart = Person("Bart");
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(bart);
|
||||
const auto bart = Person("Bart");
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class1
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc2 {
|
||||
namespace test_custom_class2 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
@@ -40,23 +41,27 @@ struct PersonImpl {
|
||||
Person to_class() const { return Person(first_name(), last_name(), age()); }
|
||||
};
|
||||
|
||||
} // namespace tcc2
|
||||
} // namespace test_custom_class2
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc2::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc2::Person,
|
||||
tcc2::PersonImpl> {};
|
||||
struct Parser<ReaderType, WriterType, test_custom_class2::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class2::Person,
|
||||
test_custom_class2::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class2() {
|
||||
std::cout << "test_custom_class2" << std::endl;
|
||||
namespace test_custom_class2 {
|
||||
|
||||
const auto bart = tcc2::Person("Bart", "Simpson", 10);
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = test_custom_class2::Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class2
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc3 {
|
||||
namespace test_custom_class3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
@@ -40,24 +41,28 @@ struct PersonImpl {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace tcc3
|
||||
} // namespace test_custom_class3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc3::Person,
|
||||
tcc3::PersonImpl> {};
|
||||
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
|
||||
test_custom_class3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class3() {
|
||||
std::cout << "test_custom_class3" << std::endl;
|
||||
namespace test_custom_class3 {
|
||||
|
||||
const auto bart =
|
||||
tcc3::Person("Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = test_custom_class3::Person(
|
||||
"Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class3
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that the example in the README works as expected.
|
||||
// It would be embarrassing if it didn't...
|
||||
void test_custom_constructor() {
|
||||
std::cout << "test_custom_constructor" << std::endl;
|
||||
namespace test_custom_constructor {
|
||||
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_flexbuf(
|
||||
typename rfl::flexbuf::Reader::InputVarType _obj) {
|
||||
/// This only exists for the purpose of the test.
|
||||
const auto change_first_name = [](auto&& _person) {
|
||||
return rfl::replace(std::move(_person),
|
||||
rfl::Field<"firstName", std::string>("Bart"));
|
||||
};
|
||||
const auto from_nt = [](auto&& _nt) {
|
||||
return rfl::from_named_tuple<Person>(std::move(_nt));
|
||||
};
|
||||
return rfl::flexbuf::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt)
|
||||
.transform(change_first_name);
|
||||
}
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_flexbuf(
|
||||
typename rfl::flexbuf::Reader::InputVarType _obj) {
|
||||
/// This only exists for the purpose of the test.
|
||||
const auto change_first_name = [](auto&& _person) {
|
||||
return rfl::replace(std::move(_person),
|
||||
rfl::Field<"firstName", std::string>("Bart"));
|
||||
};
|
||||
const auto from_nt = [](auto&& _nt) {
|
||||
return rfl::from_named_tuple<Person>(std::move(_nt));
|
||||
};
|
||||
return rfl::flexbuf::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt)
|
||||
.transform(change_first_name);
|
||||
}
|
||||
|
||||
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<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto person1 = Person{
|
||||
.first_name = "Homer", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
@@ -53,3 +54,4 @@ void test_custom_constructor() {
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace test_custom_constructor
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_deque() {
|
||||
std::cout << "test_deque" << std::endl;
|
||||
namespace test_deque {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_deque
|
||||
|
||||
@@ -2,33 +2,37 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_field_variant() {
|
||||
std::cout << "test_field_variant" << std::endl;
|
||||
namespace test_field_variant {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r =
|
||||
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_field_variant
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_flatten_anonymous() {
|
||||
std::cout << "test_flatten_anonymous" << std::endl;
|
||||
namespace test_flatten_anonymous {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
@@ -31,3 +34,4 @@ void test_flatten_anonymous() {
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
} // namespace test_flatten_anonymous
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_forward_list() {
|
||||
std::cout << "test_forward_list" << std::endl;
|
||||
namespace test_forward_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_forward_list
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_list() {
|
||||
std::cout << "test_list" << std::endl;
|
||||
namespace test_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_list
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_literal() {
|
||||
std::cout << "test_literal" << std::endl;
|
||||
namespace test_literal {
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_literal
|
||||
|
||||
@@ -2,27 +2,31 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_map() {
|
||||
std::cout << "test_map" << std::endl;
|
||||
namespace test_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<int, Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_map
|
||||
|
||||
@@ -1,41 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_monster_example1() {
|
||||
std::cout << "test_monster_example1" << std::endl;
|
||||
namespace test_monster_example1 {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<std::uint8_t> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<std::uint8_t> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
@@ -59,3 +62,4 @@ void test_monster_example1() {
|
||||
write_and_read(orc);
|
||||
}
|
||||
|
||||
} // namespace test_monster_example1
|
||||
|
||||
@@ -1,41 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_monster_example2() {
|
||||
std::cout << "test_monster_example2" << std::endl;
|
||||
namespace test_monster_example2 {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", std::vector<Vec3>> path;
|
||||
};
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", std::vector<Vec3>> path;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
@@ -59,4 +62,4 @@ void test_monster_example2() {
|
||||
|
||||
write_and_read(orc);
|
||||
}
|
||||
|
||||
} // namespace test_monster_example2
|
||||
|
||||
@@ -2,27 +2,31 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multimap() {
|
||||
std::cout << "test_multimap" << std::endl;
|
||||
namespace test_multimap {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multimap<int, Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_multimap
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multiset() {
|
||||
std::cout << "test_multiset" << std::endl;
|
||||
namespace test_multiset {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multiset<std::string>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_multiset
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_optional_fields() {
|
||||
std::cout << "test_optional_fields" << std::endl;
|
||||
namespace test_optional_fields {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::optional<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::optional<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
@@ -28,3 +31,4 @@ void test_optional_fields() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_optional_fields
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_readme_example() {
|
||||
std::cout << "test_readme_example" << std::endl;
|
||||
namespace test_readme_example {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
@@ -52,3 +55,4 @@ void test_readme_example() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_readme_example
|
||||
|
||||
@@ -2,32 +2,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_ref() {
|
||||
std::cout << "test_ref" << std::endl;
|
||||
namespace test_ref {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
@@ -41,3 +42,4 @@ void test_ref() {
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_ref
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_save_load() {
|
||||
std::cout << "test_save_load" << std::endl;
|
||||
namespace test_save_load {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
@@ -66,3 +69,4 @@ void test_save_load() {
|
||||
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_save_load
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_set() {
|
||||
std::cout << "test_set" << std::endl;
|
||||
namespace test_set {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::set<std::string>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_set
|
||||
|
||||
@@ -3,18 +3,21 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_map() {
|
||||
std::cout << "test_string_map" << std::endl;
|
||||
namespace test_string_map {
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
std::map<std::string, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
std::map<std::string, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer);
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_string_map
|
||||
|
||||
@@ -2,24 +2,27 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_unordered_map() {
|
||||
std::cout << "test_string_unordered_map" << std::endl;
|
||||
namespace test_string_unordered_map {
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
|
||||
Map homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
|
||||
Map homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_string_unordered_map
|
||||
|
||||
@@ -2,30 +2,29 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_tagged_union() {
|
||||
std::cout << "test_tagged_union" << std::endl;
|
||||
namespace test_tagged_union {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"shape", rfl::Literal<"Rectangle">> shape = rfl::default_value;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
@@ -33,3 +32,4 @@ void test_tagged_union() {
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_tagged_union
|
||||
|
||||
@@ -2,20 +2,23 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unique_ptr() {
|
||||
std::cout << "test_unique_ptr" << std::endl;
|
||||
namespace test_unique_ptr {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::vector<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
@@ -27,3 +30,4 @@ void test_unique_ptr() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_unique_ptr
|
||||
|
||||
@@ -2,32 +2,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unique_ptr2() {
|
||||
std::cout << "test_unique_ptr2" << std::endl;
|
||||
namespace test_unique_ptr2 {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
|
||||
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
|
||||
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
@@ -41,3 +42,4 @@ void test_unique_ptr2() {
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_unique_ptr2
|
||||
|
||||
@@ -1,32 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_map() {
|
||||
std::cout << "test_unordered_map" << std::endl;
|
||||
namespace test_unordered_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_map
|
||||
|
||||
@@ -2,32 +2,35 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multimap() {
|
||||
std::cout << "test_unordered_multimap" << std::endl;
|
||||
namespace test_unordered_multimap {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children",
|
||||
std::unique_ptr<std::unordered_multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_multimap
|
||||
|
||||
@@ -1,31 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multiset() {
|
||||
std::cout << "test_unordered_multiset" << std::endl;
|
||||
namespace test_unordered_multiset {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children",
|
||||
std::unique_ptr<std::unordered_multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_multiset<std::string>>(
|
||||
std::unordered_multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_multiset<std::string>>(
|
||||
std::unordered_multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
// Unordered multisets are unpredictable. We therefore only make sure that
|
||||
// this compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered multisets are unpredictable. We therefore only make sure that
|
||||
// this compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_multiset
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_set() {
|
||||
std::cout << "test_unordered_set" << std::endl;
|
||||
namespace test_unordered_set {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_set<std::string>>(
|
||||
std::unordered_set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_set<std::string>>(
|
||||
std::unordered_set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
// Unordered sets are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered sets are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_set
|
||||
|
||||
@@ -2,27 +2,29 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_variant() {
|
||||
std::cout << "test_variant" << std::endl;
|
||||
namespace test_variant {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
|
||||
|
||||
@@ -30,3 +32,4 @@ void test_variant() {
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_variant
|
||||
|
||||
+33
-33
@@ -33,43 +33,43 @@
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
test_monster_example1();
|
||||
test_monster_example2();
|
||||
test_anonymous_fields();
|
||||
test_flatten_anonymous();
|
||||
test_optional_fields();
|
||||
test_unique_ptr();
|
||||
test_unique_ptr2();
|
||||
test_literal();
|
||||
test_variant();
|
||||
test_field_variant();
|
||||
test_tagged_union();
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_box();
|
||||
test_ref();
|
||||
test_readme_example::test();
|
||||
test_monster_example1::test();
|
||||
test_monster_example2::test();
|
||||
test_anonymous_fields::test();
|
||||
test_flatten_anonymous::test();
|
||||
test_optional_fields::test();
|
||||
test_unique_ptr::test();
|
||||
test_unique_ptr2::test();
|
||||
test_literal::test();
|
||||
test_variant::test();
|
||||
test_field_variant::test();
|
||||
test_tagged_union::test();
|
||||
test_custom_class1::test();
|
||||
test_custom_class2::test();
|
||||
test_custom_class3::test();
|
||||
test_box::test();
|
||||
test_ref::test();
|
||||
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
test_list();
|
||||
test_map();
|
||||
test_multimap();
|
||||
test_multiset();
|
||||
test_set();
|
||||
test_string_map();
|
||||
test_string_unordered_map();
|
||||
test_unordered_map();
|
||||
test_unordered_multimap();
|
||||
test_unordered_multiset();
|
||||
test_unordered_set();
|
||||
test_deque::test();
|
||||
test_forward_list::test();
|
||||
test_list::test();
|
||||
test_map::test();
|
||||
test_multimap::test();
|
||||
test_multiset::test();
|
||||
test_set::test();
|
||||
test_string_map::test();
|
||||
test_string_unordered_map::test();
|
||||
test_unordered_map::test();
|
||||
test_unordered_multimap::test();
|
||||
test_unordered_multiset::test();
|
||||
test_unordered_set::test();
|
||||
|
||||
test_custom_constructor();
|
||||
test_custom_constructor::test();
|
||||
|
||||
test_all_of();
|
||||
test_all_of::test();
|
||||
|
||||
test_save_load();
|
||||
test_save_load::test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_all_of() {
|
||||
std::cout << "test_all_of" << std::endl;
|
||||
namespace test_all_of {
|
||||
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
Age age;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
@@ -23,3 +26,4 @@ void test_all_of() {
|
||||
write_and_read(homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
|
||||
}
|
||||
} // namespace test_all_of
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_alphanumeric_map() {
|
||||
std::cout << "test_alphanumeric_map" << std::endl;
|
||||
namespace test_alphanumeric_map {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
std::map<rfl::AlphaNumeric, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(
|
||||
@@ -18,3 +21,5 @@ void test_alphanumeric_map() {
|
||||
|
||||
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
|
||||
}
|
||||
|
||||
} // namespace test_alphanumeric_map
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_alphanumeric_unordered_map() {
|
||||
std::cout << "test_alphanumeric_unordered_map" << std::endl;
|
||||
namespace test_alphanumeric_unordered_map {
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Map =
|
||||
std::unordered_map<rfl::AlphaNumeric, std::unique_ptr<std::string>>;
|
||||
@@ -24,3 +26,4 @@ void test_alphanumeric_unordered_map() {
|
||||
const auto homer2 = rfl::json::read<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_alphanumeric_unordered_map
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_anonymous_fields() {
|
||||
std::cout << "test_anonymous_fields" << std::endl;
|
||||
namespace test_anonymous_fields {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart",
|
||||
.last_name = "Simpson",
|
||||
@@ -49,5 +52,6 @@ void test_anonymous_fields() {
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"(["Homer","Simpson","1987-04-19",45,"homer@simpson.com",[["Bart","Simpson","1987-04-19",10,"bart@simpson.com",[]],["Lisa","Simpson","1987-04-19",8,"lisa@simpson.com",[]],["Maggie","Simpson","1987-04-19",0,"maggie@simpson.com",[]]]])");
|
||||
R"({"first_name":"Homer","last_name":"Simpson","birthday":"1987-04-19","age":45,"email":"homer@simpson.com","children":[{"first_name":"Bart","last_name":"Simpson","birthday":"1987-04-19","age":10,"email":"bart@simpson.com","children":[]},{"first_name":"Lisa","last_name":"Simpson","birthday":"1987-04-19","age":8,"email":"lisa@simpson.com","children":[]},{"first_name":"Maggie","last_name":"Simpson","birthday":"1987-04-19","age":0,"email":"maggie@simpson.com","children":[]}]})");
|
||||
}
|
||||
} // namespace test_anonymous_fields
|
||||
|
||||
+16
-11
@@ -1,24 +1,27 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_any_of() {
|
||||
std::cout << "test_any_of" << std::endl;
|
||||
namespace test_any_of {
|
||||
|
||||
using Age = rfl::Validator<
|
||||
unsigned int,
|
||||
rfl::AnyOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
|
||||
rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
|
||||
using Age =
|
||||
rfl::Validator<unsigned int,
|
||||
rfl::AnyOf<rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<10>>,
|
||||
rfl::AllOf<rfl::Minimum<40>, rfl::Maximum<130>>>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", Age> age;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
Age age;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
|
||||
@@ -26,3 +29,5 @@ void test_any_of() {
|
||||
write_and_read(homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
|
||||
}
|
||||
|
||||
} // namespace test_any_of
|
||||
|
||||
+24
-22
@@ -3,36 +3,38 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_array() {
|
||||
std::cout << "test_array" << std::endl;
|
||||
namespace test_array {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::array<Person, 3>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::array<Person, 3>> children = nullptr;
|
||||
};
|
||||
|
||||
auto bart = Person{.first_name = "Bart"};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto lisa = Person{.first_name = "Lisa"};
|
||||
auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
auto maggie = Person{.first_name = "Maggie"};
|
||||
auto lisa = Person{.first_name = "Lisa"};
|
||||
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.children =
|
||||
std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
|
||||
std::move(bart), std::move(lisa), std::move(maggie)})};
|
||||
auto maggie = Person{.first_name = "Maggie"};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
|
||||
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
|
||||
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
|
||||
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
|
||||
std::move(bart), std::move(lisa), std::move(maggie)})};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
"{\"firstName\":\"Homer\",\"lastName\":\"Simpson\",\"children\":[{"
|
||||
"\"firstName\":\"Bart\",\"lastName\":\"Simpson\"},{"
|
||||
"\"firstName\":\"Lisa\",\"lastName\":\"Simpson\"},{"
|
||||
"\"firstName\":\"Maggie\",\"lastName\":\"Simpson\"}]}");
|
||||
}
|
||||
} // namespace test_array
|
||||
|
||||
+24
-19
@@ -2,35 +2,40 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_as() {
|
||||
std::cout << "test_as" << std::endl;
|
||||
namespace test_as {
|
||||
|
||||
struct A {
|
||||
rfl::Field<"f1", std::string> f1;
|
||||
rfl::Field<"f2", rfl::Box<std::string>> f2;
|
||||
};
|
||||
struct A {
|
||||
rfl::Field<"f1", std::string> f1;
|
||||
rfl::Field<"f2", rfl::Box<std::string>> f2;
|
||||
};
|
||||
|
||||
struct B {
|
||||
rfl::Field<"f3", std::string> f3;
|
||||
rfl::Field<"f4", rfl::Box<std::string>> f4;
|
||||
};
|
||||
struct B {
|
||||
rfl::Field<"f3", std::string> f3;
|
||||
rfl::Field<"f4", rfl::Box<std::string>> f4;
|
||||
};
|
||||
|
||||
struct C {
|
||||
rfl::Field<"f1", std::string> f1;
|
||||
rfl::Field<"f2", rfl::Box<std::string>> f2;
|
||||
rfl::Field<"f4", rfl::Box<std::string>> f4;
|
||||
};
|
||||
struct C {
|
||||
rfl::Field<"f1", std::string> f1;
|
||||
rfl::Field<"f2", rfl::Box<std::string>> f2;
|
||||
rfl::Field<"f4", rfl::Box<std::string>> f4;
|
||||
};
|
||||
|
||||
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
|
||||
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
|
||||
|
||||
const auto c = rfl::as<C>(std::move(a), std::move(b));
|
||||
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
|
||||
|
||||
write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})");
|
||||
const auto c = rfl::as<C>(std::move(a), std::move(b));
|
||||
|
||||
write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})");
|
||||
}
|
||||
|
||||
} // namespace test_as
|
||||
|
||||
@@ -2,37 +2,41 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_as_flatten() {
|
||||
std::cout << "test_as_flatten" << std::endl;
|
||||
namespace test_as_flatten {
|
||||
|
||||
struct A {
|
||||
rfl::Field<"f1", std::string> f1;
|
||||
rfl::Field<"f2", rfl::Box<std::string>> f2;
|
||||
};
|
||||
struct A {
|
||||
rfl::Field<"f1", std::string> f1;
|
||||
rfl::Field<"f2", rfl::Box<std::string>> f2;
|
||||
};
|
||||
|
||||
struct B {
|
||||
rfl::Field<"f3", std::string> f3;
|
||||
rfl::Field<"f4", rfl::Box<std::string>> f4;
|
||||
};
|
||||
struct B {
|
||||
rfl::Field<"f3", std::string> f3;
|
||||
rfl::Field<"f4", rfl::Box<std::string>> f4;
|
||||
};
|
||||
|
||||
struct C {
|
||||
rfl::Flatten<A> a;
|
||||
rfl::Flatten<B> b;
|
||||
rfl::Field<"f5", int> f5;
|
||||
};
|
||||
struct C {
|
||||
rfl::Flatten<A> a;
|
||||
rfl::Flatten<B> b;
|
||||
rfl::Field<"f5", int> f5;
|
||||
};
|
||||
|
||||
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
|
||||
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
|
||||
|
||||
const auto c =
|
||||
rfl::as<C>(std::move(a), std::move(b), rfl::make_field<"f5">(5));
|
||||
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
|
||||
|
||||
write_and_read(
|
||||
c, R"({"f1":"Hello","f2":"World","f3":"Hello","f4":"World","f5":5})");
|
||||
const auto c =
|
||||
rfl::as<C>(std::move(a), std::move(b), rfl::make_field<"f5">(5));
|
||||
|
||||
write_and_read(
|
||||
c, R"({"f1":"Hello","f2":"World","f3":"Hello","f4":"World","f5":5})");
|
||||
}
|
||||
} // namespace test_as_flatten
|
||||
|
||||
+30
-26
@@ -2,44 +2,48 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_box() {
|
||||
std::cout << "test_box" << std::endl;
|
||||
namespace test_box {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
|
||||
};
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Box<DecisionTree> lesser;
|
||||
rfl::Box<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
write_and_read(
|
||||
tree,
|
||||
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(
|
||||
tree,
|
||||
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
|
||||
}
|
||||
} // namespace test_box
|
||||
|
||||
@@ -2,37 +2,40 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_custom_class1() {
|
||||
std::cout << "test_custom_class1" << std::endl;
|
||||
namespace test_custom_class1 {
|
||||
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
using ReflectionType = PersonImpl;
|
||||
using ReflectionType = PersonImpl;
|
||||
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
|
||||
const auto bart = Person("Bart");
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(
|
||||
bart, R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
|
||||
const auto bart = Person("Bart");
|
||||
|
||||
write_and_read(bart,
|
||||
R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
|
||||
}
|
||||
} // namespace test_custom_class1
|
||||
|
||||
@@ -2,41 +2,45 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_custom_class2() {
|
||||
std::cout << "test_custom_class2" << std::endl;
|
||||
namespace test_custom_class2 {
|
||||
|
||||
struct FiveDigitCode {
|
||||
using ReflectionType = int;
|
||||
struct FiveDigitCode {
|
||||
using ReflectionType = int;
|
||||
|
||||
FiveDigitCode(const int _code) : code(_code) {
|
||||
if (_code < 10000) {
|
||||
throw std::runtime_error("Less than five digits.");
|
||||
}
|
||||
if (_code > 99999) {
|
||||
throw std::runtime_error("More than five digits.");
|
||||
}
|
||||
}
|
||||
|
||||
ReflectionType reflection() const { return code; };
|
||||
|
||||
private:
|
||||
int code;
|
||||
};
|
||||
|
||||
const auto result = rfl::json::read<FiveDigitCode>("123");
|
||||
|
||||
if (result) {
|
||||
std::cout << "Failed: Expected an error, but got none." << std::endl;
|
||||
return;
|
||||
FiveDigitCode(const int _code) : code(_code) {
|
||||
if (_code < 10000) {
|
||||
throw std::runtime_error("Less than five digits.");
|
||||
}
|
||||
if (_code > 99999) {
|
||||
throw std::runtime_error("More than five digits.");
|
||||
}
|
||||
}
|
||||
|
||||
const auto c = FiveDigitCode(12345);
|
||||
ReflectionType reflection() const { return code; };
|
||||
|
||||
write_and_read(c, "12345");
|
||||
private:
|
||||
int code;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto result = rfl::json::read<FiveDigitCode>("123");
|
||||
|
||||
if (result) {
|
||||
std::cout << "Failed: Expected an error, but got none." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
const auto c = FiveDigitCode(12345);
|
||||
|
||||
write_and_read(c, "12345");
|
||||
}
|
||||
} // namespace test_custom_class2
|
||||
|
||||
@@ -2,62 +2,65 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc3 {
|
||||
namespace test_custom_class3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
const int _age)
|
||||
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
const int _age)
|
||||
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
std::string last_name_;
|
||||
int age_;
|
||||
private:
|
||||
std::string first_name_;
|
||||
std::string last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = _p.last_name(),
|
||||
.age = _p.age()};
|
||||
}
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = _p.last_name(),
|
||||
.age = _p.age()};
|
||||
}
|
||||
|
||||
Person to_class() const { return Person(first_name(), last_name(), age()); }
|
||||
Person to_class() const { return Person(first_name(), last_name(), age); }
|
||||
};
|
||||
|
||||
} // namespace tcc3
|
||||
} // namespace test_custom_class3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc3::Person,
|
||||
tcc3::PersonImpl> {};
|
||||
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
|
||||
test_custom_class3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class3() {
|
||||
std::cout << "test_custom_class3" << std::endl;
|
||||
namespace test_custom_class3 {
|
||||
|
||||
const auto bart = tcc3::Person("Bart", "Simpson", 10);
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(bart,
|
||||
R"({"firstName":"Bart","lastName":"Simpson","age":10})");
|
||||
const auto bart = Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})");
|
||||
}
|
||||
|
||||
} // namespace test_custom_class3
|
||||
|
||||
@@ -2,64 +2,66 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc4 {
|
||||
namespace test_custom_class4 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
const rfl::Box<std::string>& _last_name, int _age)
|
||||
: first_name_(_first_name),
|
||||
last_name_(rfl::make_box<std::string>(*_last_name)),
|
||||
age_(_age) {}
|
||||
Person(const std::string& _first_name,
|
||||
const rfl::Box<std::string>& _last_name, int _age)
|
||||
: first_name_(_first_name),
|
||||
last_name_(rfl::make_box<std::string>(*_last_name)),
|
||||
age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
rfl::Box<std::string> last_name_;
|
||||
int age_;
|
||||
private:
|
||||
std::string first_name_;
|
||||
rfl::Box<std::string> last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{
|
||||
.first_name = _p.first_name(),
|
||||
.last_name = rfl::make_box<std::string>(*_p.last_name()),
|
||||
.age = _p.age()};
|
||||
}
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = rfl::make_box<std::string>(*_p.last_name()),
|
||||
.age = _p.age()};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace tcc4
|
||||
} // namespace test_custom_class4
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc4::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc4::Person,
|
||||
tcc4::PersonImpl> {};
|
||||
struct Parser<ReaderType, WriterType, test_custom_class4::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class4::Person,
|
||||
test_custom_class4::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class4() {
|
||||
std::cout << "test_custom_class4" << std::endl;
|
||||
namespace test_custom_class4 {
|
||||
|
||||
const auto bart =
|
||||
tcc4::Person("Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(bart,
|
||||
R"({"firstName":"Bart","lastName":"Simpson","age":10})");
|
||||
const auto bart = test_custom_class4::Person(
|
||||
"Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart, R"({"firstName":"Bart","lastName":"Simpson","age":10})");
|
||||
}
|
||||
} // namespace test_custom_class4
|
||||
|
||||
@@ -1,52 +1,53 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that the example in the README works as expected.
|
||||
// It would be embarrassing if it didn't...
|
||||
void test_custom_constructor() {
|
||||
std::cout << "test_custom_constructor" << std::endl;
|
||||
namespace test_custom_constructor {
|
||||
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_json_obj(
|
||||
typename rfl::json::Reader::InputVarType _obj) {
|
||||
/// This only exists for the purpose of the test.
|
||||
const auto change_first_name = [](auto&& _person) {
|
||||
return rfl::replace(
|
||||
std::move(_person),
|
||||
rfl::Field<"firstName", std::string>("Bart"));
|
||||
};
|
||||
const auto from_nt = [](auto&& _nt) {
|
||||
return rfl::from_named_tuple<Person>(std::move(_nt));
|
||||
};
|
||||
return rfl::json::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt)
|
||||
.transform(change_first_name);
|
||||
}
|
||||
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_json_obj(
|
||||
typename rfl::json::Reader::InputVarType _obj) {
|
||||
/// This only exists for the purpose of the test.
|
||||
const auto change_first_name = [](auto&& _person) {
|
||||
return rfl::replace(std::move(_person),
|
||||
rfl::Field<"firstName", std::string>("Bart"));
|
||||
};
|
||||
const auto from_nt = [](auto&& _nt) {
|
||||
return rfl::from_named_tuple<Person>(std::move(_nt));
|
||||
};
|
||||
return rfl::json::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt)
|
||||
.transform(change_first_name);
|
||||
}
|
||||
|
||||
const auto res = rfl::json::read<Person>(
|
||||
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})");
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
|
||||
};
|
||||
|
||||
if (!res) {
|
||||
std::cout << "Test failed on read. Error: "
|
||||
<< res.error().value().what() << std::endl;
|
||||
return;
|
||||
}
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
if (res.value().first_name() != "Bart") {
|
||||
std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'"
|
||||
<< std::endl
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
const auto res = rfl::json::read<Person>(
|
||||
R"({"firstName":"Homer","lastName":"Simpson","birthday":"1987-04-19"})");
|
||||
|
||||
if (!res) {
|
||||
std::cout << "Test failed on read. Error: " << res.error().value().what()
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.value().first_name() != "Bart") {
|
||||
std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'"
|
||||
<< std::endl
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace test_custom_constructor
|
||||
|
||||
@@ -2,30 +2,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_default_values() {
|
||||
std::cout << "test_default_values" << std::endl;
|
||||
namespace test_default_values {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})");
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":[]},{"firstName":"Lisa","lastName":"Simpson","children":[]},{"firstName":"Maggie","lastName":"Simpson","children":[]}]})");
|
||||
}
|
||||
} // namespace test_default_values
|
||||
|
||||
+20
-17
@@ -1,30 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_deque() {
|
||||
std::cout << "test_deque" << std::endl;
|
||||
namespace test_deque {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::deque<Person>> children;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
}
|
||||
} // namespace test_deque
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_email() {
|
||||
std::cout << "test_email" << std::endl;
|
||||
namespace test_email {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Email email;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer = Person{.first_name = "Homer",
|
||||
.last_name = "Simpson",
|
||||
@@ -23,3 +26,4 @@ void test_email() {
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","email":"homer@simpson.com"})");
|
||||
}
|
||||
} // namespace test_email
|
||||
|
||||
@@ -1,42 +1,46 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_error_messages() {
|
||||
std::cout << "test_error_messages" << std::endl;
|
||||
namespace test_error_messages {
|
||||
|
||||
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;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
const std::string faulty_string =
|
||||
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto result = rfl::json::read<Person>(faulty_string);
|
||||
const std::string faulty_string =
|
||||
R"({"firstName":"Homer","lastName":12345,"birthday":"04/19/1987"})";
|
||||
|
||||
const std::string expected = R"(Found 3 errors:
|
||||
const auto result = rfl::json::read<Person>(faulty_string);
|
||||
|
||||
const std::string expected = R"(Found 3 errors:
|
||||
1) Failed to parse field 'lastName': Could not cast to string.
|
||||
2) Failed to parse field 'birthday': String '04/19/1987' did not match format '%Y-%m-%d'.
|
||||
3) Field named 'children' not found.)";
|
||||
|
||||
if (!result.error()) {
|
||||
std::cout << "Expected an error, got none." << std::endl << std::endl;
|
||||
}
|
||||
if (!result.error()) {
|
||||
std::cout << "Expected an error, got none." << std::endl << std::endl;
|
||||
}
|
||||
|
||||
if (result.error().value().what() != expected) {
|
||||
std::cout << "Expected: " << std::endl
|
||||
<< expected << std::endl
|
||||
<< "Got:" << std::endl
|
||||
<< result.error().value().what() << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
if (result.error().value().what() != expected) {
|
||||
std::cout << "Expected: " << std::endl
|
||||
<< expected << std::endl
|
||||
<< "Got:" << std::endl
|
||||
<< result.error().value().what() << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_error_messages
|
||||
|
||||
@@ -2,33 +2,37 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_field_variant() {
|
||||
std::cout << "test_field_variant" << std::endl;
|
||||
namespace test_field_variant {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r =
|
||||
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
|
||||
|
||||
write_and_read(r, R"({"rectangle":{"height":10.0,"width":5.0}})");
|
||||
}
|
||||
} // namespace test_field_variant
|
||||
|
||||
+25
-21
@@ -2,34 +2,38 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_flatten() {
|
||||
std::cout << "test_flatten" << std::endl;
|
||||
namespace test_flatten {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Field<"employer", rfl::Box<std::string>> employer;
|
||||
rfl::Field<"salary", float> salary;
|
||||
};
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Field<"employer", rfl::Box<std::string>> employer;
|
||||
rfl::Field<"salary", float> salary;
|
||||
};
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(
|
||||
employee,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(
|
||||
employee,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
|
||||
}
|
||||
} // namespace test_flatten
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_flatten_anonymous() {
|
||||
std::cout << "test_flatten_anonymous" << std::endl;
|
||||
namespace test_flatten_anonymous {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
@@ -29,5 +32,9 @@ void test_flatten_anonymous() {
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee, R"(["Homer","Simpson",45,"Mr. Burns",60000.0])");
|
||||
write_and_read(
|
||||
employee,
|
||||
R"({"first_name":"Homer","last_name":"Simpson","age":45,"employer":"Mr. Burns","salary":60000.0})");
|
||||
}
|
||||
|
||||
} // namespace test_flatten_anonymous
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_flatten_tuple() {
|
||||
std::cout << "test_flatten_tuple" << std::endl;
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Person = std::tuple<std::string, rfl::Box<std::string>, int>;
|
||||
|
||||
|
||||
@@ -1,30 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_forward_list() {
|
||||
std::cout << "test_forward_list" << std::endl;
|
||||
namespace test_forward_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::forward_list<Person>> children;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
}
|
||||
} // namespace test_forward_list
|
||||
|
||||
+20
-17
@@ -1,30 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_list() {
|
||||
std::cout << "test_list" << std::endl;
|
||||
namespace test_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::list<Person>> children;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})");
|
||||
}
|
||||
} // namespace test_list
|
||||
|
||||
+16
-14
@@ -2,27 +2,29 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_literal() {
|
||||
std::cout << "test_literal" << std::endl;
|
||||
namespace test_literal {
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", FirstName> first_name;
|
||||
rfl::Rename<"lastName", LastName> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(
|
||||
bart, R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
|
||||
write_and_read(bart,
|
||||
R"({"firstName":"Bart","lastName":"Simpson","children":[]})");
|
||||
}
|
||||
} // namespace test_literal
|
||||
|
||||
@@ -3,15 +3,18 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_literal_map() {
|
||||
std::cout << "test_literal_map" << std::endl;
|
||||
namespace test_literal_map {
|
||||
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
std::map<FieldName, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(std::make_pair(FieldName::make<"firstName">(),
|
||||
@@ -21,3 +24,4 @@ void test_literal_map() {
|
||||
|
||||
write_and_read(homer, R"({"firstName":"Homer","lastName":"Simpson"})");
|
||||
}
|
||||
} // namespace test_literal_map
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_literal_unordered_map() {
|
||||
std::cout << "test_literal_unordered_map" << std::endl;
|
||||
namespace test_literal_unordered_map {
|
||||
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
|
||||
using Map = std::unordered_map<FieldName, std::unique_ptr<std::string>>;
|
||||
using Map = std::unordered_map<FieldName, std::unique_ptr<std::string>>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
Map homer;
|
||||
|
||||
@@ -29,3 +32,4 @@ void test_literal_unordered_map() {
|
||||
const auto homer2 = rfl::json::read<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_literal_unordered_map
|
||||
|
||||
+11
-8
@@ -2,19 +2,21 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_map() {
|
||||
std::cout << "test_map" << std::endl;
|
||||
namespace test_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<int, Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::map<int, Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
@@ -28,3 +30,4 @@ void test_map() {
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})");
|
||||
}
|
||||
} // namespace test_map
|
||||
|
||||
@@ -2,21 +2,23 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_map_with_key_validation() {
|
||||
std::cout << "test_map_with_key_validation" << std::endl;
|
||||
namespace test_map_with_key_validation {
|
||||
|
||||
using Between1and3 = rfl::Validator<int, rfl::Minimum<1>, rfl::Maximum<3>>;
|
||||
using Between1and3 = rfl::Validator<int, rfl::Minimum<1>, rfl::Maximum<3>>;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<Between1and3, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::map<Between1and3, Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::map<Between1and3, Person>>();
|
||||
|
||||
@@ -31,3 +33,4 @@ void test_map_with_key_validation() {
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":{"1":{"firstName":"Bart","lastName":"Simpson"},"2":{"firstName":"Lisa","lastName":"Simpson"},"3":{"firstName":"Maggie","lastName":"Simpson"}}})");
|
||||
}
|
||||
} // namespace test_map_with_key_validation
|
||||
|
||||
@@ -1,41 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_monster_example() {
|
||||
std::cout << "test_monster_example" << std::endl;
|
||||
namespace test_monster_example {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<std::uint8_t> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<std::uint8_t> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto sword = Weapon{.name = "Sword", .damage = 3};
|
||||
const auto axe = Weapon{.name = "Axe", .damage = 5};
|
||||
@@ -58,6 +61,6 @@ void test_monster_example() {
|
||||
|
||||
write_and_read(
|
||||
orc,
|
||||
R"([[1.0,2.0,3.0],150,80,"MyMonster",false,[0,1,2,3,4,5,6,7,8,9],"Red",[["Sword",3],["Axe",5]],{"weapon":["Axe",5]},[]])");
|
||||
R"({"pos":{"x":1.0,"y":2.0,"z":3.0},"mana":150,"hp":80,"name":"MyMonster","friendly":false,"inventory":[0,1,2,3,4,5,6,7,8,9],"color":"Red","weapons":[{"name":"Sword","damage":3},{"name":"Axe","damage":5}],"equipped":{"weapon":{"name":"Axe","damage":5}},"path":[]})");
|
||||
}
|
||||
|
||||
} // namespace test_monster_example
|
||||
|
||||
@@ -2,27 +2,31 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_move_replace() {
|
||||
std::cout << "test_move_replace" << std::endl;
|
||||
namespace test_move_replace {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"children", rfl::Box<std::vector<Person>>> children;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Box<std::vector<Person>> children;
|
||||
};
|
||||
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::make_box<std::vector<Person>>()};
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto maggie =
|
||||
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
|
||||
const auto lisa = Person{.first_name = "Lisa",
|
||||
.last_name = "Simpson",
|
||||
.children = rfl::make_box<std::vector<Person>>()};
|
||||
|
||||
write_and_read(
|
||||
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
|
||||
const auto maggie =
|
||||
rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")));
|
||||
|
||||
write_and_read(
|
||||
maggie, R"({"firstName":"Maggie","lastName":"Simpson","children":[]})");
|
||||
}
|
||||
} // namespace test_move_replace
|
||||
|
||||
@@ -2,29 +2,32 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multimap() {
|
||||
std::cout << "test_multimap" << std::endl;
|
||||
namespace test_multimap {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::multimap<int, Person>> children;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})");
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})");
|
||||
}
|
||||
} // namespace test_multimap
|
||||
|
||||
@@ -1,28 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multiset() {
|
||||
std::cout << "test_multiset" << std::endl;
|
||||
namespace test_multiset {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::multiset<std::string>> children;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
|
||||
}
|
||||
} // namespace test_multiset
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user