From 548fefe38b665e1ed26a3fb99e64ad5afccfdda7 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 22 Oct 2023 19:06:43 +0200 Subject: [PATCH 1/6] Create supporting_your_own_format.md --- docs/supporting_your_own_format.md | 99 ++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 docs/supporting_your_own_format.md diff --git a/docs/supporting_your_own_format.md b/docs/supporting_your_own_format.md new file mode 100644 index 00000000..7efd131f --- /dev/null +++ b/docs/supporting_your_own_format.md @@ -0,0 +1,99 @@ +# Supporting your own format + +In order to support your own serialization format, you need to implement a reader and a writer for your concept. + +The reader and writer constitute a thin layer between the serialization library of your choice and reflect-cpp. + +The requirements are laid down in the concepts [IsReader](https://github.com/getml/reflect-cpp/blob/main/include/rfl/parsing/IsReader.hpp) and +[IsWriter](https://github.com/getml/reflect-cpp/blob/main/include/rfl/parsing/IsWriter.hpp) and also documented below. + +Using the reader and the writer you can define your parser like this: + +```cpp +template +using YourParser = rfl::parsing::Parser; +``` + +This can then be used to implement a `read` function and a `write` function: + +```cpp +using InputVarType = typename YourReader::InputVarType; +using OutputVarType = typename YourWriter::OutputVarType; + +template +rfl::Result read(const std::string& _str) { + // This should be supported by whatever library you are + // using for your format. + const InputVarType root = str_to_input_var(_str); + + // You can pass variables to the constructor, if necessary + const auto r = Reader(...); + + return YourParser::read(r, root); +} + +template +std::string write(const T& _obj) { + // You can pass variables to the constructor, if necessary + auto w = Writer(...); + + OutputVarType var = Parser::write(w, _obj); + + // This should be supported by whatever library you are + // using for your format. + return output_var_to_str(var); +} +``` + +In the following two sections, we will provide templates for your `Reader` and `Writer`. +You should probably just Copy + Paste this into your own code and fill in the blanks. + +## Implementing your own writer + +Because writers are somewhat simpler, we will start with them. + +Any Writer needs to define the following types: + +1) An `OutputArrayType`, which must be an array-like data structure. +2) An `OutputObjectType`, which must contain key-value pairs. +3) An `OutputVarType`, which must be able to represent either + OutputArrayType, OutputObjectType or a basic type (bool, integral, + floating point, std::string). We hesitate to call these "primitive types", + because primitive types in C++ are defined as a slightly different group + of types. + +It also needs to support the following methods: + +```cpp +struct Writer { + using OutputArrayType = ...; + using OutputObjectType = ...; + using OutputVarType = ...; + + /// Appends `_var` to the end of `_arr`, thus mutating it. + void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {...} + + /// Returns an empty OutputVarType, the NULL type of the format. + OutputVarType empty_var() const noexcept {...} + + /// Generates an OutputVarType from a basic type + /// (std::string, bool, floating point or integral). + template + OutputVarType from_basic_type(const T& _var) const noexcept {...} + + /// Generates a new, empty array. + OutputArrayType new_array() const noexcept {...} + + /// Generates a new, empty object. + OutputObjectType new_object() const noexcept {...} + + /// Determines whether the var is empty (whether it is the NULL type). + bool is_empty(const OutputVarType& _var) const noexcept {...} + + /// Adds a new field to obj, thus mutating it. + void set_field(const std::string& _name, const OutputVarType& _var, + OutputObjectType* _obj) const noexcept {...} +}; +``` + +## Implementing your own reader From 882abbc7896a187a8fb21d797d292b88ea435df3 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 22 Oct 2023 19:42:39 +0200 Subject: [PATCH 2/6] Added documentation for the custom reader --- docs/supporting_your_own_format.md | 120 ++++++++++++++++++++++++++++- 1 file changed, 119 insertions(+), 1 deletion(-) diff --git a/docs/supporting_your_own_format.md b/docs/supporting_your_own_format.md index 7efd131f..983dad33 100644 --- a/docs/supporting_your_own_format.md +++ b/docs/supporting_your_own_format.md @@ -1,6 +1,7 @@ # Supporting your own format -In order to support your own serialization format, you need to implement a reader and a writer for your concept. +In order to support your own serialization format, you need to implement a reader and a writer on top of whatever serialization libary +you have chosen. The reader and writer constitute a thin layer between the serialization library of your choice and reflect-cpp. @@ -97,3 +98,120 @@ struct Writer { ``` ## Implementing your own reader + +Any Reader needs to define the following: + +1) An `InputArrayType`, which must be an array-like data structure. +2) An `InputObjectType`, which must contain key-value pairs. +3) An `InputVarType`, which must be able to represent either + `InputArrayType`, `InputObjectType` or a basic type (bool, integral, + floating point, std::string). +4) A `static constexpr bool has_custom_constructor`, that determines + whether the class in question as a custom constructor, which might + be called something like `from_json_obj(...)`. If you do not want to + support this functionality, just set it to false. + +It also needs to support the following methods: + +```cpp +struct Reader { + using InputArrayType = ...; + using InputObjectType = ...; + using InputVarType = ...; + + /// If you do not want to support custom constructors, + /// just set this to false. + template + static constexpr bool has_custom_constructor = false; + + /// Retrieves a particular field from an object. + /// Returns an rfl::Error if the field cannot be found. + rfl::Result get_field( + const std::string& _name, const InputObjectType& _obj) const noexcept {...} + + /// Determines whether a variable is empty (the NULL type). + bool is_empty(const InputVarType& _var) const noexcept {...} + + /// Cast _var as a basic type (bool, integral, + /// floating point, std::string). + /// Returns an rfl::Error if it cannot be cast + /// as that type + template + rfl::Result to_basic_type(const InputVarType& _var) const noexcept {...} + + /// Casts _var as an InputArrayType. + /// Returns an rfl::Error if `_var` cannot be cast as an array. + rfl::Result to_array(const InputVarType& _var) const noexcept {...} + + /// fct is a function that turns the field name into the field index of the + /// struct. It returns -1, if the fields does not exist on the struct. This + /// returns an std::array that can be used to build up the struct. + /// See below for a more comprehensive explanation. + template + std::array to_fields_array( + const FunctionType _fct, const InputObjectType& _obj) const noexcept {...} + + /// Iterates through an object and writes the contained key-value pairs into + /// a vector. + std::vector> to_map( + const InputObjectType& _obj) const noexcept {...} + + /// Casts _var as an InputObjectType. + /// Returns an rfl::Error if `_var` cannot be cast as an object. + rfl::Result to_object( + const InputVarType& _var) const noexcept {...} + + /// Iterates through an array and writes the contained values into + /// a std::vector. + std::vector to_vec(const InputArrayType& _arr) const noexcept {...} + + /// Constructs T using its custom constructor. This will only be triggered if + /// T was determined to have a custom constructor by + /// static constexpr bool has_custom_constructor, as defined above. + /// Returns an rfl::Error, if the custom constructor throws an exception. + template + rfl::Result use_custom_constructor( + const InputVarType& _var) const noexcept { + // If you do not want to support this functionality, + // just return this. + return rfl::Error("Not supported."); + } +}; +``` + +Of these methods, `to_fields_array` probably requires futher explanation. + +Consider the following struct: + +```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> children; +}; +``` + +This struct contains four fields. `rfl::parsing::Parser` expects `to_fields_array` +to return an `std::array` containing the field "firstName" in the +first position, "lastName" in the second position, "birthday" in the third position +and "children" in the fourth position. + +`rfl::parsing::Parser` will pass the following two elements: + +1. A function `_fct` of type `FunctionType`. +2. An object `_obj` of type `InputObjectType`. + +`_fct` takes a field name of type `std::string_view` as an input and +returns an index. This index signifies the position in the `std::array` +it wants this field to have. If the field is not contained in the struct, +the index will be -1. + +Your job is to implement the following: + +1. Iterate through `_obj`. +2. Identify the required index of the field name using `_fct`. +3. Set the corresponding field in `std::array` to the field value associated with the field name. +4. Any field that could not be set in steps 1-3 must be set to the NULL value, + such that `Reader.is_empty(...)` would return `true`. + From 96599faaf2420975682237199d8b5b1779d496b1 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 22 Oct 2023 19:44:21 +0200 Subject: [PATCH 3/6] Update README.md --- docs/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/README.md b/docs/README.md index bb1f70ec..2d6e57ee 100644 --- a/docs/README.md +++ b/docs/README.md @@ -40,3 +40,7 @@ 4.1) [JSON](https://github.com/getml/reflect-cpp/blob/main/docs/json.md) +## 5) Advanced topics + +5.1) [Supporting your own format](https://github.com/getml/reflect-cpp/blob/main/docs/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats. + From 6bf12bedb4db69f09e81e59d092702f8b227cda3 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 22 Oct 2023 19:45:21 +0200 Subject: [PATCH 4/6] Update supporting_your_own_format.md --- docs/supporting_your_own_format.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/supporting_your_own_format.md b/docs/supporting_your_own_format.md index 983dad33..cecaeb9e 100644 --- a/docs/supporting_your_own_format.md +++ b/docs/supporting_your_own_format.md @@ -47,7 +47,7 @@ std::string write(const T& _obj) { ``` In the following two sections, we will provide templates for your `Reader` and `Writer`. -You should probably just Copy + Paste this into your own code and fill in the blanks. +You should probably just copy + paste this into your own code and fill in the blanks. ## Implementing your own writer From 99358eea8239314c3cf78319ce8d516ec2c415ea Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 22 Oct 2023 19:49:00 +0200 Subject: [PATCH 5/6] Update supporting_your_own_format.md --- docs/supporting_your_own_format.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/supporting_your_own_format.md b/docs/supporting_your_own_format.md index cecaeb9e..ad1230b2 100644 --- a/docs/supporting_your_own_format.md +++ b/docs/supporting_your_own_format.md @@ -58,7 +58,7 @@ Any Writer needs to define the following types: 1) An `OutputArrayType`, which must be an array-like data structure. 2) An `OutputObjectType`, which must contain key-value pairs. 3) An `OutputVarType`, which must be able to represent either - OutputArrayType, OutputObjectType or a basic type (bool, integral, + `OutputArrayType`, `OutputObjectType` or a basic type (bool, integral, floating point, std::string). We hesitate to call these "primitive types", because primitive types in C++ are defined as a slightly different group of types. From 6e38653a20a16463baac5a828bc9bd3009c2d472 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Mon, 23 Oct 2023 18:48:59 +0200 Subject: [PATCH 6/6] Added a reference to the JSON implementation --- docs/supporting_your_own_format.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/supporting_your_own_format.md b/docs/supporting_your_own_format.md index ad1230b2..1b7d7537 100644 --- a/docs/supporting_your_own_format.md +++ b/docs/supporting_your_own_format.md @@ -49,6 +49,8 @@ std::string write(const T& _obj) { In the following two sections, we will provide templates for your `Reader` and `Writer`. You should probably just copy + paste this into your own code and fill in the blanks. +As a reference, you can take a look at how this is done for JSON: https://github.com/getml/reflect-cpp/tree/main/include/rfl/json + ## Implementing your own writer Because writers are somewhat simpler, we will start with them.