#ifndef RFL_AVRO_READ_HPP_ #define RFL_AVRO_READ_HPP_ #include #include #include #include #include "../Processors.hpp" #include "../concepts.hpp" #include "../internal/ptr_cast.hpp" #include "../internal/wrap_in_rfl_array_t.hpp" #include "Parser.hpp" #include "Reader.hpp" #include "Schema.hpp" #include "to_schema.hpp" namespace rfl::avro { using InputObjectType = typename Reader::InputObjectType; using InputVarType = typename Reader::InputVarType; /// Parses an object from a AVRO var. template auto read(const InputVarType& _obj) { const auto r = Reader(); return Parser>::read(r, _obj); } /// Parses an object from AVRO using reflection. template Result> read( const concepts::ByteLike auto* _bytes, const size_t _size, const Schema& _schema) noexcept { avro_reader_t avro_reader = avro_reader_memory(internal::ptr_cast(_bytes), _size); avro_value_t root; int res = avro_generic_value_new(_schema.iface(), &root); if (res != 0) { avro_value_decref(&root); avro_reader_free(avro_reader); return error(std::string("Could not read root value: ") + avro_strerror()); } res = avro_value_read(avro_reader, &root); if (res) { avro_value_decref(&root); avro_reader_free(avro_reader); return error(std::string("Could not read root value: ") + avro_strerror()); } auto result = read(InputVarType{&root}); avro_value_decref(&root); avro_reader_free(avro_reader); return result; } /// Parses an object from AVRO using reflection. template auto read(const concepts::ByteLike auto* _bytes, const size_t _size) { const auto schema = to_schema, Ps...>(); return read(_bytes, _size, schema); } /// Parses an object from AVRO using reflection. template auto read(const concepts::ContiguousByteContainer auto& _bytes, const Schema& _schema) noexcept { return read(_bytes.data(), _bytes.size(), _schema); } /// Parses an object from AVRO using reflection. template auto read(const concepts::ContiguousByteContainer auto& _bytes) { return read(_bytes.data(), _bytes.size()); } /// Parses an object from a stream. template auto read(std::istream& _stream) { std::istreambuf_iterator begin(_stream), end; auto bytes = std::vector(begin, end); return read(bytes.data(), bytes.size()); } } // namespace rfl::avro #endif