diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index 1e43f489..e4f7e527 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -33,6 +33,7 @@ struct Reader { }; struct YYJSONInputVar { + YYJSONInputVar() : val_(nullptr) {} YYJSONInputVar(yyjson_val* _val) : val_(_val) {} yyjson_val* val_; }; @@ -64,8 +65,8 @@ struct Reader { return var; } - bool is_empty(InputVarType* _var) const noexcept { - return !_var->val_ || yyjson_is_null(_var->val_); + bool is_empty(const InputVarType& _var) const noexcept { + return !_var.val_ || yyjson_is_null(_var.val_); } template @@ -103,19 +104,18 @@ struct Reader { return InputArrayType(_var->val_); } - template - std::array, size> to_fields_array( - const std::unordered_map& _field_indices, - InputObjectType* _obj) const noexcept { - std::array, size> f_arr; + template + std::array to_fields_array( + const FunctionType _fct, InputObjectType* _obj) const noexcept { + std::array f_arr; yyjson_obj_iter iter; yyjson_obj_iter_init(_obj->val_, &iter); yyjson_val* key; while ((key = yyjson_obj_iter_next(&iter))) { const char* k = yyjson_get_str(key); - const auto it = _field_indices.find(std::string_view(k)); - if (it != _field_indices.end()) { - f_arr[it->second] = InputVarType(yyjson_obj_iter_get_val(key)); + const auto ix = _fct(std::string_view(k)); + if (ix != -1) { + f_arr[ix] = InputVarType(yyjson_obj_iter_get_val(key)); } } return f_arr; diff --git a/include/rfl/json/Writer.hpp b/include/rfl/json/Writer.hpp index bd26e0fe..f628b5d2 100644 --- a/include/rfl/json/Writer.hpp +++ b/include/rfl/json/Writer.hpp @@ -13,6 +13,7 @@ #include #include "rfl/Result.hpp" +#include "rfl/always_false.hpp" namespace rfl { namespace json { @@ -55,7 +56,7 @@ struct Writer { } template - OutputVarType from_basic_type(const T _var) const noexcept { + OutputVarType from_basic_type(const T& _var) const noexcept { if constexpr (std::is_same, std::string>()) { return OutputVarType(yyjson_mut_strcpy(doc_, _var.c_str())); } else if constexpr (std::is_same, bool>()) { @@ -63,8 +64,10 @@ struct Writer { } else if constexpr (std::is_floating_point>()) { return OutputVarType( yyjson_mut_real(doc_, static_cast(_var))); - } else { + } else if constexpr (std::is_integral>()) { return OutputVarType(yyjson_mut_int(doc_, static_cast(_var))); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); } } @@ -76,8 +79,8 @@ struct Writer { return OutputObjectType(yyjson_mut_obj(doc_)); } - bool is_empty(OutputVarType* _var) const noexcept { - return yyjson_mut_is_null(_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, diff --git a/include/rfl/parsing/AreReaderAndWriter.hpp b/include/rfl/parsing/AreReaderAndWriter.hpp index 962f6390..70648a3c 100644 --- a/include/rfl/parsing/AreReaderAndWriter.hpp +++ b/include/rfl/parsing/AreReaderAndWriter.hpp @@ -8,7 +8,7 @@ namespace rfl { namespace parsing { template -concept AreReaderAndWriter = IsReader && IsWriter; +concept AreReaderAndWriter = IsReader && IsWriter; } // namespace parsing } // namespace rfl diff --git a/include/rfl/parsing/IsReader.hpp b/include/rfl/parsing/IsReader.hpp index 99c1fcbd..ce95d882 100644 --- a/include/rfl/parsing/IsReader.hpp +++ b/include/rfl/parsing/IsReader.hpp @@ -1,8 +1,12 @@ #ifndef RFL_PARSING_ISREADER_HPP_ #define RFL_PARSING_ISREADER_HPP_ +#include #include -#include +#include +#include +#include +#include #include "rfl/Result.hpp" @@ -10,26 +14,64 @@ namespace rfl { namespace parsing { template -concept IsReader = requires(R r, typename R::InputArrayType arr, +concept IsReader = requires(R r, std::string name, + std::function fct, + typename R::InputArrayType arr, typename R::InputObjectType obj, typename R::InputVarType var) { + /// 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(...). + + /// Retrieves a particular field from an object. + { + r.get_field(name, &obj) + } -> std::same_as>; + + /// Determines whether a variable is empty (the NULL type). + { r.is_empty(var) } -> std::same_as; + + /// Transforms var to a basic type (bool, integral, + /// floating point, std::string) { r.template to_basic_type(&var) } -> std::same_as>; + /// 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. + { + r.template to_fields_array<6>(fct, &obj) + } -> std::same_as>; + + /// Casts var as an InputArrayType. { r.to_array(&var) } -> std::same_as>; + /// Iterates through an object and writes the contained key-value pairs into + /// a vector. { r.to_map(&obj) } -> std::same_as< std::vector>>; + /// Casts var as an InputObjectType. { r.to_object(&var) } -> std::same_as>; + /// Iterates through an array and writes the contained vars into + /// a vector. { r.to_vec(&arr) } -> std::same_as>; + /// Uses the custom constructor, if it has been determined that T has one + /// (see above). { r.template use_custom_constructor(&var) } -> std::same_as>; diff --git a/include/rfl/parsing/IsWriter.hpp b/include/rfl/parsing/IsWriter.hpp index 11c89a2f..351f114f 100644 --- a/include/rfl/parsing/IsWriter.hpp +++ b/include/rfl/parsing/IsWriter.hpp @@ -8,21 +8,39 @@ namespace rfl { namespace parsing { -template -concept IsWriter = requires(W w, std::string name, +template +concept IsWriter = requires(W w, T t, std::string name, typename W::OutputArrayType arr, typename W::OutputObjectType obj, typename W::OutputVarType var) { + /// Any Writer needs to define the following: + /// + /// 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). + + /// Appends var to the end of arr, thus mutating it. { w.add(var, &arr) } -> std::same_as; + /// Returns an empty var (usually the NULL type). { w.empty_var() } -> std::same_as; + /// Generates a var from a basic type (std::string, bool, floating point or + /// integral). + { w.from_basic_type(t) } -> std::same_as; + + /// Generates a new, empty array. { w.new_array() } -> std::same_as; + /// Generates a new, empty object. { w.new_object() } -> std::same_as; - { w.is_empty(&var) } -> std::same_as; + /// Determines whether the var is empty (the NULL type). + { w.is_empty(var) } -> std::same_as; + /// Adds a new field to obj, thus mutating it. { w.set_field(name, var, &obj) } -> std::same_as; }; diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index cf28477a..fc2a9da3 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -346,9 +347,15 @@ struct Parser> { /// Generates a NamedTuple from a JSON Object. static Result> read(const R& _r, 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(-1); + }; const auto to_fields_array = [&](auto _obj) { - return _r.template to_fields_array( - field_indices(), &_obj); + return _r.template to_fields_array(fct, + &_obj); }; const auto build = [&](auto _fields_vec) { return build_named_tuple_recursively(_r, _fields_vec); @@ -369,8 +376,7 @@ struct Parser> { template static Result> build_named_tuple_recursively( const R& _r, - const std::array, sizeof...(FieldTypes)>& - _fields_vec, + const std::array& _fields_vec, Args&&... _args) noexcept { constexpr auto i = sizeof...(Args); @@ -384,7 +390,7 @@ struct Parser> { const auto& f = std::get(_fields_vec); - if (!f) { + if (_r.is_empty(f)) { if constexpr (is_required()) { const auto key = FieldType::name_.str(); return collect_errors( @@ -409,7 +415,7 @@ struct Parser> { _r, _fields_vec, std::vector({std::move(_error)})); }; - return get_value(_r, *f) + return get_value(_r, f) .or_else(handle_error) .and_then(build); } @@ -420,8 +426,7 @@ struct Parser> { template static Error collect_errors( const R& _r, - const std::array, sizeof...(FieldTypes)>& - _fields_vec, + const std::array& _fields_vec, std::vector _errors) noexcept { if constexpr (_i == sizeof...(FieldTypes)) { if (_errors.size() == 1) { @@ -443,7 +448,7 @@ struct Parser> { const auto& f = std::get<_i>(_fields_vec); - if (!f) { + if (_r.is_empty(f)) { if constexpr (is_required()) { const auto key = FieldType::name_.str(); _errors.emplace_back( @@ -459,7 +464,7 @@ struct Parser> { return _error; }; - get_value(_r, *f).or_else(add_error_if_applicable); + get_value(_r, f).or_else(add_error_if_applicable); return collect_errors<_i + 1>(_r, _fields_vec, std::move(_errors)); } @@ -480,7 +485,7 @@ struct Parser> { auto value = Parser::write(_w, rfl::get<_i>(_tup)); const auto name = FieldType::name_.str(); if constexpr (!is_required()) { - if (!_w.is_empty(&value)) { + if (!_w.is_empty(value)) { _w.set_field(name, value, _ptr); } } else { @@ -523,14 +528,16 @@ struct Parser> { typename std::tuple_element<_i, std::tuple>::type; const auto name = FieldType::name_.string_view(); - Parser>::field_indices_[name] = _i; + Parser>::field_indices_[name] = + static_cast(_i); set_field_indices<_i + 1>(); } } private: /// Maps each of the field names to an index signifying their order. - static inline std::unordered_map field_indices_; + static inline std::unordered_map + field_indices_; }; // ---------------------------------------------------------------------------- @@ -544,7 +551,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, InputVarType* _var) noexcept { - if (_r.is_empty(_var)) { + if (_r.is_empty(*_var)) { return std::optional(); } const auto to_opt = [](auto&& _t) { return std::make_optional(_t); }; @@ -594,7 +601,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, InputVarType* _var) noexcept { - if (_r.is_empty(_var)) { + if (_r.is_empty(*_var)) { return std::shared_ptr(); } const auto to_ptr = [](auto&& _t) { @@ -850,7 +857,7 @@ struct Parser> { /// Expresses the variables as type T. static Result> read(const R& _r, InputVarType* _var) noexcept { - if (_r.is_empty(_var)) { + if (_r.is_empty(*_var)) { return std::unique_ptr(); } const auto to_ptr = [](auto&& _t) {