Make sure avro ints and floats are always written properly; fixes #522 (#532)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-10-25 19:55:08 +02:00
committed by GitHub
parent d0f60b214f
commit 3ae683387e
72 changed files with 346 additions and 289 deletions
+4 -4
View File
@@ -48,13 +48,13 @@ template <class ProcessorsType>
requires AreReaderAndWriter<avro::Reader, avro::Writer, Generic>
struct Parser<avro::Reader, avro::Writer, Generic, ProcessorsType> {
template <class T>
static Result<Generic> read(const avro::Reader&, const T&) noexcept {
static Result<Generic> read(const avro::Reader&, const T&) {
static_assert(always_false_v<T>, "Generics are unsupported in Avro.");
return error("Unsupported");
}
template <class P>
static void write(const avro::Writer&, const Generic&, const P&) noexcept {
static void write(const avro::Writer&, const Generic&, const P&) {
static_assert(always_false_v<P>, "Generics are unsupported in Avro.");
}
@@ -78,7 +78,7 @@ struct Parser<avro::Reader, avro::Writer,
template <class U>
static Result<internal::Skip<T, _skip_serialization, _skip_deserialization>>
read(const R&, const U&) noexcept {
read(const R&, const U&) {
static_assert(always_false_v<T>, "rfl::Skip is unsupported in Avro.");
return Error("Unsupported");
}
@@ -87,7 +87,7 @@ struct Parser<avro::Reader, avro::Writer,
static void write(const W& /*_w*/,
const internal::Skip<T, _skip_serialization,
_skip_deserialization>& /*_skip*/,
const P& /*_parent*/) noexcept {
const P& /*_parent*/) {
static_assert(always_false_v<P>, "rfl::Skip is unsupported in Avro.");
}
+24 -31
View File
@@ -9,10 +9,9 @@
#include <string_view>
#include <type_traits>
#include "../Bytestring.hpp"
#include "../Result.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../concepts.hpp"
#include "../internal/is_literal.hpp"
#include "../parsing/schemaful/IsSchemafulReader.hpp"
@@ -67,22 +66,15 @@ struct Reader {
return std::string("");
}
return std::string(c_str, size - 1);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
} else if constexpr (concepts::MutableContiguousByteContainer<
std::remove_cvref_t<T>>) {
using VectorType = std::remove_cvref_t<T>;
using ValueType = typename VectorType::value_type;
const void* ptr = nullptr;
size_t size = 0;
const auto err = avro_value_get_bytes(_var.val_, &ptr, &size);
if (err) {
if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
return error("Could not cast to bytestring.");
} else {
return error("Could not cast to vectorstring.");
}
return error("Could not cast to bytestring.");
}
const auto data = internal::ptr_cast<const ValueType*>(ptr);
return VectorType(data, data + size);
@@ -93,7 +85,8 @@ struct Reader {
int result_value = 0;
int result = avro_value_get_boolean(_var.val_, &result_value);
if (result != 0) {
return error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
return error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return (result_value != 0);
@@ -191,16 +184,16 @@ struct Reader {
const InputArrayType& _arr) const noexcept {
size_t size = 0;
int res = avro_value_get_size(_arr.val_, &size);
if(res)
{
return Error(std::string(__FUNCTION__) + " error(" + std::to_string(res) + "): " + avro_strerror());
if (res) {
return Error(std::string(__FUNCTION__) + " error(" + std::to_string(res) +
"): " + avro_strerror());
}
for (size_t ix = 0; ix < size; ++ix) {
avro_value_t element;
res = avro_value_get_by_index(_arr.val_, ix, &element, nullptr);
if(res)
{
return Error(std::string(__FUNCTION__) + " error(" + std::to_string(res) + "): " + avro_strerror());
if (res) {
return Error(std::string(__FUNCTION__) + " error(" +
std::to_string(res) + "): " + avro_strerror());
}
const auto err = _array_reader.read(InputVarType{&element});
if (err) {
@@ -215,17 +208,17 @@ struct Reader {
const InputMapType& _map) const noexcept {
size_t size = 0;
int res = avro_value_get_size(_map.val_, &size);
if(res!=0)
{
return Error(std::string(__FUNCTION__) + " error("+ std::to_string(res)+"): " + avro_strerror());
if (res != 0) {
return Error(std::string(__FUNCTION__) + " error(" + std::to_string(res) +
"): " + avro_strerror());
}
for (size_t ix = 0; ix < size; ++ix) {
avro_value_t element;
const char* key = nullptr;
res = avro_value_get_by_index(_map.val_, ix, &element, &key);
if(res!=0)
{
return Error(std::string(__FUNCTION__) + " error("+ std::to_string(res)+"): " + avro_strerror());
if (res != 0) {
return Error(std::string(__FUNCTION__) + " error(" +
std::to_string(res) + "): " + avro_strerror());
}
_map_reader.read(std::string_view(key), InputVarType{&element});
}
@@ -237,16 +230,16 @@ struct Reader {
const InputObjectType& _obj) const noexcept {
size_t size = 0;
int res = avro_value_get_size(_obj.val_, &size);
if(res!=0)
{
return Error(std::string(__FUNCTION__) + " error("+ std::to_string(res)+"): " + avro_strerror());
if (res != 0) {
return Error(std::string(__FUNCTION__) + " error(" + std::to_string(res) +
"): " + avro_strerror());
}
for (size_t ix = 0; ix < size; ++ix) {
avro_value_t element;
res = avro_value_get_by_index(_obj.val_, ix, &element, nullptr);
if(res!=0)
{
return Error(std::string(__FUNCTION__) + " error("+ std::to_string(res)+"): " + avro_strerror());
if (res != 0) {
return Error(std::string(__FUNCTION__) + " error(" +
std::to_string(res) + "): " + avro_strerror());
}
_object_reader.read(static_cast<int>(ix), InputVarType{&element});
}
+64 -55
View File
@@ -5,18 +5,15 @@
#include <climits>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include "../Bytestring.hpp"
#include "../Timestamp.hpp"
#include "../Vectorstring.hpp"
#include "../always_false.hpp"
#include "../common.hpp"
#include "../concepts.hpp"
#include "../internal/is_literal.hpp"
#include "../internal/is_validator.hpp"
#include "../patterns.hpp"
namespace rfl::avro {
@@ -52,15 +49,15 @@ class RFL_API Writer {
~Writer();
OutputArrayType array_as_root(const size_t _size) const noexcept;
OutputArrayType array_as_root(const size_t _size) const;
OutputMapType map_as_root(const size_t _size) const noexcept;
OutputMapType map_as_root(const size_t _size) const;
OutputObjectType object_as_root(const size_t _size) const noexcept;
OutputObjectType object_as_root(const size_t _size) const;
OutputVarType null_as_root() const;
OutputUnionType union_as_root() const noexcept;
OutputUnionType union_as_root() const;
template <class T>
OutputVarType value_as_root(const T& _var) const {
@@ -138,7 +135,7 @@ class RFL_API Writer {
avro_value_t new_value;
int result = avro_value_append(&_parent->val_, &new_value, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
throw std::runtime_error("Error adding value to array: error(" +
std::to_string(result) +
"): " + avro_strerror());
}
@@ -153,7 +150,7 @@ class RFL_API Writer {
int result = avro_value_add(&_parent->val_, _name.data(), &new_value,
nullptr, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
throw std::runtime_error("Error adding value to map: error(" +
std::to_string(result) +
"): " + avro_strerror());
}
@@ -169,7 +166,7 @@ class RFL_API Writer {
int result = avro_value_get_by_name(&_parent->val_, _name.data(),
&new_value, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
throw std::runtime_error("Error adding value to object: error(" +
std::to_string(result) +
"): " + avro_strerror());
}
@@ -181,13 +178,13 @@ class RFL_API Writer {
OutputVarType add_value_to_union(const size_t _index, const T& _var,
OutputUnionType* _parent) const {
if (_index > static_cast<size_t>(INT_MAX)) {
throw std::runtime_error(std::string(__FUNCTION__) + " index error");
throw std::runtime_error("Error adding value to unions: Index error");
}
avro_value_t new_value;
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index),
&new_value);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
throw std::runtime_error("Error adding value to union: error(" +
std::to_string(result) +
"): " + avro_strerror());
}
@@ -204,67 +201,79 @@ class RFL_API Writer {
private:
template <class T>
void set_value(const T& _var, avro_value_t* _val) const {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_same_v<Type, std::string>) {
int result =
avro_value_set_string_len(_val, _var.c_str(), _var.size() + 1);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) +
"): " + avro_strerror());
throw std::runtime_error(
"Error setting string value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>() ||
std::is_same<std::remove_cvref_t<T>,
rfl::Vectorstring>()) {
} else if constexpr (concepts::MutableContiguousByteContainer<Type>) {
auto var = _var;
if (!var.data()) {
return;
}
int result = avro_value_set_bytes(_val, var.data(), var.size());
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) +
"): " + avro_strerror());
throw std::runtime_error(
"Error setting bytestring value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
} else if constexpr (std::is_same_v<Type, bool>) {
int result = avro_value_set_boolean(_val, _var);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) +
"): " + avro_strerror());
throw std::runtime_error(
"Error setting boolean value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
/*int result = avro_value_set_double(_val, static_cast<double>(_var));
} else if constexpr (std::is_same_v<Type, float>) {
int result = avro_value_set_float(_val, static_cast<float>(_var));
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+
std::to_string(result)+"): " + avro_strerror());
}*/
avro_value_set_double(_val, static_cast<double>(_var));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
/*int result = avro_value_set_long(_val, static_cast<std::int64_t>(_var));
throw std::runtime_error(
"Error setting float value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (std::is_floating_point_v<Type>) {
int result = avro_value_set_double(_val, static_cast<double>(_var));
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+
std::to_string(result)+"): " + avro_strerror());
}*/
avro_value_set_long(_val, static_cast<std::int64_t>(_var));
throw std::runtime_error(
"Error setting double value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (std::is_same_v<Type, std::int64_t> ||
std::is_same_v<Type, std::uint32_t> ||
std::is_same_v<Type, std::uint64_t>) {
int result = avro_value_set_long(_val, static_cast<std::int64_t>(_var));
if (result != 0) {
throw std::runtime_error(
"Error setting long value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (std::is_integral_v<Type>) {
int result = avro_value_set_int(_val, static_cast<std::int32_t>(_var));
if (result != 0) {
throw std::runtime_error(
"Error setting int value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (internal::is_literal_v<T>) {
int result = avro_value_set_enum(_val, static_cast<int>(_var.value()));
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) +
"): " + avro_strerror());
throw std::runtime_error(
"Error setting literal value: " + std::to_string(result) + ": " +
avro_strerror());
}
} else if constexpr (internal::is_validator_v<T>) {
using ValueType = std::remove_cvref_t<typename T::ReflectionType>;
const auto val = _var.value();
set_value<ValueType>(val, _val);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Timestamp<"%Y-%m-%d">>()) {
const auto str = _var.to_string();
set_value<std::string>(str, _val);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Email>()) {
const auto& str = static_cast<const std::string&>(_var);
set_value<std::string>(str, _val);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+1 -2
View File
@@ -6,7 +6,6 @@
#include <string>
#include "../../Literal.hpp"
//#include "../../Object.hpp"
#include "../../Ref.hpp"
#include "../../Rename.hpp"
#include "../../Variant.hpp"
@@ -36,7 +35,7 @@ struct RFL_API Type {
};
struct Double {
Literal<"float"> type{};
Literal<"double"> type{};
};
struct Bytes {
+1 -7
View File
@@ -3,18 +3,12 @@
#include <string>
//#include "../Literal.hpp"
#include "../Processors.hpp"
//#include "../Variant.hpp"
//#include "../json.hpp"
//#include "../parsing/schema/Type.hpp"
//#include "../parsing/schema/ValidationType.hpp"
#include "../common.hpp"
#include "../parsing/schema/make.hpp"
#include "Reader.hpp"
#include "Schema.hpp"
#include "Writer.hpp"
//#include "schema/Type.hpp"
#include "../common.hpp"
namespace rfl::avro {
+1 -2
View File
@@ -39,8 +39,7 @@ struct CustomParser {
}
template <class P>
static auto write(const W& _w, const OriginalClass& _p,
const P& _parent) noexcept {
static auto write(const W& _w, const OriginalClass& _p, const P& _parent) {
Parser<R, W, CustomParserHelperStruct, ProcessorsType>::write(
_w, HelperStruct::from_class(_p), _parent);
}
+1 -1
View File
@@ -57,7 +57,7 @@ struct FieldVariantParser {
template <class P>
static void write(const W& _w, const rfl::Variant<FieldTypes...>& _v,
const P& _parent) noexcept {
const P& _parent) {
static_assert(
internal::no_duplicate_field_names<rfl::Tuple<FieldTypes...>>(),
"Externally tagged variants cannot have duplicate field "
+3 -5
View File
@@ -44,7 +44,7 @@ struct MapParser {
}
template <class P>
static void write(const W& _w, const MapType& _m, const P& _parent) noexcept {
static void write(const W& _w, const MapType& _m, const P& _parent) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
write_map(_w, _m, _parent);
} else {
@@ -82,8 +82,7 @@ struct MapParser {
}
template <class P>
static void write_map(const W& _w, const MapType& _m,
const P& _parent) noexcept {
static void write_map(const W& _w, const MapType& _m, const P& _parent) {
auto m = ParentType::add_map(_w, _m.size(), _parent);
using ParentMapType =
@@ -122,8 +121,7 @@ struct MapParser {
}
template <class P>
static void write_object(const W& _w, const MapType& _m,
const P& _parent) noexcept {
static void write_object(const W& _w, const MapType& _m, const P& _parent) {
auto obj = ParentType::add_object(_w, _m.size(), _parent);
for (const auto& [k, v] : _m) {
if constexpr (internal::has_reflection_type_v<KeyType>) {
+3 -3
View File
@@ -145,7 +145,7 @@ struct NamedTupleParser {
template <class P>
static void write(const W& _w, const NamedTuple<FieldTypes...>& _tup,
const P& _parent) noexcept {
const P& _parent) {
if constexpr (_no_field_names) {
auto arr = ParentType::add_array(_w, _tup.num_fields(), _parent);
build_object(_w, _tup, &arr, std::make_integer_sequence<int, size_>());
@@ -169,7 +169,7 @@ struct NamedTupleParser {
template <int _i>
static void add_field_to_object(const W& _w,
const NamedTuple<FieldTypes...>& _tup,
OutputObjectOrArrayType* _ptr) noexcept {
OutputObjectOrArrayType* _ptr) {
using FieldType = internal::nth_element_t<_i, FieldTypes...>;
using ValueType = std::remove_cvref_t<typename FieldType::Type>;
const auto value = rfl::get<_i>(_tup);
@@ -222,7 +222,7 @@ struct NamedTupleParser {
template <int... _is>
static void build_object(const W& _w, const NamedTuple<FieldTypes...>& _tup,
OutputObjectOrArrayType* _ptr,
std::integer_sequence<int, _is...>) noexcept {
std::integer_sequence<int, _is...>) {
(add_field_to_object<_is>(_w, _tup, _ptr), ...);
}
+1 -1
View File
@@ -55,7 +55,7 @@ struct Parser<R, W, std::array<T, _size>, ProcessorsType> {
template <class P>
static void write(const W& _w, const std::array<T, _size>& _arr,
const P& _parent) noexcept {
const P& _parent) {
auto arr = ParentType::add_array(_w, _size, _parent);
const auto new_parent = typename ParentType::Array{&arr};
for (const auto& e : _arr) {
+7 -5
View File
@@ -13,19 +13,21 @@ namespace rfl {
namespace parsing {
template <class R, class W, class T, Copyability C, class ProcessorsType>
requires AreReaderAndWriter<R, W, Box<T, C>>
requires AreReaderAndWriter<R, W, Box<T, C>>
struct Parser<R, W, Box<T, C>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
static Result<Box<T, C>> read(const R& _r, const InputVarType& _var) noexcept {
const auto to_box = [](auto&& _t) { return Box<T, C>::make(std::move(_t)); };
static Result<Box<T, C>> read(const R& _r,
const InputVarType& _var) noexcept {
const auto to_box = [](auto&& _t) {
return Box<T, C>::make(std::move(_t));
};
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r, _var)
.transform(to_box);
}
template <class P>
static void write(const W& _w, const Box<T, C>& _box,
const P& _parent) noexcept {
static void write(const W& _w, const Box<T, C>& _box, const P& _parent) {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(_w, *_box,
_parent);
}
+1 -2
View File
@@ -23,8 +23,7 @@ struct Parser<R, W, Bytestring, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const Bytestring& _b,
const P& _parent) noexcept {
static void write(const W& _w, const Bytestring& _b, const P& _parent) {
ParentType::add_value(_w, _b, _parent);
}
+2 -3
View File
@@ -17,7 +17,7 @@ namespace rfl {
namespace parsing {
template <class R, class W, class T, size_t _size, class ProcessorsType>
requires AreReaderAndWriter<R, W, T[_size]>
requires AreReaderAndWriter<R, W, T[_size]>
struct Parser<R, W, T[_size], ProcessorsType> {
public:
using InputArrayType = typename R::InputArrayType;
@@ -33,8 +33,7 @@ struct Parser<R, W, T[_size], ProcessorsType> {
}
template <class P>
static void write(const W& _w, const CArray& _arr,
const P& _parent) noexcept {
static void write(const W& _w, const CArray& _arr, const P& _parent) {
auto arr = ParentType::add_array(_w, _size, _parent);
const auto new_parent = typename ParentType::Array{&arr};
for (const auto& e : _arr) {
+1 -1
View File
@@ -105,7 +105,7 @@ struct Parser {
}
template <class P>
static void write(const W& _w, const T& _var, const P& _parent) noexcept {
static void write(const W& _w, const T& _var, const P& _parent) {
if constexpr (internal::has_write_reflector<T>) {
Parser<R, W, typename Reflector<T>::ReflType, ProcessorsType>::write(
_w, Reflector<T>::from(_var), _parent);
+1 -2
View File
@@ -50,8 +50,7 @@ struct Parser<R, W, std::chrono::duration<Rep, Period>, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const DurationType& _d,
const P& _parent) noexcept {
static void write(const W& _w, const DurationType& _d, const P& _parent) {
const auto r =
RType{.count = static_cast<int64_t>(_d.count()), .unit = make_unit()};
return Parser<R, W, RType, ProcessorsType>::write(_w, r, _parent);
+1 -1
View File
@@ -33,7 +33,7 @@ struct Parser<R, W, std::filesystem::path, ProcessorsType> {
template <class P>
static void write(const W& _w, const std::filesystem::path& _p,
const P& _parent) noexcept {
const P& _parent) {
return Parser<R, W, std::string, ProcessorsType>::write(_w, _p.string(),
_parent);
}
+4 -3
View File
@@ -38,7 +38,9 @@ struct Parser<R, W, std::optional<T>, ProcessorsType> {
if (_r.is_empty(_var)) {
return std::optional<T>();
}
const auto to_opt = [](auto&& _t) { return std::make_optional<T>(std::forward<decltype(_t)>(_t)); };
const auto to_opt = [](auto&& _t) {
return std::make_optional<T>(std::forward<decltype(_t)>(_t));
};
return Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::read(_r,
_var)
.transform(to_opt);
@@ -46,8 +48,7 @@ struct Parser<R, W, std::optional<T>, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const std::optional<T>& _o,
const P& _parent) noexcept {
static void write(const W& _w, const std::optional<T>& _o, const P& _parent) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
auto u = ParentType::add_union(_w, _parent);
using UnionType = typename ParentType::template Union<decltype(u)>;
+2 -2
View File
@@ -15,7 +15,7 @@ namespace parsing {
template <class R, class W, class FirstType, class SecondType,
class ProcessorsType>
requires AreReaderAndWriter<R, W, std::pair<FirstType, SecondType>>
requires AreReaderAndWriter<R, W, std::pair<FirstType, SecondType>>
struct Parser<R, W, std::pair<FirstType, SecondType>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
@@ -33,7 +33,7 @@ struct Parser<R, W, std::pair<FirstType, SecondType>, ProcessorsType> {
template <class P>
static void write(const W& _w, const std::pair<FirstType, SecondType>& _p,
const P& _parent) noexcept {
const P& _parent) {
const auto tup = std::make_tuple(&_p.first, &_p.second);
Parser<R, W, std::tuple<const FirstType*, const SecondType*>,
ProcessorsType>::write(_w, tup, _parent);
+1 -1
View File
@@ -47,7 +47,7 @@ struct Parser<R, W, T*, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const T* _ptr, const P& _parent) noexcept {
static void write(const W& _w, const T* _ptr, const P& _parent) {
if (!_ptr) {
ParentType::add_null(_w, _parent);
return;
+2 -3
View File
@@ -14,7 +14,7 @@ namespace rfl {
namespace parsing {
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Ref<T>>
requires AreReaderAndWriter<R, W, Ref<T>>
struct Parser<R, W, Ref<T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
@@ -25,8 +25,7 @@ struct Parser<R, W, Ref<T>, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const Ref<T>& _ref,
const P& _parent) noexcept {
static void write(const W& _w, const Ref<T>& _ref, const P& _parent) {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(_w, *_ref,
_parent);
}
@@ -31,7 +31,7 @@ struct Parser<R, W, std::reference_wrapper<T>, ProcessorsType> {
template <class P>
static void write(const W& _w, const std::reference_wrapper<T> _ref,
const P& _p) noexcept {
const P& _p) {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(_w, _ref.get(),
_p);
}
+2 -2
View File
@@ -16,7 +16,7 @@ namespace parsing {
template <class R, class W, class T, internal::StringLiteral _name,
class ProcessorsType>
requires AreReaderAndWriter<R, W, Rename<_name, T>>
requires AreReaderAndWriter<R, W, Rename<_name, T>>
struct Parser<R, W, Rename<_name, T>, ProcessorsType> {
using InputVarType = typename R::InputVarType;
@@ -31,7 +31,7 @@ struct Parser<R, W, Rename<_name, T>, ProcessorsType> {
template <class P>
static void write(const W& _w, const Rename<_name, T>& _rename,
const P& _parent) noexcept {
const P& _parent) {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
_w, _rename.value(), _parent);
}
+1 -2
View File
@@ -45,8 +45,7 @@ struct Parser<R, W, Result<T>, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const Result<T>& _r,
const P& _parent) noexcept {
static void write(const W& _w, const Result<T>& _r, const P& _parent) {
if (_r) {
Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write(
_w, _r.value(), _parent);
+2 -2
View File
@@ -15,7 +15,7 @@ namespace rfl {
namespace parsing {
template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, internal::Array<T>>
requires AreReaderAndWriter<R, W, internal::Array<T>>
struct Parser<R, W, internal::Array<T>, ProcessorsType> {
public:
using InputArrayType = typename R::InputArrayType;
@@ -30,7 +30,7 @@ struct Parser<R, W, internal::Array<T>, ProcessorsType> {
template <class P>
static void write(const W& _w, const internal::Array<T>& _arr,
const P& _parent) noexcept {
const P& _parent) {
Parser<R, W, StdArray, ProcessorsType>::write(_w, _arr.arr_, _parent);
}
+1 -1
View File
@@ -92,7 +92,7 @@ class Parser<R, W, rfl::Variant<AlternativeTypes...>, ProcessorsType> {
template <class P>
static void write(const W& _w,
const rfl::Variant<AlternativeTypes...>& _variant,
const P& _parent) noexcept {
const P& _parent) {
if constexpr (internal::all_fields<std::tuple<AlternativeTypes...>>()) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
using WrappedType = rfl::Variant<
+1 -1
View File
@@ -44,7 +44,7 @@ struct Parser<R, W,
static void write(const W& _w,
const internal::Skip<T, _skip_serialization,
_skip_deserialization>& _skip,
const P& _parent) noexcept {
const P& _parent) {
if constexpr (_skip_serialization) {
ParentType::add_null(_w, _parent);
} else {
+1 -2
View File
@@ -55,8 +55,7 @@ struct Parser<R, W, std::span<T>, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const std::span<T>& _span,
const P& _parent) noexcept {
static void write(const W& _w, const std::span<T>& _span, const P& _parent) {
auto arr = ParentType::add_array(_w, _span.size(), _parent);
const auto new_parent = typename ParentType::Array{&arr};
for (const auto& v : _span) {
+1 -2
View File
@@ -46,8 +46,7 @@ struct Parser<R, W, std::string_view, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const std::string_view& _str,
const P& _p) noexcept {
static void write(const W& _w, const std::string_view& _str, const P& _p) {
Parser<R, W, std::string, ProcessorsType>::write(_w, std::string(_str), _p);
}
+1 -1
View File
@@ -73,7 +73,7 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>,
static void write(
const W& _w,
const TaggedUnion<_discriminator, AlternativeTypes...>& _tagged_union,
const P& _parent) noexcept {
const P& _parent) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
Parser<R, W, Variant<AlternativeTypes...>, ProcessorsType>::write(
_w, _tagged_union.variant(), _parent);
+1 -1
View File
@@ -46,7 +46,7 @@ struct Parser<R, W, std::unique_ptr<T>, ProcessorsType> {
template <class P>
static void write(const W& _w, const std::unique_ptr<T>& _s,
const P& _parent) noexcept {
const P& _parent) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
auto u = ParentType::add_union(_w, _parent);
using UnionType = typename ParentType::template Union<decltype(u)>;
+11 -11
View File
@@ -11,6 +11,7 @@
#include "../Result.hpp"
#include "../Variant.hpp"
#include "../always_false.hpp"
#include "../internal/all_fields.hpp"
#include "../internal/to_ptr_field.hpp"
#include "FieldVariantParser.hpp"
#include "Parent.hpp"
@@ -21,8 +22,6 @@
#include "schemaful/IsSchemafulWriter.hpp"
#include "schemaful/VariantReader.hpp"
#include "to_single_error_message.hpp"
#include "../internal/all_fields.hpp"
namespace rfl::parsing {
@@ -72,11 +71,11 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
return _r.template read_union<std::variant<AlternativeTypes...>, V>(_u);
});
} else if constexpr (ProcessorsType::add_tags_to_variants_ ||
} else if constexpr (ProcessorsType::add_tags_to_variants_ ||
ProcessorsType::add_namespaced_tags_to_variants_) {
constexpr bool remove_namespaces = ProcessorsType::add_tags_to_variants_;
using FieldVariantType =
rfl::Variant<VariantAlternativeWrapper<AlternativeTypes, remove_namespaces>...>;
using FieldVariantType = rfl::Variant<
VariantAlternativeWrapper<AlternativeTypes, remove_namespaces>...>;
const auto from_field_variant =
[](auto&& _field) -> std::variant<AlternativeTypes...> {
return std::move(_field.value());
@@ -109,7 +108,7 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
template <class P>
static void write(const W& _w,
const std::variant<AlternativeTypes...>& _variant,
const P& _parent) noexcept {
const P& _parent) {
if constexpr (internal::all_fields<std::tuple<AlternativeTypes...>>()) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
using WrappedType = rfl::Variant<
@@ -152,11 +151,12 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
},
_variant);
} else if constexpr (ProcessorsType::add_tags_to_variants_ ||
} else if constexpr (ProcessorsType::add_tags_to_variants_ ||
ProcessorsType::add_namespaced_tags_to_variants_) {
constexpr bool remove_namespaces = ProcessorsType::add_tags_to_variants_;
using FieldVariantType =
rfl::Variant<VariantAlternativeWrapper<const AlternativeTypes*, remove_namespaces>...>;
rfl::Variant<VariantAlternativeWrapper<const AlternativeTypes*,
remove_namespaces>...>;
const auto to_field_variant =
[]<class T>(const T& _t) -> FieldVariantType {
return VariantAlternativeWrapper<const T*, remove_namespaces>(&_t);
@@ -179,11 +179,11 @@ class Parser<R, W, std::variant<AlternativeTypes...>, ProcessorsType> {
return FieldVariantParser<R, W, ProcessorsType,
AlternativeTypes...>::to_schema(_definitions);
} else if constexpr (ProcessorsType::add_tags_to_variants_ ||
} else if constexpr (ProcessorsType::add_tags_to_variants_ ||
ProcessorsType::add_namespaced_tags_to_variants_) {
constexpr bool remove_namespaces = ProcessorsType::add_tags_to_variants_;
using FieldVariantType =
rfl::Variant<VariantAlternativeWrapper<AlternativeTypes, remove_namespaces>...>;
using FieldVariantType = rfl::Variant<
VariantAlternativeWrapper<AlternativeTypes, remove_namespaces>...>;
return Parser<R, W, FieldVariantType, ProcessorsType>::to_schema(
_definitions);
+1 -2
View File
@@ -23,8 +23,7 @@ struct Parser<R, W, Vectorstring, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const Vectorstring& _b,
const P& _parent) noexcept {
static void write(const W& _w, const Vectorstring& _b, const P& _parent) {
ParentType::add_value(_w, _b, _parent);
}
+1 -2
View File
@@ -52,8 +52,7 @@ struct Parser<R, W, std::wstring, ProcessorsType> {
}
template <class P>
static void write(const W& _w, const std::wstring& _str,
const P& _parent) noexcept {
static void write(const W& _w, const std::wstring& _str, const P& _parent) {
if (_str.empty()) {
ParentType::add_value(_w, std::string(), _parent);
return;
+3 -4
View File
@@ -73,8 +73,7 @@ struct TupleParser {
}
template <class P>
static void write(const W& _w, const TupleType& _tup,
const P& _parent) noexcept {
static void write(const W& _w, const TupleType& _tup, const P& _parent) {
if constexpr (schemaful::IsSchemafulWriter<W>) {
const auto named_tuple = schemaful::tuple_to_named_tuple(_tup);
Parser<R, W, std::remove_cvref_t<decltype(named_tuple)>,
@@ -115,7 +114,7 @@ struct TupleParser {
template <int _i, class P>
static void add_to_array(const W& _w, const TupleType& _tup,
const P& _parent) noexcept {
const P& _parent) {
using NewFieldType =
std::remove_cvref_t<rfl::tuple_element_t<_i, TupleType>>;
Parser<R, W, NewFieldType, ProcessorsType>::write(_w, rfl::get<_i>(_tup),
@@ -124,7 +123,7 @@ struct TupleParser {
template <int... _is, class P>
static void to_array(const W& _w, const TupleType& _tup, const P& _parent,
std::integer_sequence<int, _is...>) noexcept {
std::integer_sequence<int, _is...>) {
(add_to_array<_is>(_w, _tup, _parent), ...);
}
};
+1 -2
View File
@@ -72,8 +72,7 @@ struct VectorParser {
}
template <class P>
static void write(const W& _w, const VecType& _vec,
const P& _parent) noexcept {
static void write(const W& _w, const VecType& _vec, const P& _parent) {
if constexpr (treat_as_map()) {
MapParser<R, W, VecType, ProcessorsType>::write(_w, _vec, _parent);
} else {
+101 -62
View File
@@ -1,4 +1,5 @@
#include "rfl/avro/Writer.hpp"
#include <string_view>
#include "rfl/parsing/schemaful/IsSchemafulWriter.hpp"
@@ -12,29 +13,28 @@ Writer::Writer(avro_value_t* _root) : root_(_root){};
Writer::~Writer() = default;
Writer::OutputArrayType Writer::array_as_root(
const size_t /*_size*/) const noexcept {
Writer::OutputArrayType Writer::array_as_root(const size_t /*_size*/) const {
return OutputArrayType{*root_};
}
Writer::OutputMapType Writer::map_as_root(const size_t /*_size*/) const noexcept {
Writer::OutputMapType Writer::map_as_root(const size_t /*_size*/) const {
return OutputMapType{*root_};
}
Writer::OutputObjectType Writer::object_as_root(
const size_t /*_size*/) const noexcept {
Writer::OutputObjectType Writer::object_as_root(const size_t /*_size*/) const {
return OutputObjectType{*root_};
}
Writer::OutputVarType Writer::null_as_root() const {
int result = avro_value_set_null(root_);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputVarType{*root_};
}
Writer::OutputUnionType Writer::union_as_root() const noexcept {
Writer::OutputUnionType Writer::union_as_root() const {
return OutputUnionType{*root_};
}
@@ -43,18 +43,21 @@ Writer::OutputArrayType Writer::add_array_to_array(
avro_value_t new_array;
int result = avro_value_append(&_parent->val_, &new_array, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputArrayType{new_array};
}
Writer::OutputArrayType Writer::add_array_to_map(
const std::string_view& _name, const size_t /*_size*/,
OutputMapType* _parent) const {
Writer::OutputArrayType Writer::add_array_to_map(const std::string_view& _name,
const size_t /*_size*/,
OutputMapType* _parent) const {
avro_value_t new_array;
int result = avro_value_add(&_parent->val_, _name.data(), &new_array, nullptr, nullptr);
int result = avro_value_add(&_parent->val_, _name.data(), &new_array, nullptr,
nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputArrayType{new_array};
}
@@ -63,9 +66,11 @@ Writer::OutputArrayType Writer::add_array_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
avro_value_t new_array;
int result = avro_value_get_by_name(&_parent->val_, _name.data(), &new_array, nullptr);
int result =
avro_value_get_by_name(&_parent->val_, _name.data(), &new_array, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputArrayType{new_array};
}
@@ -77,30 +82,35 @@ Writer::OutputArrayType Writer::add_array_to_union(
throw std::runtime_error(std::string(__FUNCTION__) + " index error");
}
avro_value_t new_array;
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_array);
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index),
&new_array);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputArrayType{new_array};
}
Writer::OutputMapType Writer::add_map_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const {
Writer::OutputMapType Writer::add_map_to_array(const size_t /*_size*/,
OutputArrayType* _parent) const {
avro_value_t new_map;
int result = avro_value_append(&_parent->val_, &new_map, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputMapType{new_map};
}
Writer::OutputMapType Writer::add_map_to_map(
const std::string_view& _name, const size_t /*_size*/,
OutputMapType* _parent) const {
Writer::OutputMapType Writer::add_map_to_map(const std::string_view& _name,
const size_t /*_size*/,
OutputMapType* _parent) const {
avro_value_t new_map;
int result = avro_value_add(&_parent->val_, _name.data(), &new_map, nullptr, nullptr);
int result =
avro_value_add(&_parent->val_, _name.data(), &new_map, nullptr, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputMapType{new_map};
}
@@ -109,23 +119,27 @@ Writer::OutputMapType Writer::add_map_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
avro_value_t new_map;
int result = avro_value_get_by_name(&_parent->val_, _name.data(), &new_map, nullptr);
int result =
avro_value_get_by_name(&_parent->val_, _name.data(), &new_map, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputMapType{new_map};
}
Writer::OutputMapType Writer::add_map_to_union(
const size_t _index, const size_t /*_size*/,
OutputUnionType* _parent) const {
Writer::OutputMapType Writer::add_map_to_union(const size_t _index,
const size_t /*_size*/,
OutputUnionType* _parent) const {
if (_index > static_cast<size_t>(INT_MAX)) {
throw std::runtime_error(std::string(__FUNCTION__) + " index error");
}
avro_value_t new_map;
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_map);
int result =
avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_map);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputMapType{new_map};
}
@@ -134,8 +148,9 @@ Writer::OutputObjectType Writer::add_object_to_array(
const size_t /*_size*/, OutputArrayType* _parent) const {
avro_value_t new_object;
int result = avro_value_append(&_parent->val_, &new_object, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputObjectType{new_object};
}
@@ -144,9 +159,11 @@ Writer::OutputObjectType Writer::add_object_to_map(
const std::string_view& _name, const size_t /*_size*/,
OutputMapType* _parent) const {
avro_value_t new_object;
int result = avro_value_add(&_parent->val_, _name.data(), &new_object, nullptr, nullptr);
int result = avro_value_add(&_parent->val_, _name.data(), &new_object,
nullptr, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputObjectType{new_object};
}
@@ -155,9 +172,11 @@ Writer::OutputObjectType Writer::add_object_to_object(
const std::string_view& _name, const size_t /*_size*/,
OutputObjectType* _parent) const {
avro_value_t new_object;
int result = avro_value_get_by_name(&_parent->val_, _name.data(), &new_object, nullptr);
int result = avro_value_get_by_name(&_parent->val_, _name.data(), &new_object,
nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputObjectType{new_object};
}
@@ -169,9 +188,11 @@ Writer::OutputObjectType Writer::add_object_to_union(
throw std::runtime_error(std::string(__FUNCTION__) + " index error");
}
avro_value_t new_object;
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_object);
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index),
&new_object);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputObjectType{new_object};
}
@@ -181,17 +202,20 @@ Writer::OutputUnionType Writer::add_union_to_array(
avro_value_t new_union;
int result = avro_value_append(&_parent->val_, &new_union, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputUnionType{new_union};
}
Writer::OutputUnionType Writer::add_union_to_map(
const std::string_view& _name, OutputMapType* _parent) const {
Writer::OutputUnionType Writer::add_union_to_map(const std::string_view& _name,
OutputMapType* _parent) const {
avro_value_t new_union;
int result = avro_value_add(&_parent->val_, _name.data(), &new_union, nullptr, nullptr);
int result = avro_value_add(&_parent->val_, _name.data(), &new_union, nullptr,
nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputUnionType{new_union};
}
@@ -199,9 +223,11 @@ Writer::OutputUnionType Writer::add_union_to_map(
Writer::OutputUnionType Writer::add_union_to_object(
const std::string_view& _name, OutputObjectType* _parent) const {
avro_value_t new_union;
int result = avro_value_get_by_name(&_parent->val_, _name.data(), &new_union, nullptr);
int result =
avro_value_get_by_name(&_parent->val_, _name.data(), &new_union, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputUnionType{new_union};
}
@@ -212,9 +238,11 @@ Writer::OutputUnionType Writer::add_union_to_union(
throw std::runtime_error(std::string(__FUNCTION__) + " index error");
}
avro_value_t new_union;
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_union);
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index),
&new_union);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputUnionType{new_union};
}
@@ -224,25 +252,30 @@ Writer::OutputVarType Writer::add_null_to_array(
avro_value_t new_null;
int result = avro_value_append(&_parent->val_, &new_null, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
result = avro_value_set_null(&new_null);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputVarType{new_null};
}
Writer::OutputVarType Writer::add_null_to_map(
const std::string_view& _name, OutputMapType* _parent) const {
Writer::OutputVarType Writer::add_null_to_map(const std::string_view& _name,
OutputMapType* _parent) const {
avro_value_t new_null;
int result = avro_value_add(&_parent->val_, _name.data(), &new_null, nullptr, nullptr);
int result =
avro_value_add(&_parent->val_, _name.data(), &new_null, nullptr, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
result = avro_value_set_null(&new_null);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputVarType{new_null};
}
@@ -250,13 +283,16 @@ Writer::OutputVarType Writer::add_null_to_map(
Writer::OutputVarType Writer::add_null_to_object(
const std::string_view& _name, OutputObjectType* _parent) const {
avro_value_t new_null;
int result = avro_value_get_by_name(&_parent->val_, _name.data(), &new_null, nullptr);
int result =
avro_value_get_by_name(&_parent->val_, _name.data(), &new_null, nullptr);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
result = avro_value_set_null(&new_null);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputVarType{new_null};
}
@@ -267,13 +303,16 @@ Writer::OutputVarType Writer::add_null_to_union(
throw std::runtime_error(std::string(__FUNCTION__) + " index error");
}
avro_value_t new_null;
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_null);
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index),
&new_null);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
result = avro_value_set_null(&new_null);
if (result != 0) {
throw std::runtime_error(std::string(__FUNCTION__) + " error("+ std::to_string(result)+"): " + avro_strerror());
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
std::to_string(result) + "): " + avro_strerror());
}
return OutputVarType{new_null};
}
-3
View File
@@ -55,9 +55,6 @@ schema::Type type_to_avro_schema_type(
return schema::Type{.value = schema::Type::Bytes{}};
} 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::Int{}};
+1 -1
View File
@@ -40,6 +40,6 @@ TEST(avro, test_add_struct_name) {
.email = "homer@simpson.com",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read<rfl::AddStructName<"type">>(homer);
write_and_read_with_json<rfl::AddStructName<"type">>(homer);
}
} // namespace test_add_struct_name
+1 -1
View File
@@ -29,6 +29,6 @@ TEST(avro, test_array) {
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
std::move(bart), std::move(lisa), std::move(maggie)})};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_array
+1 -2
View File
@@ -35,7 +35,6 @@ TEST(avro, test_box) {
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
write_and_read_with_json(tree);
}
} // namespace test_box
+1 -1
View File
@@ -43,6 +43,6 @@ TEST(avro, test_combined_processors) {
using Processors =
rfl::Processors<rfl::SnakeCaseToCamelCase, rfl::AddStructName<"type">>;
write_and_read<Processors>(homer);
write_and_read_with_json<Processors>(homer);
}
} // namespace test_combined_processors
+1 -1
View File
@@ -30,6 +30,6 @@ struct Person {
TEST(avro, test_custom_class1) {
const auto bart = Person("Bart");
write_and_read(bart);
write_and_read_with_json(bart);
}
} // namespace test_custom_class1
+1 -1
View File
@@ -56,7 +56,7 @@ namespace test_custom_class3 {
TEST(avro, test_custom_class3) {
const auto bart = Person("Bart", "Simpson", 10);
write_and_read(bart);
write_and_read_with_json(bart);
}
} // namespace test_custom_class3
+1 -1
View File
@@ -58,6 +58,6 @@ TEST(avro, test_custom_class4) {
const auto bart = test_custom_class4::Person(
"Bart", rfl::make_box<std::string>("Simpson"), 10);
write_and_read(bart);
write_and_read_with_json(bart);
}
} // namespace test_custom_class4
+1 -1
View File
@@ -21,6 +21,6 @@ TEST(avro, test_default_values) {
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_default_values
+1 -2
View File
@@ -20,7 +20,6 @@ TEST(avro, test_default_values) {
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_deque
+1 -1
View File
@@ -16,7 +16,7 @@ struct Circle {
TEST(avro, test_enum) {
const auto circle = Circle{.radius = 2.0, .color = Color::green};
write_and_read(circle);
write_and_read_with_json(circle);
}
} // namespace test_enum
+1 -1
View File
@@ -26,6 +26,6 @@ TEST(avro, test_field_variant_std) {
const Shapes r =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
write_and_read(r);
write_and_read_with_json(r);
}
} // namespace test_field_variant
+1 -1
View File
@@ -26,6 +26,6 @@ TEST(avro, test_field_variant_std) {
const Shapes r =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
write_and_read(r);
write_and_read_with_json(r);
}
} // namespace test_field_variant_std
+1 -1
View File
@@ -26,7 +26,7 @@ TEST(avro, test_flag_enum) {
const auto circle =
Circle{.radius = 2.0, .color = Color::blue | Color::orange};
write_and_read(circle);
write_and_read_with_json(circle);
}
} // namespace test_flag_enum
+1 -1
View File
@@ -25,7 +25,7 @@ struct Circle {
TEST(avro, test_flag_enum_with_int) {
const auto circle = Circle{.radius = 2.0, .color = static_cast<Color>(10000)};
write_and_read(circle);
write_and_read_with_json(circle);
}
} // namespace test_flag_enum_with_int
+1 -1
View File
@@ -26,6 +26,6 @@ TEST(avro, test_flatten) {
.employer = rfl::make_box<std::string>("Mr. Burns"),
.salary = 60000.0};
write_and_read(employee);
write_and_read_with_json(employee);
}
} // namespace test_flatten
+1 -1
View File
@@ -26,7 +26,7 @@ TEST(avro, test_flatten_anonymous) {
.employer = rfl::make_box<std::string>("Mr. Burns"),
.salary = 60000.0};
write_and_read(employee);
write_and_read_with_json(employee);
}
} // namespace test_flatten_anonymous
+38
View File
@@ -0,0 +1,38 @@
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_float_and_int {
struct float_struct {
float i;
};
struct double_struct {
double i;
};
struct int_struct {
int i;
};
struct long_struct {
long i;
};
TEST(avro, test_float_and_int) {
float_struct test_float_struct{.i = 0.1};
double_struct test_double_struct{.i = 0.1};
int_struct test_int_struct{.i = 1};
long_struct test_long_struct{.i = 1};
write_and_read_with_json(test_float_struct);
write_and_read_with_json(test_double_struct);
write_and_read_with_json(test_int_struct);
write_and_read_with_json(test_long_struct);
}
} // namespace test_float_and_int
+1 -1
View File
@@ -22,6 +22,6 @@ TEST(avro, test_map2) {
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_map2
+1 -1
View File
@@ -53,6 +53,6 @@ TEST(avro, test_monster_example) {
.weapons = weapons,
.equipped = rfl::make_field<"weapon">(axe)};
write_and_read(orc);
write_and_read_with_json(orc);
}
} // namespace test_monster_example
+1 -1
View File
@@ -24,6 +24,6 @@ TEST(avro, test_optional_fields) {
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_optional_fields
+1 -1
View File
@@ -40,6 +40,6 @@ TEST(avro, test_readme_example) {
.email = "homer@simpson.com",
.child = std::vector<Person>({bart, lisa, maggie})};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_readme_example
+1 -1
View File
@@ -15,6 +15,6 @@ TEST(avro, test_readme_example2) {
const auto homer =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_readme_example2
+1 -1
View File
@@ -25,6 +25,6 @@ TEST(avro, test_readme_example3) {
.birthday = "1987-04-19",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_readme_example3
+2 -3
View File
@@ -23,7 +23,7 @@ struct DecisionTree {
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
TEST(avro, test_ref) {
TEST(avro, test_ref) {
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
@@ -35,7 +35,6 @@ TEST(avro, test_ref) {
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
write_and_read_with_json(tree);
}
} // namespace test_ref
+1 -1
View File
@@ -23,6 +23,6 @@ using Shapes = rfl::Variant<Circle, Rectangle, std::unique_ptr<Square>>;
TEST(avro, test_rfl_variant) {
const Shapes r = Rectangle{.height = 10, .width = 5};
write_and_read(r);
write_and_read_with_json(r);
}
} // namespace test_variant
+2 -2
View File
@@ -11,13 +11,13 @@ struct Person {
std::unique_ptr<std::set<std::string>> children;
};
TEST(avro, test_set) {
TEST(avro, test_set) {
auto children = std::make_unique<std::set<std::string>>(
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_set
+1 -1
View File
@@ -23,6 +23,6 @@ TEST(avro, test_shared_ptr) {
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_shared_ptr
+1 -1
View File
@@ -28,6 +28,6 @@ TEST(avro, test_snake_case_to_camel_case) {
.birthday = "1987-04-19",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read<rfl::SnakeCaseToCamelCase>(homer);
write_and_read_with_json<rfl::SnakeCaseToCamelCase>(homer);
}
} // namespace test_snake_case_to_camel_case
@@ -28,6 +28,6 @@ TEST(avro, test_snake_case_to_pascal_case) {
.birthday = "1987-04-19",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read<rfl::SnakeCaseToPascalCase>(homer);
write_and_read_with_json<rfl::SnakeCaseToPascalCase>(homer);
}
} // namespace test_snake_case_to_pascal_case
+1 -1
View File
@@ -14,6 +14,6 @@ TEST(avro, test_string_map) {
homer.insert(
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_string_map
+1 -1
View File
@@ -22,6 +22,6 @@ using Shapes = rfl::TaggedUnion<"shape", Rectangle, Circle, Square>;
TEST(avro, test_tagged_union) {
const Shapes r = Rectangle{.height = 10, .width = 5};
write_and_read(r);
write_and_read_with_json(r);
}
} // namespace test_tagged_union
+2 -2
View File
@@ -14,7 +14,7 @@ struct Person {
TS birthday;
};
TEST(avro, test_timestamp) {
TEST(avro, test_timestamp) {
const auto result = TS::from_string("nonsense");
if (result) {
@@ -24,6 +24,6 @@ TEST(avro, test_timestamp) {
const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"};
write_and_read(bart);
write_and_read_with_json(bart);
}
} // namespace test_timestamp
+1 -1
View File
@@ -28,6 +28,6 @@ TEST(avro, test_tuple) {
std::tuple<Person, Person, Person>{
std::move(bart), std::move(lisa), std::move(maggie)})};
write_and_read(homer);
write_and_read_with_json(homer);
}
} // namespace test_tuple
+3 -16
View File
@@ -4,21 +4,9 @@
#include <rfl/avro.hpp>
#include <string>
//#include "write_and_read.hpp"
/// The basic example from the Avro C tutorial.
namespace test_tutorial_example {
const char PERSON_SCHEMA[] =
"{\"type\":\"record\",\
\"name\":\"Person\",\
\"fields\":[\
{\"name\": \"ID\", \"type\": \"long\"},\
{\"name\": \"First\", \"type\": \"string\"},\
{\"name\": \"Last\", \"type\": \"string\"},\
{\"name\": \"Phone\", \"type\": \"string\"},\
{\"name\": \"Age\", \"type\": \"int\"}]}";
struct Person {
size_t ID;
std::string First;
@@ -33,12 +21,11 @@ TEST(avro, test_tutorial_example) {
.Last = "Graves",
.Phone = "(555) 123-5678",
.Age = 30};
const auto schema = rfl::avro::Schema<Person>::from_json(PERSON_SCHEMA);
const auto serialized1 = rfl::avro::write(person, schema.value());
const auto res = rfl::avro::read<Person>(serialized1, schema.value());
const auto serialized1 = rfl::avro::write(person);
const auto res = rfl::avro::read<Person>(serialized1);
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
const auto serialized2 = rfl::avro::write(res.value(), schema.value());
const auto serialized2 = rfl::avro::write(res.value());
EXPECT_EQ(serialized1, serialized2);
}
} // namespace test_tutorial_example
+15
View File
@@ -4,6 +4,8 @@
#include <gtest/gtest.h>
#include <rfl/avro.hpp>
#include <rfl/json.hpp>
#include <string>
template <class... Ps>
void write_and_read(const auto& _struct) {
@@ -15,4 +17,17 @@ void write_and_read(const auto& _struct) {
const auto serialized2 = rfl::avro::write<Ps...>(res.value());
EXPECT_EQ(serialized1, serialized2);
}
template <class... Ps>
void write_and_read_with_json(const auto& _struct) {
using T = std::remove_cvref_t<decltype(_struct)>;
const auto serialized1 = rfl::avro::write<Ps...>(_struct);
const auto res = rfl::avro::read<T, Ps...>(serialized1);
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().what();
const auto serialized2 = rfl::avro::write<Ps...>(res.value());
EXPECT_EQ(serialized1, serialized2);
EXPECT_EQ(rfl::json::write<Ps...>(_struct),
rfl::json::write<Ps...>(res.value()));
}
#endif