mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Move some heavy templates into cpp (#204)
This commit is contained in:
+78
-43
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.23)
|
||||
|
||||
option(REFLECTCPP_BUILD_SHARED "Build shared library" ${BUILD_SHARED_LIBS})
|
||||
|
||||
option(REFLECTCPP_JSON "Enable JSON support" ON) # enabled by default
|
||||
option(REFLECTCPP_BSON "Enable BSON support" OFF)
|
||||
option(REFLECTCPP_CBOR "Enable CBOR support" OFF)
|
||||
option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF)
|
||||
@@ -17,6 +18,7 @@ option(REFLECTCPP_USE_BUNDLED_DEPENDENCIES "Use the bundled dependencies" ON)
|
||||
|
||||
set(REFLECTCPP_USE_VCPKG_DEFAULT OFF)
|
||||
if(REFLECTCPP_BUILD_BENCHMARKS)
|
||||
set(REFLECTCPP_JSON ON CACHE BOOL "" FORCE)
|
||||
set(REFLECTCPP_BSON ON CACHE BOOL "" FORCE)
|
||||
set(REFLECTCPP_CBOR ON CACHE BOOL "" FORCE)
|
||||
set(REFLECTCPP_FLEXBUFFERS ON CACHE BOOL "" FORCE)
|
||||
@@ -25,7 +27,9 @@ if(REFLECTCPP_BUILD_BENCHMARKS)
|
||||
set(REFLECTCPP_TOML ON CACHE BOOL "" FORCE)
|
||||
set(REFLECTCPP_YAML ON CACHE BOOL "" FORCE)
|
||||
endif()
|
||||
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML)
|
||||
if (REFLECTCPP_BUILD_TESTS OR REFLECTCPP_BUILD_BENCHMARKS OR
|
||||
(REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES) OR
|
||||
REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_MSGPACK OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML)
|
||||
# enable vcpkg per default if require features other than JSON
|
||||
set(REFLECTCPP_USE_VCPKG_DEFAULT ON)
|
||||
endif()
|
||||
@@ -40,58 +44,71 @@ project(reflectcpp LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
if(REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
|
||||
if (REFLECTCPP_BUILD_SHARED)
|
||||
add_library(reflectcpp SHARED)
|
||||
else()
|
||||
add_library(reflectcpp STATIC)
|
||||
endif()
|
||||
|
||||
target_sources(reflectcpp PRIVATE src/reflectcpp.cpp src/yyjson.c)
|
||||
set_source_files_properties(src/yyjson.c PROPERTIES LANGUAGE CXX)
|
||||
|
||||
target_compile_features(reflectcpp PUBLIC cxx_std_20)
|
||||
|
||||
if (MSVC)
|
||||
target_compile_options(reflectcpp PRIVATE $<$<CONFIG:Debug>:-Wall>)
|
||||
else()
|
||||
target_compile_options(reflectcpp PRIVATE $<$<CONFIG:Debug>:-Wall -Wextra>)
|
||||
endif()
|
||||
|
||||
target_include_directories(
|
||||
reflectcpp PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/rfl/thirdparty>)
|
||||
if (REFLECTCPP_BUILD_SHARED)
|
||||
add_library(reflectcpp SHARED)
|
||||
else()
|
||||
if (REFLECTCPP_BUILD_SHARED)
|
||||
add_library(reflectcpp SHARED)
|
||||
else()
|
||||
add_library(reflectcpp STATIC)
|
||||
endif()
|
||||
|
||||
target_sources(reflectcpp PRIVATE src/reflectcpp.cpp)
|
||||
|
||||
target_include_directories(
|
||||
reflectcpp PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>)
|
||||
|
||||
target_compile_features(reflectcpp PUBLIC cxx_std_20)
|
||||
|
||||
find_package(ctre CONFIG REQUIRED)
|
||||
find_package(yyjson CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE ctre::ctre yyjson::yyjson)
|
||||
add_library(reflectcpp STATIC)
|
||||
endif()
|
||||
|
||||
set_target_properties(reflectcpp PROPERTIES LINKER_LANGUAGE CXX)
|
||||
if (MSVC)
|
||||
target_compile_options(reflectcpp PRIVATE $<$<CONFIG:Debug>:-Wall>)
|
||||
else()
|
||||
target_compile_options(reflectcpp PRIVATE $<$<CONFIG:Debug>:-Wall -Wextra>)
|
||||
endif()
|
||||
|
||||
target_compile_features(reflectcpp PUBLIC cxx_std_20)
|
||||
|
||||
set(REFLECT_CPP_SOURCES
|
||||
src/reflectcpp.cpp
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
reflectcpp PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>)
|
||||
|
||||
if (REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
|
||||
target_include_directories(
|
||||
reflectcpp PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/rfl/thirdparty>)
|
||||
else ()
|
||||
find_package(ctre CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE ctre::ctre)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_JSON)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_json.cpp
|
||||
)
|
||||
if (REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/yyjson.c
|
||||
)
|
||||
set_source_files_properties(src/yyjson.c PROPERTIES LANGUAGE CXX)
|
||||
|
||||
target_include_directories(
|
||||
reflectcpp PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/rfl/thirdparty>)
|
||||
else ()
|
||||
find_package(yyjson CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE yyjson::yyjson)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_BSON)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_bson.cpp
|
||||
)
|
||||
find_package(bson-1.0 CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp PRIVATE $<IF:$<TARGET_EXISTS:mongo::bson_static>,mongo::bson_static,mongo::bson_shared>)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_CBOR)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_cbor.cpp
|
||||
)
|
||||
target_include_directories(reflectcpp SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include")
|
||||
target_include_directories(reflectcpp SYSTEM PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include/tinycbor")
|
||||
if (MSVC)
|
||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tinycbor${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
||||
else ()
|
||||
@@ -100,11 +117,17 @@ if (REFLECTCPP_CBOR)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_FLEXBUFFERS)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_flexbuf.cpp
|
||||
)
|
||||
find_package(flatbuffers CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_MSGPACK)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_msgpack.cpp
|
||||
)
|
||||
find_package(msgpack-c CONFIG REQUIRED)
|
||||
if (MSVC)
|
||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/msgpack-c${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
||||
@@ -114,6 +137,9 @@ if (REFLECTCPP_MSGPACK)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_TOML)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_toml.cpp
|
||||
)
|
||||
if (MSVC)
|
||||
target_link_libraries(reflectcpp PRIVATE "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/tomlplusplus${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
||||
else ()
|
||||
@@ -122,15 +148,24 @@ if (REFLECTCPP_TOML)
|
||||
endif()
|
||||
|
||||
if (REFLECTCPP_XML)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_xml.cpp
|
||||
)
|
||||
find_package(pugixml CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE pugixml::pugixml)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_YAML)
|
||||
list(APPEND REFLECT_CPP_SOURCES
|
||||
src/reflectcpp_yaml.cpp
|
||||
)
|
||||
find_package(yaml-cpp CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE yaml-cpp::yaml-cpp)
|
||||
endif ()
|
||||
|
||||
set_target_properties(reflectcpp PROPERTIES LINKER_LANGUAGE CXX)
|
||||
target_sources(reflectcpp PRIVATE ${REFLECT_CPP_SOURCES})
|
||||
|
||||
if (REFLECTCPP_BUILD_TESTS)
|
||||
if (MSVC)
|
||||
set(REFLECT_CPP_GTEST_LIB "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/lib/gtest${CMAKE_STATIC_LIBRARY_SUFFIX}")
|
||||
|
||||
@@ -527,10 +527,13 @@ The following compilers are supported:
|
||||
|
||||
### Option 1: Include source files into your own build
|
||||
|
||||
Simply copy the contents of the folder `include` into your source repository or add it to your include path and also add `src/reflectcpp.cpp` and `src/yyjson.c` to your source files for compilation.
|
||||
If you don't need JSON support or want to link to your own version of YYJSON, then only copy `src/reflectcpp.cpp`.
|
||||
Simply copy the contents of the folders `include` and `src` into your source repository or add it to your include path
|
||||
and also add `src/reflectcpp.cpp` and `src/reflectcpp_json.cpp` and `src/yyjson.c` to your source files for compilation.
|
||||
If you want to link to your own version of YYJSON, then only copy `src/reflectcpp.cpp` and `src/reflectcpp_json.cpp`.
|
||||
If you don't need JSON support, then only copy `src/reflectcpp.cpp`.
|
||||
|
||||
If you need support for other serialization formats like flexbuffers or XML, you should also include and link the respective libraries, as listed in the section on serialization formats.
|
||||
If you need support for other serialization formats like flexbuffers or XML, you should also add `src/reflectcpp_<format>.cpp`
|
||||
and include and link the respective libraries, as listed in the section on serialization formats.
|
||||
|
||||
### Option 2: Compilation using cmake
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ template <class R, class W, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, bson_oid_t>
|
||||
struct Parser<R, W, ProcessorsType, bson_oid_t> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -50,49 +50,12 @@ struct Reader {
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _arr.val_->value.v_doc;
|
||||
if (bson_init_static(&b, doc.data, doc.data_len)) {
|
||||
if (bson_iter_init(&iter, &b)) {
|
||||
size_t i = 0;
|
||||
while (bson_iter_next(&iter)) {
|
||||
if (i == _idx) {
|
||||
return to_input_var(&iter);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
} else {
|
||||
return Error("Could not init the array iteration.");
|
||||
}
|
||||
} else {
|
||||
return Error("Could not init array.");
|
||||
}
|
||||
return Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _obj.val_->value.v_doc;
|
||||
if (bson_init_static(&b, doc.data, doc.data_len)) {
|
||||
if (bson_iter_init(&iter, &b)) {
|
||||
while (bson_iter_next(&iter)) {
|
||||
auto key = std::string(bson_iter_key(&iter));
|
||||
if (key == _name) {
|
||||
return to_input_var(&iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.val_->value_type == BSON_TYPE_NULL;
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -159,13 +122,7 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto btype = _var.val_->value_type;
|
||||
if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return InputArrayType{_var.val_};
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -212,13 +169,7 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto btype = _var.val_->value_type;
|
||||
if (btype != BSON_TYPE_DOCUMENT) {
|
||||
return Error("Could not cast to a document.");
|
||||
}
|
||||
return InputObjectType{_var.val_};
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
@@ -231,12 +182,10 @@ struct Reader {
|
||||
}
|
||||
|
||||
private:
|
||||
InputVarType to_input_var(bson_iter_t* _iter) const noexcept {
|
||||
return InputVarType{bson_iter_value(_iter)};
|
||||
}
|
||||
InputVarType to_input_var(bson_iter_t* _iter) const noexcept;
|
||||
};
|
||||
|
||||
} // namespace bson
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
+14
-76
@@ -62,23 +62,15 @@ class Writer {
|
||||
using OutputObjectType = BSONOutputObject;
|
||||
using OutputVarType = BSONOutputVar;
|
||||
|
||||
Writer(bson_t* _doc) : doc_(_doc) {}
|
||||
Writer(bson_t* _doc);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
bson_array_builder_t* val = bson_array_builder_new();
|
||||
return OutputArrayType(val, IsRoot{});
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return OutputObjectType(doc_, IsRoot{});
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
// Appears to be unsupported by the BSON C API.
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
@@ -88,38 +80,18 @@ class Writer {
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
bson_array_builder_t* val;
|
||||
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
|
||||
return OutputArrayType(val, IsArray{_parent->val_});
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
bson_array_builder_t* val;
|
||||
bson_append_array_builder_begin(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), &val);
|
||||
return OutputArrayType(val, IsObject{_parent->val_});
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
subdocs_->emplace_back(rfl::Box<BSONType>());
|
||||
bson_array_builder_append_document_begin(_parent->val_,
|
||||
&(subdocs_->back()->val_));
|
||||
return OutputObjectType(&subdocs_->back()->val_, IsArray{_parent->val_});
|
||||
}
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
subdocs_->emplace_back(rfl::Box<BSONType>());
|
||||
bson_append_document_begin(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()),
|
||||
&(subdocs_->back()->val_));
|
||||
return OutputObjectType(&subdocs_->back()->val_, IsObject{_parent->val_});
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -183,48 +155,14 @@ class Writer {
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
bson_array_builder_append_null(_parent->val_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
bson_append_null(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()));
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
const auto handle = [&](const auto _parent) {
|
||||
using Type = std::remove_cvref_t<decltype(_parent)>;
|
||||
if constexpr (std::is_same<Type, IsArray>()) {
|
||||
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
|
||||
} else if constexpr (std::is_same<Type, IsObject>()) {
|
||||
bson_append_array_builder_end(_parent.ptr_, _arr->val_);
|
||||
} else if constexpr (std::is_same<Type, IsRoot>()) {
|
||||
bson_array_builder_build(_arr->val_, doc_);
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
|
||||
}
|
||||
};
|
||||
std::visit(handle, _arr->parent_);
|
||||
}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
const auto handle = [&](const auto _parent) {
|
||||
using Type = std::remove_cvref_t<decltype(_parent)>;
|
||||
if constexpr (std::is_same<Type, IsArray>()) {
|
||||
bson_array_builder_append_document_end(_parent.ptr_, _obj->val_);
|
||||
} else if constexpr (std::is_same<Type, IsObject>()) {
|
||||
bson_append_document_end(_parent.ptr_, _obj->val_);
|
||||
} else if constexpr (std::is_same<Type, IsRoot>()) {
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
|
||||
}
|
||||
};
|
||||
std::visit(handle, _obj->parent_);
|
||||
}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
/// Pointer to the main document. In BSON, documents are what are usually
|
||||
@@ -238,4 +176,4 @@ class Writer {
|
||||
} // namespace bson
|
||||
} // namespace rfl
|
||||
|
||||
#endif // BSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -62,4 +62,4 @@ std::ostream& write(const auto& _obj, std::ostream& _stream) noexcept {
|
||||
} // namespace bson
|
||||
} // namespace rfl
|
||||
|
||||
#endif // BSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -41,68 +41,12 @@ struct Reader {
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
InputVarType var;
|
||||
auto err = cbor_value_enter_container(&_arr.val_, &var.val_);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_array_length(&_arr.val_, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_idx >= length) {
|
||||
return Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
for (size_t i = 0; i < _idx; ++i) {
|
||||
err = cbor_value_advance(&var.val_);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return var;
|
||||
}
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
InputVarType var;
|
||||
auto buffer = std::string();
|
||||
auto err = cbor_value_enter_container(&_obj.val_, &var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_map_length(&_obj.val_, &length);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&var.val_)) {
|
||||
return Error("Expected the key to be a string value.");
|
||||
}
|
||||
err = get_string(&var.val_, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
err = cbor_value_advance(&var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_name == buffer) {
|
||||
return var;
|
||||
}
|
||||
err = cbor_value_advance(&var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return cbor_value_is_null(&_var.val_);
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -171,20 +115,10 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_array(&_var.val_)) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return InputArrayType{_var.val_};
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_map(&_var.val_)) {
|
||||
return Error("Could not cast to an object.");
|
||||
}
|
||||
return InputObjectType{_var.val_};
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -259,38 +193,13 @@ struct Reader {
|
||||
|
||||
private:
|
||||
CborError get_bytestring(const CborValue* _ptr,
|
||||
rfl::Bytestring* _str) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_str->resize(length);
|
||||
if (length > 0) {
|
||||
return cbor_value_copy_byte_string(
|
||||
_ptr, reinterpret_cast<uint8_t*>(&((*_str)[0])), &length, NULL);
|
||||
} else {
|
||||
return CborNoError;
|
||||
}
|
||||
}
|
||||
rfl::Bytestring* _str) const noexcept;
|
||||
|
||||
CborError get_string(const CborValue* _ptr,
|
||||
std::string* _str) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_str->resize(length);
|
||||
if (length > 0) {
|
||||
return cbor_value_copy_text_string(_ptr, &((*_str)[0]), &length, NULL);
|
||||
} else {
|
||||
return CborNoError;
|
||||
}
|
||||
}
|
||||
std::string* _str) const noexcept;
|
||||
};
|
||||
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
+17
-53
@@ -40,22 +40,15 @@ class Writer {
|
||||
using OutputObjectType = CBOROutputObject;
|
||||
using OutputVarType = CBOROutputVar;
|
||||
|
||||
Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
|
||||
Writer(CborEncoder* _encoder);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size, encoder_);
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size, encoder_);
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
cbor_encode_null(encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
@@ -63,28 +56,18 @@ class Writer {
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -100,40 +83,21 @@ class Writer {
|
||||
return new_value(_var, _parent->encoder_);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
|
||||
}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
|
||||
}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
||||
return OutputArrayType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
CborEncoder* _parent) const noexcept;
|
||||
|
||||
OutputObjectType new_object(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
|
||||
return OutputObjectType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
CborEncoder* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var, CborEncoder* _parent) const noexcept {
|
||||
@@ -166,4 +130,4 @@ class Writer {
|
||||
} // namespace cbor
|
||||
} // namespace rfl
|
||||
|
||||
#endif // CBOR_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -44,28 +44,12 @@ struct Reader {
|
||||
static constexpr bool has_custom_constructor = has_from_flexbuf<T>::value;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
if (_idx >= _arr.size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr[_idx];
|
||||
}
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
const auto keys = _obj.Keys();
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
if (_name == keys[i].AsString().c_str()) {
|
||||
return _obj.Values()[i];
|
||||
}
|
||||
}
|
||||
return rfl::Error("Map does not contain any element called '" + _name +
|
||||
"'.");
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.IsNull();
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -130,21 +114,10 @@ struct Reader {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.IsVector()) {
|
||||
return rfl::Error("Could not cast to Vector.");
|
||||
}
|
||||
return _var.AsVector();
|
||||
}
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.IsMap()) {
|
||||
return rfl::Error("Could not cast to Map!");
|
||||
}
|
||||
return _var.AsMap();
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
|
||||
@@ -38,22 +38,15 @@ struct Writer {
|
||||
using OutputObjectType = OutputObject;
|
||||
using OutputVarType = OutputVar;
|
||||
|
||||
Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
|
||||
Writer(const Ref<flexbuffers::Builder>& _fbb);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
@@ -61,26 +54,18 @@ struct Writer {
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -95,24 +80,14 @@ struct Writer {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
fbb_->Null(_name.data());
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
fbb_->EndVector(_arr->start_, false, false);
|
||||
}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
fbb_->EndMap(_obj->start_);
|
||||
}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
@@ -154,25 +129,13 @@ struct Writer {
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept {
|
||||
const auto start = fbb_->StartVector(_name.data());
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept;
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
const auto start = fbb_->StartVector();
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
OutputArrayType new_array() const noexcept;
|
||||
|
||||
OutputObjectType new_object(const std::string_view& _name) const noexcept {
|
||||
const auto start = fbb_->StartMap(_name.data());
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
OutputObjectType new_object(const std::string_view& _name) const noexcept;
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
const auto start = fbb_->StartMap();
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
OutputObjectType new_object() const noexcept;
|
||||
|
||||
private:
|
||||
Ref<flexbuffers::Builder> fbb_;
|
||||
|
||||
@@ -48,31 +48,16 @@ struct Reader {
|
||||
using InputVarType = YYJSONInputVar;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_json_obj(var);
|
||||
});
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_json_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx));
|
||||
if (!var.val_) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
|
||||
if (!var.val_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType _var) const noexcept {
|
||||
return !_var.val_ || yyjson_is_null(_var.val_);
|
||||
}
|
||||
bool is_empty(const InputVarType _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -135,20 +120,10 @@ struct Reader {
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
|
||||
if (!yyjson_is_arr(_var.val_)) {
|
||||
return rfl::Error("Could not cast to array!");
|
||||
}
|
||||
return InputArrayType(_var.val_);
|
||||
}
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType _var) const noexcept {
|
||||
if (!yyjson_is_obj(_var.val_)) {
|
||||
return rfl::Error("Could not cast to object!");
|
||||
}
|
||||
return InputObjectType(_var.val_);
|
||||
}
|
||||
const InputVarType _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
@@ -164,4 +139,4 @@ struct Reader {
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
+16
-57
@@ -48,27 +48,13 @@ class Writer {
|
||||
using OutputObjectType = YYJSONOutputObject;
|
||||
using OutputVarType = YYJSONOutputVar;
|
||||
|
||||
Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
Writer(yyjson_mut_doc* _doc);
|
||||
|
||||
~Writer() = default;
|
||||
OutputArrayType array_as_root(const size_t) const noexcept;
|
||||
|
||||
OutputArrayType array_as_root(const size_t) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
@@ -78,36 +64,18 @@ class Writer {
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t, OutputArrayType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
OutputObjectType add_object_to_array(const size_t,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -127,23 +95,14 @@ class Writer {
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType*) const noexcept {}
|
||||
void end_array(OutputArrayType*) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType*) const noexcept {}
|
||||
void end_object(OutputObjectType*) const noexcept;
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
@@ -170,4 +129,4 @@ class Writer {
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
|
||||
|
||||
template <class T>
|
||||
struct TypeHelper {};
|
||||
|
||||
@@ -35,25 +33,16 @@ struct TypeHelper<rfl::Variant<Ts...>> {
|
||||
using JSONSchemaType = rfl::Variant<schema::JSONSchema<Ts>...>;
|
||||
};
|
||||
|
||||
std::string to_schema_internal_schema(
|
||||
const parsing::schema::Definition& internal_schema,
|
||||
const yyjson_write_flag);
|
||||
|
||||
/// Returns the JSON schema for a class.
|
||||
template <class T, class... Ps>
|
||||
std::string to_schema(const yyjson_write_flag _flag = 0) {
|
||||
const auto internal_schema =
|
||||
parsing::schema::make<Reader, Writer, T, Processors<Ps...>>();
|
||||
auto definitions = std::map<std::string, schema::Type>();
|
||||
for (const auto& [k, v] : internal_schema.definitions_) {
|
||||
definitions[k] = type_to_json_schema_type(v);
|
||||
}
|
||||
using JSONSchemaType =
|
||||
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
|
||||
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
|
||||
using U = std::decay_t<decltype(_root)>;
|
||||
return schema::JSONSchema<U>{.root = std::move(_root),
|
||||
.definitions = definitions};
|
||||
};
|
||||
auto root = type_to_json_schema_type(internal_schema.root_);
|
||||
const auto json_schema = rfl::visit(to_schema, std::move(root.value));
|
||||
return write(json_schema, _flag);
|
||||
return to_schema_internal_schema(internal_schema, _flag);
|
||||
}
|
||||
} // namespace rfl::json
|
||||
|
||||
|
||||
@@ -53,4 +53,4 @@ std::ostream& write(const auto& _obj, std::ostream& _stream,
|
||||
} // namespace json
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -27,33 +27,12 @@ struct Reader {
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
if (_idx >= _arr.size) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr.ptr[_idx];
|
||||
}
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
}
|
||||
const auto current_name =
|
||||
std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
if (_name == current_name) {
|
||||
return _obj.ptr[i].val;
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept ;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.type == MSGPACK_OBJECT_NIL;
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -96,20 +75,10 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_ARRAY) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return _var.via.array;
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
return _var.via.map;
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -153,4 +122,4 @@ struct Reader {
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -32,53 +32,32 @@ class Writer {
|
||||
using OutputObjectType = MsgpackOutputObject;
|
||||
using OutputVarType = MsgpackOutputVar;
|
||||
|
||||
Writer(msgpack_packer* _pk) : pk_(_pk) {}
|
||||
Writer(msgpack_packer* _pk);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var);
|
||||
}
|
||||
OutputVarType value_as_root(const T& _var) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_array(_size);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_object(_size);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -95,33 +74,19 @@ class Writer {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size) const noexcept {
|
||||
msgpack_pack_array(pk_, _size);
|
||||
return OutputArrayType{};
|
||||
}
|
||||
OutputArrayType new_array(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType new_object(const size_t _size) const noexcept {
|
||||
msgpack_pack_map(pk_, _size);
|
||||
return OutputObjectType{};
|
||||
}
|
||||
OutputObjectType new_object(const size_t _size) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var) const noexcept {
|
||||
@@ -155,4 +120,4 @@ class Writer {
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
|
||||
#endif // MSGPACK_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -30,9 +30,6 @@ struct FieldVariantParser {
|
||||
using InputObjectType = typename R::InputObjectType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static ResultType read(const R& _r, const InputVarType& _var) noexcept {
|
||||
static_assert(
|
||||
internal::no_duplicate_field_names<rfl::Tuple<FieldTypes...>>(),
|
||||
|
||||
@@ -25,9 +25,6 @@ struct MapParser {
|
||||
using InputObjectType = typename R::InputObjectType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using KeyType = std::remove_cvref_t<typename MapType::value_type::first_type>;
|
||||
using ValueType =
|
||||
std::remove_cvref_t<typename MapType::value_type::second_type>;
|
||||
|
||||
@@ -40,7 +40,6 @@ template <class R, class W, bool _ignore_empty_containers, bool _all_required,
|
||||
requires AreReaderAndWriter<R, W, NamedTuple<FieldTypes...>>
|
||||
struct NamedTupleParser {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -23,9 +23,6 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::array<T, _size>> read(const R& _r,
|
||||
|
||||
@@ -16,7 +16,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, Box<T>>
|
||||
struct Parser<R, W, Box<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<Box<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_box = [](auto&& _t) { return Box<T>::make(std::move(_t)); };
|
||||
|
||||
@@ -23,9 +23,6 @@ struct Parser<R, W, T[_size], ProcessorsType> {
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
using CArray = T[_size];
|
||||
|
||||
|
||||
@@ -36,7 +36,6 @@ requires AreReaderAndWriter<R, W, T>
|
||||
struct Parser {
|
||||
public:
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ template <class R, class W, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::filesystem::path>
|
||||
struct Parser<R, W, std::filesystem::path, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::filesystem::path> read(const R& _r,
|
||||
|
||||
@@ -19,7 +19,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::optional<T>>
|
||||
struct Parser<R, W, std::optional<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ template <class R, class W, class FirstType, class SecondType,
|
||||
requires AreReaderAndWriter<R, W, std::pair<FirstType, SecondType>>
|
||||
struct Parser<R, W, std::pair<FirstType, SecondType>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<std::pair<FirstType, SecondType>> read(
|
||||
|
||||
@@ -18,7 +18,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, T*>
|
||||
struct Parser<R, W, T*, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, Ref<T>>
|
||||
struct Parser<R, W, Ref<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(std::move(_t)); };
|
||||
|
||||
@@ -17,7 +17,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::reference_wrapper<T>>
|
||||
struct Parser<R, W, std::reference_wrapper<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<std::reference_wrapper<T>> read(
|
||||
const R&, const InputVarType&) noexcept {
|
||||
|
||||
@@ -19,7 +19,6 @@ template <class R, class W, class T, internal::StringLiteral _name,
|
||||
requires AreReaderAndWriter<R, W, Rename<_name, T>>
|
||||
struct Parser<R, W, Rename<_name, T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<Rename<_name, T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
|
||||
@@ -17,7 +17,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, Result<T>>
|
||||
struct Parser<R, W, Result<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ErrorType = NamedTuple<Field<"error", std::string>>;
|
||||
using VariantType = std::variant<std::remove_cvref_t<T>, ErrorType>;
|
||||
|
||||
@@ -22,9 +22,6 @@ struct Parser<R, W, internal::Array<T>, ProcessorsType> {
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using StdArray = internal::to_std_array_t<T>;
|
||||
|
||||
static Result<internal::Array<T>> read(const R& _r,
|
||||
|
||||
@@ -20,7 +20,6 @@ requires AreReaderAndWriter<R, W, rfl::Variant<AlternativeTypes...>>
|
||||
class Parser<R, W, rfl::Variant<AlternativeTypes...>, ProcessorsType> {
|
||||
public:
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<rfl::Variant<AlternativeTypes...>> read(
|
||||
const R& _r, const InputVarType& _var) noexcept {
|
||||
|
||||
@@ -18,7 +18,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::shared_ptr<T>>
|
||||
struct Parser<R, W, std::shared_ptr<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ struct Parser<R, W,
|
||||
internal::Skip<T, _skip_serialization, _skip_deserialization>,
|
||||
ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<internal::Skip<T, _skip_serialization, _skip_deserialization>>
|
||||
read(const R& _r, const InputVarType& _var) noexcept {
|
||||
|
||||
@@ -18,7 +18,6 @@ template <class R, class W, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::string_view>
|
||||
struct Parser<R, W, std::string_view, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<std::string_view> read(const R&,
|
||||
const InputVarType&) noexcept {
|
||||
|
||||
@@ -30,9 +30,6 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
|
||||
using InputObjectType = typename R::InputObjectType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
constexpr static bool no_field_names_ = ProcessorsType::no_field_names_;
|
||||
|
||||
using InputObjectOrArrayType =
|
||||
|
||||
@@ -19,7 +19,6 @@ template <class R, class W, class T, class ProcessorsType>
|
||||
requires AreReaderAndWriter<R, W, std::unique_ptr<T>>
|
||||
struct Parser<R, W, std::unique_ptr<T>, ProcessorsType> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
|
||||
|
||||
public:
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
static Result<std::variant<AlternativeTypes...>> read(
|
||||
const R& _r, const InputVarType& _var) noexcept {
|
||||
|
||||
@@ -18,7 +18,6 @@ requires AreReaderAndWriter<R, W, std::wstring>
|
||||
struct Parser<R, W, std::wstring, ProcessorsType> {
|
||||
public:
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
|
||||
@@ -24,9 +24,6 @@ struct TupleParser {
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<TupleType> read(const R& _r,
|
||||
|
||||
@@ -33,9 +33,6 @@ struct VectorParser {
|
||||
using InputArrayType = typename R::InputArrayType;
|
||||
using InputVarType = typename R::InputVarType;
|
||||
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
using T = typename VecType::value_type;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../Tuple.hpp"
|
||||
#include "../internal/is_array.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl::parsing {
|
||||
|
||||
|
||||
@@ -25,30 +25,16 @@ struct Reader {
|
||||
using InputVarType = ::toml::node*;
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_toml_obj(var);
|
||||
});
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_toml_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
if (_idx >= _arr->size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr->get(_idx);
|
||||
}
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = (*_obj)[_name];
|
||||
if (!var) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var.node();
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var && true;
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -81,14 +67,7 @@ struct Reader {
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto ptr = _var->as_array();
|
||||
if (!ptr) {
|
||||
return rfl::Error("Could not cast to an array!");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -112,13 +91,7 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto ptr = _var->as_table();
|
||||
if (!ptr) {
|
||||
return rfl::Error("Could not cast to a table!");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
|
||||
+17
-43
@@ -33,22 +33,16 @@ class Writer {
|
||||
using OutputObjectType = TOMLObject;
|
||||
using OutputVarType = TOMLVar;
|
||||
|
||||
Writer(::toml::table* _root) : root_(_root) {}
|
||||
Writer(::toml::table* _root);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
template <class T>
|
||||
OutputArrayType array_as_root(const T _size) const noexcept {
|
||||
static_assert(rfl::always_false_v<T>,
|
||||
"TOML only allows tables as the root element.");
|
||||
return OutputArrayType{nullptr};
|
||||
}
|
||||
OutputArrayType array_as_root(const T _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return OutputObjectType{root_};
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept { return OutputVarType{}; }
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
@@ -58,32 +52,18 @@ class Writer {
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto i = _parent->val_->size();
|
||||
_parent->val_->push_back(::toml::array());
|
||||
return OutputArrayType{_parent->val_->at(i).as_array()};
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::array());
|
||||
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
|
||||
}
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto i = _parent->val_->size();
|
||||
_parent->val_->push_back(::toml::table());
|
||||
return OutputObjectType{_parent->val_->at(i).as_table()};
|
||||
}
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::table());
|
||||
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -100,20 +80,14 @@ class Writer {
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
_parent->val_->push_back(std::string(""));
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::value(std::string("")));
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
::toml::table* root_;
|
||||
@@ -121,4 +95,4 @@ class Writer {
|
||||
|
||||
} // namespace rfl::toml
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
#ifndef RFL_XML_READER_HPP_
|
||||
#define RFL_XML_READER_HPP_
|
||||
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <pugixml.hpp>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
@@ -49,49 +42,13 @@ struct Reader {
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = false;
|
||||
|
||||
/// XML-only helper function. This is needed because XML distinguishes between
|
||||
/// nodes and attributes.
|
||||
static rfl::Result<pugi::xml_node> cast_as_node(
|
||||
const std::variant<pugi::xml_node, pugi::xml_attribute>&
|
||||
_node_or_attribute) {
|
||||
const auto cast = [](const auto& _n) -> Result<pugi::xml_node> {
|
||||
using Type = std::remove_cvref_t<decltype(_n)>;
|
||||
if constexpr (std::is_same<Type, pugi::xml_node>()) {
|
||||
return _n;
|
||||
} else {
|
||||
return Error("Field '" + std::string(_n.name()) + "' is an attribute.");
|
||||
}
|
||||
};
|
||||
return std::visit(cast, _node_or_attribute);
|
||||
}
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
const auto name = _arr.node_.name();
|
||||
size_t i = 0;
|
||||
for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
|
||||
if (i == _idx) {
|
||||
return InputVarType(node);
|
||||
}
|
||||
}
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
const auto node = _obj.node_.child(_name.c_str());
|
||||
if (!node) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return InputVarType(node);
|
||||
}
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType _var) const noexcept {
|
||||
const auto wrap = [](const auto& _node) { return !_node; };
|
||||
return std::visit(cast_as_node, _var.node_or_attribute_)
|
||||
.transform(wrap)
|
||||
.value_or(false);
|
||||
}
|
||||
bool is_empty(const InputVarType _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
|
||||
@@ -128,10 +85,7 @@ struct Reader {
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
|
||||
const auto wrap = [](const auto& _node) { return InputArrayType(_node); };
|
||||
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
|
||||
}
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -168,10 +122,7 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType _var) const noexcept {
|
||||
const auto wrap = [](const auto& _node) { return InputObjectType(_node); };
|
||||
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
|
||||
}
|
||||
const InputVarType _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
|
||||
+30
-85
@@ -1,26 +1,17 @@
|
||||
#ifndef RFL_XML_WRITER_HPP_
|
||||
#define RFL_XML_WRITER_HPP_
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <pugixml.hpp>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
struct Writer {
|
||||
static constexpr const char* XML_CONTENT = "xml_content";
|
||||
|
||||
struct XMLOutputArray {
|
||||
XMLOutputArray(const std::string_view& _name,
|
||||
const Ref<pugi::xml_node>& _node)
|
||||
@@ -43,72 +34,41 @@ struct Writer {
|
||||
using OutputObjectType = XMLOutputObject;
|
||||
using OutputVarType = XMLOutputVar;
|
||||
|
||||
Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
|
||||
: root_(_root), root_name_(_root_name) {}
|
||||
Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputArrayType(root_name_, node_child);
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
return value_as_root_impl(str);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return *_parent;
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return OutputArrayType(_name, _parent->node_);
|
||||
}
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.data()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.data()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
return add_value_to_array_impl(str, _parent);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -116,43 +76,18 @@ struct Writer {
|
||||
const std::string_view& _name, const T& _var, OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
if (_is_attribute) {
|
||||
_parent->node_->append_attribute(_name.data()) = str.c_str();
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
_parent->node_->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
return add_value_to_object_impl(_name, str, _parent, _is_attribute);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.data()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept {
|
||||
if (_is_attribute) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
}
|
||||
const bool _is_attribute = false) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
@@ -169,6 +104,16 @@ struct Writer {
|
||||
}
|
||||
}
|
||||
|
||||
OutputVarType value_as_root_impl(const std::string& _str) const noexcept;
|
||||
|
||||
OutputVarType add_value_to_array_impl(
|
||||
const std::string& _str, OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_value_to_object_impl(
|
||||
const std::string_view& _name, const std::string& _str,
|
||||
OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept;
|
||||
|
||||
public:
|
||||
Ref<pugi::xml_node> root_;
|
||||
|
||||
@@ -178,4 +123,4 @@ struct Writer {
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // XML_PARSER_HPP_
|
||||
#endif // RFL_XML_WRITER_HPP_
|
||||
|
||||
@@ -75,4 +75,4 @@ std::string write(const auto& _obj, const std::string& _indent = " ") {
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // XML_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -45,30 +45,16 @@ struct Reader {
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_yaml_obj(var);
|
||||
});
|
||||
static constexpr bool has_custom_constructor =
|
||||
(requires(InputVarType var) { T::from_yaml_obj(var); });
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
if (_idx >= _arr.node_.size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return InputVarType(_arr.node_[_idx]);
|
||||
}
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = InputVarType(_obj.node_[_name]);
|
||||
if (!var.node_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.node_ && true;
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -86,13 +72,7 @@ struct Reader {
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsSequence()) {
|
||||
return rfl::Error("Could not cast to sequence!");
|
||||
}
|
||||
return InputArrayType(_var.node_);
|
||||
}
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -121,12 +101,7 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsMap()) {
|
||||
return rfl::Error("Could not cast to map!");
|
||||
}
|
||||
return InputObjectType(_var.node_);
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
|
||||
+21
-55
@@ -31,21 +31,15 @@ class Writer {
|
||||
using OutputObjectType = YAMLObject;
|
||||
using OutputVarType = YAMLVar;
|
||||
|
||||
Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
|
||||
Writer(const Ref<YAML::Emitter>& _out);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
@@ -53,26 +47,18 @@ class Writer {
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
OutputArrayType add_array_to_object(const std::string_view& _name,
|
||||
const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
OutputObjectType add_object_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -87,22 +73,14 @@ class Writer {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, YAML::Null);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
(*out_) << YAML::EndSeq;
|
||||
}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
(*out_) << YAML::EndMap;
|
||||
}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
@@ -118,25 +96,13 @@ class Writer {
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
OutputArrayType new_array(const std::string_view& _name) const noexcept;
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
(*out_) << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
OutputArrayType new_array() const noexcept;
|
||||
|
||||
OutputObjectType new_object(const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
OutputObjectType new_object(const std::string_view& _name) const noexcept;
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
(*out_) << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
OutputObjectType new_object() const noexcept;
|
||||
|
||||
public:
|
||||
const Ref<YAML::Emitter> out_;
|
||||
@@ -145,4 +111,4 @@ class Writer {
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
set(REFLECTCPP_JSON @REFLECTCPP_JSON@)
|
||||
set(REFLECTCPP_BSON @REFLECTCPP_BSON@)
|
||||
set(REFLECTCPP_FLEXBUFFERS @REFLECTCPP_FLEXBUFFERS@)
|
||||
set(REFLECTCPP_TOML @REFLECTCPP_TOML@)
|
||||
@@ -7,13 +8,13 @@ set(REFLECTCPP_XML @REFLECTCPP_XML@)
|
||||
set(REFLECTCPP_YAML @REFLECTCPP_YAML@)
|
||||
set(REFLECTCPP_USE_BUNDLED_DEPENDENCIES @REFLECTCPP_USE_BUNDLED_DEPENDENCIES@)
|
||||
|
||||
if(REFLECTCPP_BSON OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML OR NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
|
||||
if(REFLECTCPP_BSON OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML OR (REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES))
|
||||
include(CMakeFindDependencyMacro)
|
||||
endif()
|
||||
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/reflectcpp-exports.cmake)
|
||||
|
||||
if (NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
|
||||
if (REFLECTCPP_JSON AND NOT REFLECTCPP_USE_BUNDLED_DEPENDENCIES)
|
||||
find_dependency(ctre)
|
||||
find_dependency(yyjson)
|
||||
endif()
|
||||
|
||||
+6
-364
@@ -24,368 +24,10 @@ SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/Generic.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
Generic::Generic() : value_(false) {}
|
||||
|
||||
Generic::Generic(Generic&& _other) noexcept = default;
|
||||
|
||||
Generic::Generic(const Generic& _other) = default;
|
||||
|
||||
Generic::Generic(const ReflectionType& _value) : value_(_value) {}
|
||||
|
||||
Generic::Generic(ReflectionType&& _value) noexcept
|
||||
: value_(std::move(_value)) {}
|
||||
|
||||
Generic::~Generic() = default;
|
||||
|
||||
Generic& Generic::operator=(const ReflectionType& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Generic& Generic::operator=(ReflectionType&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Generic& Generic::operator=(const Generic& _other) = default;
|
||||
|
||||
Generic& Generic::operator=(Generic&& _other) = default;
|
||||
|
||||
Result<Generic::Array> Generic::to_array() const noexcept {
|
||||
if (const auto* ptr = std::get_if<Array>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to an "
|
||||
"rfl::Generic::Array.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<bool> Generic::to_bool() const noexcept {
|
||||
if (const auto* ptr = std::get_if<bool>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to a boolean.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<double> Generic::to_double() const noexcept {
|
||||
if (const auto* ptr = std::get_if<double>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to a double.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<int> Generic::to_int() const noexcept {
|
||||
if (const auto* ptr = std::get_if<int>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to an integer.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<Generic::Object> Generic::to_object() const noexcept {
|
||||
if (const auto* ptr = std::get_if<Object>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to an "
|
||||
"rfl::Generic::Object.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::string> Generic::to_string() const noexcept {
|
||||
if (const auto* ptr = std::get_if<std::string>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to a string.");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "rfl/json/to_schema.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
|
||||
|
||||
bool is_optional(const parsing::schema::Type& _t) {
|
||||
const auto handle = [](const auto& _v) -> bool {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
return std::is_same<T, parsing::schema::Type::Optional>();
|
||||
};
|
||||
return rfl::visit(handle, _t.variant_);
|
||||
}
|
||||
|
||||
std::string numeric_type_to_string(const parsing::schema::Type& _type) {
|
||||
const auto handle_variant = [](const auto& _t) -> std::string {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
if constexpr (std::is_same<T, Type::Int32>() ||
|
||||
std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>() ||
|
||||
std::is_same<T, Type::Integer>()) {
|
||||
return schema::Type::Integer{}.type.str();
|
||||
} else {
|
||||
return schema::Type::Number{}.type.str();
|
||||
}
|
||||
};
|
||||
return rfl::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
schema::Type handle_validation_type(
|
||||
const parsing::schema::Type& _type,
|
||||
const parsing::schema::ValidationType& _validation_type) {
|
||||
auto handle_variant = [&](const auto& _v) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
if constexpr (std::is_same<T, ValidationType::AllOf>()) {
|
||||
auto all_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
all_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AllOf{.allOf = all_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::AnyOf>()) {
|
||||
auto any_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
any_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::OneOf>()) {
|
||||
auto one_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
one_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
|
||||
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
|
||||
auto t = type_to_json_schema_type(_type);
|
||||
const auto to_size = [](const auto _v) {
|
||||
return static_cast<size_t>(_v);
|
||||
};
|
||||
auto handle_size_variant = [&](auto& _t, const auto& _size_limit) {
|
||||
using U = std::remove_cvref_t<decltype(_t)>;
|
||||
using V = std::remove_cvref_t<decltype(_size_limit)>;
|
||||
if constexpr (std::is_same<U, schema::Type::TypedArray>() ||
|
||||
std::is_same<U, schema::Type::String>()) {
|
||||
if constexpr (std::is_same<V, ValidationType::Minimum>()) {
|
||||
_t.minSize = _size_limit.value_.visit(to_size);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::Maximum>()) {
|
||||
_t.maxSize = _size_limit.value_.visit(to_size);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::EqualTo>()) {
|
||||
_t.minSize = _size_limit.value_.visit(to_size);
|
||||
_t.maxSize = _size_limit.value_.visit(to_size);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::AnyOf>() ||
|
||||
std::is_same<V, ValidationType::AllOf>()) {
|
||||
V v;
|
||||
for (const auto& limiter : _size_limit.types_) {
|
||||
v.types_.push_back(ValidationType{ValidationType::Size{
|
||||
.size_limit_ = rfl::Ref<ValidationType>::make(limiter)}});
|
||||
}
|
||||
return handle_validation_type(_type, ValidationType{.variant_ = v});
|
||||
}
|
||||
}
|
||||
return t;
|
||||
};
|
||||
|
||||
return rfl::visit(handle_size_variant, t.value, _v.size_limit_->variant_);
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMinimum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMinimum{
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Maximum>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Maximum{
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Minimum>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Minimum{
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::EqualTo>()) {
|
||||
const auto maximum = schema::Type{
|
||||
.value = schema::Type::Maximum{
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
const auto minimum = schema::Type{
|
||||
.value = schema::Type::Minimum{
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
return schema::Type{.value =
|
||||
schema::Type::AllOf{.allOf = {maximum, minimum}}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::NotEqualTo>()) {
|
||||
const auto excl_maximum =
|
||||
schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
const auto excl_minimum =
|
||||
schema::Type{.value = schema::Type::ExclusiveMinimum{
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
return schema::Type{
|
||||
.value = schema::Type::AnyOf{.anyOf = {excl_maximum, excl_minimum}}};
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return rfl::visit(handle_variant, _validation_type.variant_);
|
||||
}
|
||||
|
||||
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
|
||||
auto handle_variant = [](const auto& _t) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
if constexpr (std::is_same<T, Type::Boolean>()) {
|
||||
return schema::Type{.value = schema::Type::Boolean{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Int32>() ||
|
||||
std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>() ||
|
||||
std::is_same<T, Type::Integer>()) {
|
||||
return schema::Type{.value = schema::Type::Integer{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Float>() ||
|
||||
std::is_same<T, Type::Double>()) {
|
||||
return schema::Type{.value = schema::Type::Number{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::String>()) {
|
||||
return schema::Type{.value = schema::Type::String{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
|
||||
auto any_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
any_of.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Description>()) {
|
||||
auto res = type_to_json_schema_type(*_t.type_);
|
||||
const auto update_prediction = [&](auto _v) -> schema::Type {
|
||||
_v.description = _t.description_;
|
||||
return schema::Type{_v};
|
||||
};
|
||||
return rfl::visit(update_prediction, res.value);
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
|
||||
return schema::Type{.value = schema::Type::FixedSizeTypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_)),
|
||||
.minItems = _t.size_,
|
||||
.maxItems = _t.size_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Literal>()) {
|
||||
return schema::Type{.value =
|
||||
schema::Type::StringEnum{.values = _t.values_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Object>()) {
|
||||
auto properties = std::map<std::string, schema::Type>();
|
||||
auto required = std::vector<std::string>();
|
||||
for (const auto& [k, v] : _t.types_) {
|
||||
properties[k] = type_to_json_schema_type(v);
|
||||
if (!is_optional(v)) {
|
||||
required.push_back(k);
|
||||
}
|
||||
}
|
||||
auto additional_properties =
|
||||
_t.additional_properties_
|
||||
? std::make_shared<schema::Type>(
|
||||
type_to_json_schema_type(*_t.additional_properties_))
|
||||
: std::shared_ptr<schema::Type>();
|
||||
return schema::Type{.value = schema::Type::Object{
|
||||
.properties = properties,
|
||||
.required = required,
|
||||
.additionalProperties = additional_properties}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Optional>()) {
|
||||
return schema::Type{.value = schema::Type::AnyOf{
|
||||
.anyOf = {type_to_json_schema_type(*_t.type_),
|
||||
schema::Type{schema::Type::Null{}}}}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Reference>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::StringMap>()) {
|
||||
return schema::Type{.value = schema::Type::StringMap{
|
||||
.additionalProperties = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.value_type_))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Tuple>()) {
|
||||
auto items = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
items.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::Tuple{.prefixItems = items}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
|
||||
return schema::Type{.value = schema::Type::TypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Validated>()) {
|
||||
return handle_validation_type(*_t.type_, _t.validation_);
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return rfl::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
} // namespace rfl::json
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#include "rfl/parsing/schema/Type.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
/// Requires a lot of template instantiation, so we do not want this to be
|
||||
/// inlined.
|
||||
|
||||
Type::Type() : variant_() {}
|
||||
|
||||
Type::Type(const VariantType& _variant) : variant_(_variant) {}
|
||||
|
||||
Type::~Type() = default;
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
#include "rfl/Generic.cpp"
|
||||
#include "rfl/parsing/schema/Type.cpp"
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/bson/Reader.cpp"
|
||||
#include "rfl/bson/Writer.cpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/cbor/Reader.cpp"
|
||||
#include "rfl/cbor/Writer.cpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/flexbuf/Reader.cpp"
|
||||
#include "rfl/flexbuf/Writer.cpp"
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/json/Reader.cpp"
|
||||
#include "rfl/json/Writer.cpp"
|
||||
#include "rfl/json/to_schema.cpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/msgpack/Reader.cpp"
|
||||
#include "rfl/msgpack/Writer.cpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/toml/Reader.cpp"
|
||||
#include "rfl/toml/Writer.cpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/xml/Reader.cpp"
|
||||
#include "rfl/xml/Writer.cpp"
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
// This file include all other source files, so that the user of the library
|
||||
// don't need to add multiple source files into their build.
|
||||
// Also, this speeds up compile time, compared to multiple separate .cpp files
|
||||
// compilation.
|
||||
|
||||
#include "rfl/yaml/Reader.cpp"
|
||||
#include "rfl/yaml/Writer.cpp"
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/Generic.hpp"
|
||||
|
||||
namespace rfl {
|
||||
|
||||
Generic::Generic() : value_(false) {}
|
||||
|
||||
Generic::Generic(Generic&& _other) noexcept = default;
|
||||
|
||||
Generic::Generic(const Generic& _other) = default;
|
||||
|
||||
Generic::Generic(const ReflectionType& _value) : value_(_value) {}
|
||||
|
||||
Generic::Generic(ReflectionType&& _value) noexcept
|
||||
: value_(std::move(_value)) {}
|
||||
|
||||
Generic::~Generic() = default;
|
||||
|
||||
Generic& Generic::operator=(const ReflectionType& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Generic& Generic::operator=(ReflectionType&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Generic& Generic::operator=(const Generic& _other) = default;
|
||||
|
||||
Generic& Generic::operator=(Generic&& _other) = default;
|
||||
|
||||
Result<Generic::Array> Generic::to_array() const noexcept {
|
||||
if (const auto* ptr = std::get_if<Array>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to an "
|
||||
"rfl::Generic::Array.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<bool> Generic::to_bool() const noexcept {
|
||||
if (const auto* ptr = std::get_if<bool>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to a boolean.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<double> Generic::to_double() const noexcept {
|
||||
if (const auto* ptr = std::get_if<double>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to a double.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<int> Generic::to_int() const noexcept {
|
||||
if (const auto* ptr = std::get_if<int>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to an integer.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<Generic::Object> Generic::to_object() const noexcept {
|
||||
if (const auto* ptr = std::get_if<Object>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to an "
|
||||
"rfl::Generic::Object.");
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::string> Generic::to_string() const noexcept {
|
||||
if (const auto* ptr = std::get_if<std::string>(&value_)) {
|
||||
return *ptr;
|
||||
} else {
|
||||
return Error(
|
||||
"rfl::Generic: Could not cast the underlying value to a string.");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
@@ -0,0 +1,72 @@
|
||||
#include "rfl/bson/Reader.hpp"
|
||||
|
||||
namespace rfl::bson {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _arr.val_->value.v_doc;
|
||||
if (bson_init_static(&b, doc.data, doc.data_len)) {
|
||||
if (bson_iter_init(&iter, &b)) {
|
||||
size_t i = 0;
|
||||
while (bson_iter_next(&iter)) {
|
||||
if (i == _idx) {
|
||||
return to_input_var(&iter);
|
||||
}
|
||||
++i;
|
||||
}
|
||||
} else {
|
||||
return Error("Could not init the array iteration.");
|
||||
}
|
||||
} else {
|
||||
return Error("Could not init array.");
|
||||
}
|
||||
return Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
bson_t b;
|
||||
bson_iter_t iter;
|
||||
const auto doc = _obj.val_->value.v_doc;
|
||||
if (bson_init_static(&b, doc.data, doc.data_len)) {
|
||||
if (bson_iter_init(&iter, &b)) {
|
||||
while (bson_iter_next(&iter)) {
|
||||
auto key = std::string(bson_iter_key(&iter));
|
||||
if (key == _name) {
|
||||
return to_input_var(&iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.val_->value_type == BSON_TYPE_NULL;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto btype = _var.val_->value_type;
|
||||
if (btype != BSON_TYPE_ARRAY && btype != BSON_TYPE_DOCUMENT) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return InputArrayType{_var.val_};
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto btype = _var.val_->value_type;
|
||||
if (btype != BSON_TYPE_DOCUMENT) {
|
||||
return Error("Could not cast to a document.");
|
||||
}
|
||||
return InputObjectType{_var.val_};
|
||||
}
|
||||
|
||||
Reader::InputVarType Reader::to_input_var(bson_iter_t* _iter) const noexcept {
|
||||
return InputVarType{bson_iter_value(_iter)};
|
||||
}
|
||||
|
||||
} // namespace rfl::bson
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "rfl/bson/Writer.hpp"
|
||||
|
||||
namespace rfl::bson {
|
||||
|
||||
Writer::Writer(bson_t* _doc) : doc_(_doc) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
bson_array_builder_t* val = bson_array_builder_new();
|
||||
return OutputArrayType(val, IsRoot{});
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return OutputObjectType(doc_, IsRoot{});
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
// Appears to be unsupported by the BSON C API.
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
bson_array_builder_t* val;
|
||||
bson_array_builder_append_array_builder_begin(_parent->val_, &val);
|
||||
return OutputArrayType(val, IsArray{_parent->val_});
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
bson_array_builder_t* val;
|
||||
bson_append_array_builder_begin(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()), &val);
|
||||
return OutputArrayType(val, IsObject{_parent->val_});
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
subdocs_->emplace_back(rfl::Box<BSONType>());
|
||||
bson_array_builder_append_document_begin(_parent->val_,
|
||||
&(subdocs_->back()->val_));
|
||||
return OutputObjectType(&subdocs_->back()->val_, IsArray{_parent->val_});
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
subdocs_->emplace_back(rfl::Box<BSONType>());
|
||||
bson_append_document_begin(_parent->val_, _name.data(),
|
||||
static_cast<int>(_name.size()),
|
||||
&(subdocs_->back()->val_));
|
||||
return OutputObjectType(&subdocs_->back()->val_, IsObject{_parent->val_});
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
bson_array_builder_append_null(_parent->val_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
bson_append_null(_parent->val_, _name.data(), static_cast<int>(_name.size()));
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {
|
||||
const auto handle = [&](const auto _parent) {
|
||||
using Type = std::remove_cvref_t<decltype(_parent)>;
|
||||
if constexpr (std::is_same<Type, IsArray>()) {
|
||||
bson_array_builder_append_array_builder_end(_parent.ptr_, _arr->val_);
|
||||
} else if constexpr (std::is_same<Type, IsObject>()) {
|
||||
bson_append_array_builder_end(_parent.ptr_, _arr->val_);
|
||||
} else if constexpr (std::is_same<Type, IsRoot>()) {
|
||||
bson_array_builder_build(_arr->val_, doc_);
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
|
||||
}
|
||||
};
|
||||
std::visit(handle, _arr->parent_);
|
||||
}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {
|
||||
const auto handle = [&](const auto _parent) {
|
||||
using Type = std::remove_cvref_t<decltype(_parent)>;
|
||||
if constexpr (std::is_same<Type, IsArray>()) {
|
||||
bson_array_builder_append_document_end(_parent.ptr_, _obj->val_);
|
||||
} else if constexpr (std::is_same<Type, IsObject>()) {
|
||||
bson_append_document_end(_parent.ptr_, _obj->val_);
|
||||
} else if constexpr (std::is_same<Type, IsRoot>()) {
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<Type>, "Unsupported type.");
|
||||
}
|
||||
};
|
||||
std::visit(handle, _obj->parent_);
|
||||
}
|
||||
|
||||
} // namespace rfl::bson
|
||||
@@ -0,0 +1,116 @@
|
||||
#include "rfl/cbor/Reader.hpp"
|
||||
|
||||
namespace rfl::cbor {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
InputVarType var;
|
||||
auto err = cbor_value_enter_container(&_arr.val_, &var.val_);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_array_length(&_arr.val_, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_idx >= length) {
|
||||
return Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
for (size_t i = 0; i < _idx; ++i) {
|
||||
err = cbor_value_advance(&var.val_);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
InputVarType var;
|
||||
auto buffer = std::string();
|
||||
auto err = cbor_value_enter_container(&_obj.val_, &var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
size_t length = 0;
|
||||
err = cbor_value_get_map_length(&_obj.val_, &length);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (!cbor_value_is_text_string(&var.val_)) {
|
||||
return Error("Expected the key to be a string value.");
|
||||
}
|
||||
err = get_string(&var.val_, &buffer);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
err = cbor_value_advance(&var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
if (_name == buffer) {
|
||||
return var;
|
||||
}
|
||||
err = cbor_value_advance(&var.val_);
|
||||
if (err != CborNoError) {
|
||||
return Error(cbor_error_string(err));
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return cbor_value_is_null(&_var.val_);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_array(&_var.val_)) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return InputArrayType{_var.val_};
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!cbor_value_is_map(&_var.val_)) {
|
||||
return Error("Could not cast to an object.");
|
||||
}
|
||||
return InputObjectType{_var.val_};
|
||||
}
|
||||
|
||||
CborError Reader::get_bytestring(const CborValue* _ptr,
|
||||
rfl::Bytestring* _str) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_str->resize(length);
|
||||
if (length > 0) {
|
||||
return cbor_value_copy_byte_string(
|
||||
_ptr, reinterpret_cast<uint8_t*>(&((*_str)[0])), &length, NULL);
|
||||
} else {
|
||||
return CborNoError;
|
||||
}
|
||||
}
|
||||
|
||||
CborError Reader::get_string(const CborValue* _ptr,
|
||||
std::string* _str) const noexcept {
|
||||
size_t length = 0;
|
||||
auto err = cbor_value_get_string_length(_ptr, &length);
|
||||
if (err != CborNoError && err != CborErrorOutOfMemory) {
|
||||
return err;
|
||||
}
|
||||
_str->resize(length);
|
||||
if (length > 0) {
|
||||
return cbor_value_copy_text_string(_ptr, &((*_str)[0]), &length, NULL);
|
||||
} else {
|
||||
return CborNoError;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rfl::cbor
|
||||
@@ -0,0 +1,83 @@
|
||||
#include "rfl/cbor/Writer.hpp"
|
||||
|
||||
namespace rfl::cbor {
|
||||
|
||||
Writer::Writer(CborEncoder* _encoder) : encoder_(_encoder) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_array(_size, encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_object(_size, encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
cbor_encode_null(encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_array(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
return new_object(_size, _parent->encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
cbor_encode_text_string(_parent->encoder_, _name.data(), _name.size());
|
||||
cbor_encode_null(_parent->encoder_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {
|
||||
cbor_encoder_close_container(_arr->parent_, _arr->encoder_);
|
||||
}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {
|
||||
cbor_encoder_close_container(_obj->parent_, _obj->encoder_);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(const size_t _size,
|
||||
CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_array(_parent, subencoders_->back().get(), _size);
|
||||
return OutputArrayType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(
|
||||
const size_t _size, CborEncoder* _parent) const noexcept {
|
||||
subencoders_->emplace_back(rfl::Box<CborEncoder>::make());
|
||||
cbor_encoder_create_map(_parent, subencoders_->back().get(), _size);
|
||||
return OutputObjectType{subencoders_->back().get(), _parent};
|
||||
}
|
||||
|
||||
} // namespace rfl::cbor
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "rfl/flexbuf/Reader.hpp"
|
||||
|
||||
namespace rfl::flexbuf {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
if (_idx >= _arr.size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr[_idx];
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
const auto keys = _obj.Keys();
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
if (_name == keys[i].AsString().c_str()) {
|
||||
return _obj.Values()[i];
|
||||
}
|
||||
}
|
||||
return rfl::Error("Map does not contain any element called '" + _name + "'.");
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.IsNull();
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.IsVector()) {
|
||||
return rfl::Error("Could not cast to Vector.");
|
||||
}
|
||||
return _var.AsVector();
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.IsMap()) {
|
||||
return rfl::Error("Could not cast to Map!");
|
||||
}
|
||||
return _var.AsMap();
|
||||
}
|
||||
|
||||
} // namespace rfl::flexbuf
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "rfl/flexbuf/Writer.hpp"
|
||||
|
||||
namespace rfl::flexbuf {
|
||||
|
||||
Writer::Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
fbb_->Null(_name.data());
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {
|
||||
fbb_->EndVector(_arr->start_, false, false);
|
||||
}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {
|
||||
fbb_->EndMap(_obj->start_);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(
|
||||
const std::string_view& _name) const noexcept {
|
||||
const auto start = fbb_->StartVector(_name.data());
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array() const noexcept {
|
||||
const auto start = fbb_->StartVector();
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(
|
||||
const std::string_view& _name) const noexcept {
|
||||
const auto start = fbb_->StartMap(_name.data());
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object() const noexcept {
|
||||
const auto start = fbb_->StartMap();
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
} // namespace rfl::flexbuf
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/json/Reader.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
const auto var = InputVarType(yyjson_arr_get(_arr.val_, _idx));
|
||||
if (!var.val_) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
const auto var = InputVarType(yyjson_obj_get(_obj.val_, _name.c_str()));
|
||||
if (!var.val_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType _var) const noexcept {
|
||||
return !_var.val_ || yyjson_is_null(_var.val_);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType _var) const noexcept {
|
||||
if (!yyjson_is_arr(_var.val_)) {
|
||||
return rfl::Error("Could not cast to array!");
|
||||
}
|
||||
return InputArrayType(_var.val_);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType _var) const noexcept {
|
||||
if (!yyjson_is_obj(_var.val_)) {
|
||||
return rfl::Error("Could not cast to object!");
|
||||
}
|
||||
return InputObjectType(_var.val_);
|
||||
}
|
||||
|
||||
} // namespace rfl::json
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/json/Writer.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
Writer::Writer(yyjson_mut_doc* _doc) : doc_(_doc) {}
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(const size_t) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(const size_t) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t, OutputArrayType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t, OutputArrayType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()), obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.data()),
|
||||
null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType*) const noexcept {}
|
||||
|
||||
void Writer::end_object(OutputObjectType*) const noexcept {}
|
||||
|
||||
} // namespace rfl::json
|
||||
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/json/to_schema.hpp"
|
||||
|
||||
namespace rfl::json {
|
||||
|
||||
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type);
|
||||
|
||||
bool is_optional(const parsing::schema::Type& _t) {
|
||||
const auto handle = [](const auto& _v) -> bool {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
return std::is_same<T, parsing::schema::Type::Optional>();
|
||||
};
|
||||
return rfl::visit(handle, _t.variant_);
|
||||
}
|
||||
|
||||
std::string numeric_type_to_string(const parsing::schema::Type& _type) {
|
||||
const auto handle_variant = [](const auto& _t) -> std::string {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
if constexpr (std::is_same<T, Type::Int32>() ||
|
||||
std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>() ||
|
||||
std::is_same<T, Type::Integer>()) {
|
||||
return schema::Type::Integer{}.type.str();
|
||||
} else {
|
||||
return schema::Type::Number{}.type.str();
|
||||
}
|
||||
};
|
||||
return rfl::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
schema::Type handle_validation_type(
|
||||
const parsing::schema::Type& _type,
|
||||
const parsing::schema::ValidationType& _validation_type) {
|
||||
auto handle_variant = [&](const auto& _v) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
using ValidationType = parsing::schema::ValidationType;
|
||||
if constexpr (std::is_same<T, ValidationType::AllOf>()) {
|
||||
auto all_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
all_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AllOf{.allOf = all_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::AnyOf>()) {
|
||||
auto any_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
any_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::OneOf>()) {
|
||||
auto one_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _v.types_) {
|
||||
one_of.emplace_back(handle_validation_type(_type, t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::OneOf{.oneOf = one_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Regex>()) {
|
||||
return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Size>()) {
|
||||
auto t = type_to_json_schema_type(_type);
|
||||
const auto to_size = [](const auto _v) {
|
||||
return static_cast<size_t>(_v);
|
||||
};
|
||||
auto handle_size_variant = [&](auto& _t, const auto& _size_limit) {
|
||||
using U = std::remove_cvref_t<decltype(_t)>;
|
||||
using V = std::remove_cvref_t<decltype(_size_limit)>;
|
||||
if constexpr (std::is_same<U, schema::Type::TypedArray>() ||
|
||||
std::is_same<U, schema::Type::String>()) {
|
||||
if constexpr (std::is_same<V, ValidationType::Minimum>()) {
|
||||
_t.minSize = _size_limit.value_.visit(to_size);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::Maximum>()) {
|
||||
_t.maxSize = _size_limit.value_.visit(to_size);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::EqualTo>()) {
|
||||
_t.minSize = _size_limit.value_.visit(to_size);
|
||||
_t.maxSize = _size_limit.value_.visit(to_size);
|
||||
return t;
|
||||
|
||||
} else if constexpr (std::is_same<V, ValidationType::AnyOf>() ||
|
||||
std::is_same<V, ValidationType::AllOf>()) {
|
||||
V v;
|
||||
for (const auto& limiter : _size_limit.types_) {
|
||||
v.types_.push_back(ValidationType{ValidationType::Size{
|
||||
.size_limit_ = rfl::Ref<ValidationType>::make(limiter)}});
|
||||
}
|
||||
return handle_validation_type(_type, ValidationType{.variant_ = v});
|
||||
}
|
||||
}
|
||||
return t;
|
||||
};
|
||||
|
||||
return rfl::visit(handle_size_variant, t.value, _v.size_limit_->variant_);
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMaximum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::ExclusiveMinimum>()) {
|
||||
return schema::Type{.value = schema::Type::ExclusiveMinimum{
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Maximum>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Maximum{
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::Minimum>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Minimum{
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::EqualTo>()) {
|
||||
const auto maximum = schema::Type{
|
||||
.value = schema::Type::Maximum{
|
||||
.maximum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
const auto minimum = schema::Type{
|
||||
.value = schema::Type::Minimum{
|
||||
.minimum = _v.value_, .type = numeric_type_to_string(_type)}};
|
||||
return schema::Type{.value =
|
||||
schema::Type::AllOf{.allOf = {maximum, minimum}}};
|
||||
|
||||
} else if constexpr (std::is_same<T, ValidationType::NotEqualTo>()) {
|
||||
const auto excl_maximum =
|
||||
schema::Type{.value = schema::Type::ExclusiveMaximum{
|
||||
.exclusiveMaximum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
const auto excl_minimum =
|
||||
schema::Type{.value = schema::Type::ExclusiveMinimum{
|
||||
.exclusiveMinimum = _v.value_,
|
||||
.type = numeric_type_to_string(_type)}};
|
||||
return schema::Type{
|
||||
.value = schema::Type::AnyOf{.anyOf = {excl_maximum, excl_minimum}}};
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return rfl::visit(handle_variant, _validation_type.variant_);
|
||||
}
|
||||
|
||||
schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
|
||||
auto handle_variant = [](const auto& _t) -> schema::Type {
|
||||
using T = std::remove_cvref_t<decltype(_t)>;
|
||||
using Type = parsing::schema::Type;
|
||||
if constexpr (std::is_same<T, Type::Boolean>()) {
|
||||
return schema::Type{.value = schema::Type::Boolean{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Int32>() ||
|
||||
std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>() ||
|
||||
std::is_same<T, Type::Integer>()) {
|
||||
return schema::Type{.value = schema::Type::Integer{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Float>() ||
|
||||
std::is_same<T, Type::Double>()) {
|
||||
return schema::Type{.value = schema::Type::Number{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::String>()) {
|
||||
return schema::Type{.value = schema::Type::String{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
|
||||
auto any_of = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
any_of.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Description>()) {
|
||||
auto res = type_to_json_schema_type(*_t.type_);
|
||||
const auto update_prediction = [&](auto _v) -> schema::Type {
|
||||
_v.description = _t.description_;
|
||||
return schema::Type{_v};
|
||||
};
|
||||
return rfl::visit(update_prediction, res.value);
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
|
||||
return schema::Type{.value = schema::Type::FixedSizeTypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_)),
|
||||
.minItems = _t.size_,
|
||||
.maxItems = _t.size_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Literal>()) {
|
||||
return schema::Type{.value =
|
||||
schema::Type::StringEnum{.values = _t.values_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Object>()) {
|
||||
auto properties = std::map<std::string, schema::Type>();
|
||||
auto required = std::vector<std::string>();
|
||||
for (const auto& [k, v] : _t.types_) {
|
||||
properties[k] = type_to_json_schema_type(v);
|
||||
if (!is_optional(v)) {
|
||||
required.push_back(k);
|
||||
}
|
||||
}
|
||||
auto additional_properties =
|
||||
_t.additional_properties_
|
||||
? std::make_shared<schema::Type>(
|
||||
type_to_json_schema_type(*_t.additional_properties_))
|
||||
: std::shared_ptr<schema::Type>();
|
||||
return schema::Type{.value = schema::Type::Object{
|
||||
.properties = properties,
|
||||
.required = required,
|
||||
.additionalProperties = additional_properties}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Optional>()) {
|
||||
return schema::Type{.value = schema::Type::AnyOf{
|
||||
.anyOf = {type_to_json_schema_type(*_t.type_),
|
||||
schema::Type{schema::Type::Null{}}}}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Reference>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::StringMap>()) {
|
||||
return schema::Type{.value = schema::Type::StringMap{
|
||||
.additionalProperties = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.value_type_))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Tuple>()) {
|
||||
auto items = std::vector<schema::Type>();
|
||||
for (const auto& t : _t.types_) {
|
||||
items.emplace_back(type_to_json_schema_type(t));
|
||||
}
|
||||
return schema::Type{.value = schema::Type::Tuple{.prefixItems = items}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
|
||||
return schema::Type{.value = schema::Type::TypedArray{
|
||||
.items = Ref<schema::Type>::make(
|
||||
type_to_json_schema_type(*_t.type_))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Validated>()) {
|
||||
return handle_validation_type(*_t.type_, _t.validation_);
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return rfl::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
std::string to_schema_internal_schema(
|
||||
const parsing::schema::Definition& internal_schema,
|
||||
const yyjson_write_flag _flag) {
|
||||
auto definitions = std::map<std::string, schema::Type>();
|
||||
for (const auto& [k, v] : internal_schema.definitions_) {
|
||||
definitions[k] = type_to_json_schema_type(v);
|
||||
}
|
||||
using JSONSchemaType =
|
||||
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
|
||||
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
|
||||
using U = std::decay_t<decltype(_root)>;
|
||||
return schema::JSONSchema<U>{.root = std::move(_root),
|
||||
.definitions = definitions};
|
||||
};
|
||||
auto root = type_to_json_schema_type(internal_schema.root_);
|
||||
const auto json_schema = rfl::visit(to_schema, std::move(root.value));
|
||||
return write(json_schema, _flag);
|
||||
}
|
||||
|
||||
} // namespace rfl::json
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "rfl/msgpack/Reader.hpp"
|
||||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
if (_idx >= _arr.size) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr.ptr[_idx];
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
}
|
||||
const auto current_name =
|
||||
std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
if (_name == current_name) {
|
||||
return _obj.ptr[i].val;
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.type == MSGPACK_OBJECT_NIL;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_ARRAY) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return _var.via.array;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
return _var.via.map;
|
||||
}
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
@@ -0,0 +1,78 @@
|
||||
#include "rfl/msgpack/Writer.hpp"
|
||||
|
||||
namespace rfl::msgpack {
|
||||
|
||||
Writer::Writer(msgpack_packer* _pk) : pk_(_pk) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_array(_size);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_object(_size);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(const size_t _size) const noexcept {
|
||||
msgpack_pack_array(pk_, _size);
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(const size_t _size) const noexcept {
|
||||
msgpack_pack_map(pk_, _size);
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/parsing/schema/Type.hpp"
|
||||
|
||||
namespace rfl::parsing::schema {
|
||||
|
||||
/// Requires a lot of template instantiation, so we do not want this to be
|
||||
/// inlined.
|
||||
|
||||
Type::Type() : variant_() {}
|
||||
|
||||
Type::Type(const VariantType& _variant) : variant_(_variant) {}
|
||||
|
||||
Type::~Type() = default;
|
||||
|
||||
} // namespace rfl::parsing::schema
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "rfl/toml/Reader.hpp"
|
||||
|
||||
namespace rfl::toml {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
if (_idx >= _arr->size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr->get(_idx);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = (*_obj)[_name];
|
||||
if (!var) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var.node();
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var && true;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto ptr = _var->as_array();
|
||||
if (!ptr) {
|
||||
return rfl::Error("Could not cast to an array!");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto ptr = _var->as_table();
|
||||
if (!ptr) {
|
||||
return rfl::Error("Could not cast to a table!");
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
} // namespace rfl::toml
|
||||
@@ -0,0 +1,62 @@
|
||||
#include "rfl/toml/Writer.hpp"
|
||||
|
||||
namespace rfl::toml {
|
||||
|
||||
Writer::Writer(::toml::table* _root) : root_(_root) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return OutputObjectType{root_};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto i = _parent->val_->size();
|
||||
_parent->val_->push_back(::toml::array());
|
||||
return OutputArrayType{_parent->val_->at(i).as_array()};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::array());
|
||||
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto i = _parent->val_->size();
|
||||
_parent->val_->push_back(::toml::table());
|
||||
return OutputObjectType{_parent->val_->at(i).as_table()};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::table());
|
||||
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
_parent->val_->push_back(std::string(""));
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
_parent->val_->emplace(_name, ::toml::value(std::string("")));
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
} // namespace rfl::toml
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/xml/Reader.hpp"
|
||||
|
||||
namespace rfl::xml {
|
||||
|
||||
/// XML-only helper function. This is needed because XML distinguishes between
|
||||
/// nodes and attributes.
|
||||
static rfl::Result<pugi::xml_node> cast_as_node(
|
||||
const std::variant<pugi::xml_node, pugi::xml_attribute>&
|
||||
_node_or_attribute) {
|
||||
const auto cast = [](const auto& _n) -> Result<pugi::xml_node> {
|
||||
using Type = std::remove_cvref_t<decltype(_n)>;
|
||||
if constexpr (std::is_same<Type, pugi::xml_node>()) {
|
||||
return _n;
|
||||
} else {
|
||||
return Error("Field '" + std::string(_n.name()) + "' is an attribute.");
|
||||
}
|
||||
};
|
||||
return std::visit(cast, _node_or_attribute);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
const auto name = _arr.node_.name();
|
||||
size_t i = 0;
|
||||
for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
|
||||
if (i == _idx) {
|
||||
return InputVarType(node);
|
||||
}
|
||||
}
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
const auto node = _obj.node_.child(_name.c_str());
|
||||
if (!node) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return InputVarType(node);
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType _var) const noexcept {
|
||||
const auto wrap = [](const auto& _node) { return !_node; };
|
||||
return std::visit(cast_as_node, _var.node_or_attribute_)
|
||||
.transform(wrap)
|
||||
.value_or(false);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType _var) const noexcept {
|
||||
const auto wrap = [](const auto& _node) { return InputArrayType(_node); };
|
||||
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType _var) const noexcept {
|
||||
const auto wrap = [](const auto& _node) { return InputObjectType(_node); };
|
||||
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
|
||||
}
|
||||
|
||||
} // namespace rfl::xml
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2024 Code17 GmbH
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
#include "rfl/xml/Writer.hpp"
|
||||
|
||||
#include <pugixml.hpp>
|
||||
|
||||
namespace rfl::xml {
|
||||
|
||||
static constexpr const char* XML_CONTENT = "xml_content";
|
||||
|
||||
Writer::Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
|
||||
: root_(_root), root_name_(_root_name) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputArrayType(root_name_, node_child);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::value_as_root_impl(
|
||||
const std::string& _str) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return *_parent;
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return OutputArrayType(_name, _parent->node_);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_value_to_array_impl(
|
||||
const std::string& _str, OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.data()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_value_to_object_impl(
|
||||
const std::string_view& _name, const std::string& _str,
|
||||
OutputObjectType* _parent, const bool _is_attribute) const noexcept {
|
||||
if (_is_attribute) {
|
||||
_parent->node_->append_attribute(_name.data()) = _str.c_str();
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
_parent->node_->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(_str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.data()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.data()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent,
|
||||
const bool _is_attribute) const noexcept {
|
||||
if (_is_attribute) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
} // namespace rfl::xml
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "rfl/yaml/Reader.hpp"
|
||||
|
||||
namespace rfl::yaml {
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_array(
|
||||
const size_t _idx, const InputArrayType& _arr) const noexcept {
|
||||
if (_idx >= _arr.node_.size()) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return InputVarType(_arr.node_[_idx]);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputVarType> Reader::get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = InputVarType(_obj.node_[_name]);
|
||||
if (!var.node_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.node_ && true;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsSequence()) {
|
||||
return rfl::Error("Could not cast to sequence!");
|
||||
}
|
||||
return InputArrayType(_var.node_);
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputObjectType> Reader::to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsMap()) {
|
||||
return rfl::Error("Could not cast to map!");
|
||||
}
|
||||
return InputObjectType(_var.node_);
|
||||
}
|
||||
|
||||
} // namespace rfl::yaml
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "rfl/yaml/Writer.hpp"
|
||||
|
||||
namespace rfl::yaml {
|
||||
|
||||
Writer::Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, YAML::Null);
|
||||
}
|
||||
|
||||
void Writer::end_array(OutputArrayType* _arr) const noexcept {
|
||||
(*out_) << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void Writer::end_object(OutputObjectType* _obj) const noexcept {
|
||||
(*out_) << YAML::EndMap;
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array(
|
||||
const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::new_array() const noexcept {
|
||||
(*out_) << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object(
|
||||
const std::string_view& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name.data() << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::new_object() const noexcept {
|
||||
(*out_) << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
} // namespace rfl::yaml
|
||||
@@ -6,8 +6,10 @@ else()
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wno-sign-compare -Wno-missing-braces -Wno-psabi -pthread -fno-strict-aliasing -fwrapv -O2 -ftemplate-backtrace-limit=0 -fsanitize=undefined")
|
||||
endif()
|
||||
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(json_c_arrays_and_inheritance)
|
||||
if (REFLECTCPP_JSON)
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(json_c_arrays_and_inheritance)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_BSON)
|
||||
add_subdirectory(bson)
|
||||
|
||||
Reference in New Issue
Block a user