Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/rfl/avro/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand All @@ -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");
}
Expand All @@ -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.");
}

Expand Down
55 changes: 24 additions & 31 deletions include/rfl/avro/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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});
}
Expand All @@ -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});
}
Expand Down
119 changes: 64 additions & 55 deletions include/rfl/avro/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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.");
}
Expand Down
3 changes: 1 addition & 2 deletions include/rfl/avro/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <string>

#include "../../Literal.hpp"
//#include "../../Object.hpp"
#include "../../Ref.hpp"
#include "../../Rename.hpp"
#include "../../Variant.hpp"
Expand Down Expand Up @@ -36,7 +35,7 @@ struct RFL_API Type {
};

struct Double {
Literal<"float"> type{};
Literal<"double"> type{};
};

struct Bytes {
Expand Down
8 changes: 1 addition & 7 deletions include/rfl/avro/to_schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Loading
Loading