mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 18:56:19 +08:00
Added support for Apache Avro, and thus for schemaful formats (#303)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
|
||||
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/avro/Reader.cpp"
|
||||
#include "rfl/avro/SchemaImpl.cpp"
|
||||
#include "rfl/avro/Type.cpp"
|
||||
#include "rfl/avro/Writer.cpp"
|
||||
#include "rfl/avro/to_schema.cpp"
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "rfl/avro/Reader.hpp"
|
||||
|
||||
#include "rfl/parsing/schemaful/IsSchemafulReader.hpp"
|
||||
|
||||
namespace rfl::avro {
|
||||
|
||||
static_assert(parsing::schemaful::IsSchemafulReader<Reader>,
|
||||
"This must be a schemaful reader.");
|
||||
|
||||
bool Reader::is_empty(const InputVarType& _var) const noexcept {
|
||||
return avro_value_get_type(_var.val_) == AVRO_NULL;
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputArrayType> Reader::to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (avro_value_get_type(_var.val_) != AVRO_ARRAY) {
|
||||
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 type = avro_value_get_type(_var.val_);
|
||||
if (type != AVRO_RECORD) {
|
||||
return Error("Could not cast to an object.");
|
||||
}
|
||||
return InputObjectType{_var.val_};
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputMapType> Reader::to_map(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto type = avro_value_get_type(_var.val_);
|
||||
if (type != AVRO_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
return InputMapType{_var.val_};
|
||||
}
|
||||
|
||||
rfl::Result<Reader::InputUnionType> Reader::to_union(
|
||||
const InputVarType& _var) const noexcept {
|
||||
const auto type = avro_value_get_type(_var.val_);
|
||||
if (type != AVRO_UNION) {
|
||||
return Error("Could not cast to a union.");
|
||||
}
|
||||
return InputUnionType{_var.val_};
|
||||
}
|
||||
|
||||
} // namespace rfl::avro
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "rfl/avro/SchemaImpl.hpp"
|
||||
|
||||
namespace rfl::avro {
|
||||
|
||||
SchemaImpl::SchemaImpl(const std::string& _json_str)
|
||||
: json_str_(_json_str),
|
||||
schema_(Box<avro_schema_t>::make()),
|
||||
iface_(nullptr) {
|
||||
const auto err = avro_schema_from_json_length(
|
||||
_json_str.c_str(), _json_str.size(), schema_.get());
|
||||
if (err) {
|
||||
throw std::runtime_error(
|
||||
"Cannot create schema from the following JSON: \"" + _json_str +
|
||||
"\": " + avro_strerror());
|
||||
}
|
||||
iface_ = avro_generic_class_from_schema(*schema_);
|
||||
}
|
||||
|
||||
SchemaImpl::~SchemaImpl() {
|
||||
if (iface_) {
|
||||
avro_value_iface_decref(iface_);
|
||||
iface_ = nullptr;
|
||||
avro_schema_decref(*schema_);
|
||||
}
|
||||
}
|
||||
|
||||
SchemaImpl::SchemaImpl(SchemaImpl&& _other) noexcept
|
||||
: json_str_(std::move(_other.json_str_)),
|
||||
schema_(std::move(_other.schema_)),
|
||||
iface_(_other.iface_) {
|
||||
_other.iface_ = nullptr;
|
||||
}
|
||||
|
||||
SchemaImpl& SchemaImpl::operator=(SchemaImpl&& _other) noexcept {
|
||||
if (this == &_other) {
|
||||
return *this;
|
||||
}
|
||||
json_str_ = std::move(_other.json_str_);
|
||||
schema_ = std::move(_other.schema_);
|
||||
iface_ = _other.iface_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace rfl::avro
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
|
||||
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/avro/schema/Type.hpp"
|
||||
|
||||
namespace rfl::avro::schema {
|
||||
|
||||
Type Type::with_name(const std::string& _name) const {
|
||||
const auto set_name = [&](const auto& _v) -> ReflectionType {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
if constexpr (std::is_same<T, Record>() || std::is_same<T, Enum>()) {
|
||||
auto v_with_name = _v;
|
||||
v_with_name.name = _name;
|
||||
return v_with_name;
|
||||
} else {
|
||||
return _v;
|
||||
}
|
||||
};
|
||||
return Type{.value = value.visit(set_name)};
|
||||
}
|
||||
|
||||
} // namespace rfl::avro::schema
|
||||
@@ -0,0 +1,190 @@
|
||||
#include "rfl/avro/Writer.hpp"
|
||||
|
||||
#include "rfl/parsing/schemaful/IsSchemafulWriter.hpp"
|
||||
|
||||
namespace rfl::avro {
|
||||
|
||||
static_assert(parsing::schemaful::IsSchemafulWriter<Writer>,
|
||||
"This must be a schemaful writer.");
|
||||
|
||||
Writer::Writer(avro_value_t* _root) : root_(_root){};
|
||||
|
||||
Writer::~Writer() = default;
|
||||
|
||||
Writer::OutputArrayType Writer::array_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return OutputArrayType{*root_};
|
||||
}
|
||||
|
||||
Writer::OutputMapType Writer::map_as_root(const size_t _size) const noexcept {
|
||||
return OutputMapType{*root_};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::object_as_root(
|
||||
const size_t _size) const noexcept {
|
||||
return OutputObjectType{*root_};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::null_as_root() const noexcept {
|
||||
avro_value_set_null(root_);
|
||||
return OutputVarType{*root_};
|
||||
}
|
||||
|
||||
Writer::OutputUnionType Writer::union_as_root() const noexcept {
|
||||
return OutputUnionType{*root_};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
avro_value_t new_array;
|
||||
avro_value_append(&_parent->val_, &new_array, nullptr);
|
||||
return OutputArrayType{new_array};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_map(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputMapType* _parent) const noexcept {
|
||||
avro_value_t new_array;
|
||||
avro_value_add(&_parent->val_, _name.data(), &new_array, nullptr, nullptr);
|
||||
return OutputArrayType{new_array};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
avro_value_t new_array;
|
||||
avro_value_get_by_name(&_parent->val_, _name.data(), &new_array, nullptr);
|
||||
return OutputArrayType{new_array};
|
||||
}
|
||||
|
||||
Writer::OutputArrayType Writer::add_array_to_union(
|
||||
const size_t _index, const size_t _size,
|
||||
OutputUnionType* _parent) const noexcept {
|
||||
avro_value_t new_array;
|
||||
avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_array);
|
||||
return OutputArrayType{new_array};
|
||||
}
|
||||
|
||||
Writer::OutputMapType Writer::add_map_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
avro_value_t new_map;
|
||||
avro_value_append(&_parent->val_, &new_map, nullptr);
|
||||
return OutputMapType{new_map};
|
||||
}
|
||||
|
||||
Writer::OutputMapType Writer::add_map_to_map(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputMapType* _parent) const noexcept {
|
||||
avro_value_t new_map;
|
||||
avro_value_add(&_parent->val_, _name.data(), &new_map, nullptr, nullptr);
|
||||
return OutputMapType{new_map};
|
||||
}
|
||||
|
||||
Writer::OutputMapType Writer::add_map_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
avro_value_t new_map;
|
||||
avro_value_get_by_name(&_parent->val_, _name.data(), &new_map, nullptr);
|
||||
return OutputMapType{new_map};
|
||||
}
|
||||
|
||||
Writer::OutputMapType Writer::add_map_to_union(
|
||||
const size_t _index, const size_t _size,
|
||||
OutputUnionType* _parent) const noexcept {
|
||||
avro_value_t new_map;
|
||||
avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_map);
|
||||
return OutputMapType{new_map};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
avro_value_t new_object;
|
||||
avro_value_append(&_parent->val_, &new_object, nullptr);
|
||||
return OutputObjectType{new_object};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_map(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputMapType* _parent) const noexcept {
|
||||
avro_value_t new_object;
|
||||
avro_value_add(&_parent->val_, _name.data(), &new_object, nullptr, nullptr);
|
||||
return OutputObjectType{new_object};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
avro_value_t new_object;
|
||||
avro_value_get_by_name(&_parent->val_, _name.data(), &new_object, nullptr);
|
||||
return OutputObjectType{new_object};
|
||||
}
|
||||
|
||||
Writer::OutputObjectType Writer::add_object_to_union(
|
||||
const size_t _index, const size_t _size,
|
||||
OutputUnionType* _parent) const noexcept {
|
||||
avro_value_t new_object;
|
||||
avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_object);
|
||||
return OutputObjectType{new_object};
|
||||
}
|
||||
|
||||
Writer::OutputUnionType Writer::add_union_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
avro_value_t new_union;
|
||||
avro_value_append(&_parent->val_, &new_union, nullptr);
|
||||
return OutputUnionType{new_union};
|
||||
}
|
||||
|
||||
Writer::OutputUnionType Writer::add_union_to_map(
|
||||
const std::string_view& _name, OutputMapType* _parent) const noexcept {
|
||||
avro_value_t new_union;
|
||||
avro_value_add(&_parent->val_, _name.data(), &new_union, nullptr, nullptr);
|
||||
return OutputUnionType{new_union};
|
||||
}
|
||||
|
||||
Writer::OutputUnionType Writer::add_union_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
avro_value_t new_union;
|
||||
avro_value_get_by_name(&_parent->val_, _name.data(), &new_union, nullptr);
|
||||
return OutputUnionType{new_union};
|
||||
}
|
||||
|
||||
Writer::OutputUnionType Writer::add_union_to_union(
|
||||
const size_t _index, OutputUnionType* _parent) const noexcept {
|
||||
avro_value_t new_union;
|
||||
avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_union);
|
||||
return OutputUnionType{new_union};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_array(
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
avro_value_t new_null;
|
||||
avro_value_append(&_parent->val_, &new_null, nullptr);
|
||||
avro_value_set_null(&new_null);
|
||||
return OutputVarType{new_null};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_map(
|
||||
const std::string_view& _name, OutputMapType* _parent) const noexcept {
|
||||
avro_value_t new_null;
|
||||
avro_value_add(&_parent->val_, _name.data(), &new_null, nullptr, nullptr);
|
||||
avro_value_set_null(&new_null);
|
||||
return OutputVarType{new_null};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_object(
|
||||
const std::string_view& _name, OutputObjectType* _parent) const noexcept {
|
||||
avro_value_t new_null;
|
||||
avro_value_get_by_name(&_parent->val_, _name.data(), &new_null, nullptr);
|
||||
avro_value_set_null(&new_null);
|
||||
return OutputVarType{new_null};
|
||||
}
|
||||
|
||||
Writer::OutputVarType Writer::add_null_to_union(
|
||||
const size_t _index, OutputUnionType* _parent) const noexcept {
|
||||
avro_value_t new_null;
|
||||
avro_value_set_branch(&_parent->val_, static_cast<int>(_index), &new_null);
|
||||
avro_value_set_null(&new_null);
|
||||
return OutputVarType{new_null};
|
||||
}
|
||||
|
||||
} // namespace rfl::avro
|
||||
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
|
||||
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/avro/to_schema.hpp"
|
||||
|
||||
#include "rfl/avro/schema/Type.hpp"
|
||||
#include "rfl/json.hpp"
|
||||
#include "rfl/parsing/schemaful/tuple_to_object.hpp"
|
||||
|
||||
namespace rfl::avro {
|
||||
|
||||
inline bool is_named_type(const parsing::schema::Type& _type) {
|
||||
return _type.variant_.visit([&](const auto& _v) -> bool {
|
||||
using T = std::remove_cvref_t<decltype(_v)>;
|
||||
return std::is_same<T, parsing::schema::Type::Object>() ||
|
||||
std::is_same<T, parsing::schema::Type::Literal>();
|
||||
});
|
||||
}
|
||||
|
||||
schema::Type type_to_avro_schema_type(
|
||||
const parsing::schema::Type& _type,
|
||||
const std::map<std::string, parsing::schema::Type>& _definitions,
|
||||
std::set<std::string>* _already_known, size_t* _num_unnamed) {
|
||||
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::Bytestring>()) {
|
||||
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{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Int64>() ||
|
||||
std::is_same<T, Type::UInt32>() ||
|
||||
std::is_same<T, Type::UInt64>()) {
|
||||
return schema::Type{.value = schema::Type::Long{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Float>()) {
|
||||
return schema::Type{.value = schema::Type::Float{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Double>()) {
|
||||
return schema::Type{.value = schema::Type::Double{}};
|
||||
|
||||
} 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_avro_schema_type(
|
||||
t, _definitions, _already_known, _num_unnamed));
|
||||
}
|
||||
return schema::Type{.value = any_of};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Description>()) {
|
||||
// TODO: Return descriptions
|
||||
return type_to_avro_schema_type(*_t.type_, _definitions, _already_known,
|
||||
_num_unnamed);
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::FixedSizeTypedArray>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Array{
|
||||
.items = Ref<schema::Type>::make(type_to_avro_schema_type(
|
||||
*_t.type_, _definitions, _already_known, _num_unnamed))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Literal>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Enum{.name = std::string("unnamed_") +
|
||||
std::to_string(++(*_num_unnamed)),
|
||||
.symbols = _t.values_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Object>()) {
|
||||
auto record = schema::Type::Record{
|
||||
.name = std::string("unnamed_") + std::to_string(++(*_num_unnamed))};
|
||||
for (const auto& [k, v] : _t.types_) {
|
||||
record.fields.push_back(schema::Type::RecordField{
|
||||
.name = k,
|
||||
.type = Ref<schema::Type>::make(type_to_avro_schema_type(
|
||||
v, _definitions, _already_known, _num_unnamed))});
|
||||
}
|
||||
return schema::Type{.value = record};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Optional>()) {
|
||||
return schema::Type{
|
||||
.value = std::vector<schema::Type>(
|
||||
{type_to_avro_schema_type(*_t.type_, _definitions, _already_known,
|
||||
_num_unnamed),
|
||||
schema::Type{schema::Type::Null{}}})};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Reference>()) {
|
||||
if (_definitions.find(_t.name_) != _definitions.end() &&
|
||||
!is_named_type(_definitions.at(_t.name_))) {
|
||||
return type_to_avro_schema_type(_definitions.at(_t.name_), _definitions,
|
||||
_already_known, _num_unnamed);
|
||||
|
||||
} else if (_already_known->find(_t.name_) != _already_known->end() ||
|
||||
_definitions.find(_t.name_) == _definitions.end()) {
|
||||
return schema::Type{.value = schema::Type::Reference{.type = _t.name_}};
|
||||
|
||||
} else {
|
||||
_already_known->insert(_t.name_);
|
||||
return type_to_avro_schema_type(_definitions.at(_t.name_), _definitions,
|
||||
_already_known, _num_unnamed)
|
||||
.with_name(_t.name_);
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::StringMap>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Map{
|
||||
.values = Ref<schema::Type>::make(
|
||||
type_to_avro_schema_type(*_t.value_type_, _definitions,
|
||||
_already_known, _num_unnamed))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Tuple>()) {
|
||||
return type_to_avro_schema_type(
|
||||
Type{parsing::schemaful::tuple_to_object(_t)}, _definitions,
|
||||
_already_known, _num_unnamed);
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::TypedArray>()) {
|
||||
return schema::Type{
|
||||
.value = schema::Type::Array{
|
||||
.items = Ref<schema::Type>::make(type_to_avro_schema_type(
|
||||
*_t.type_, _definitions, _already_known, _num_unnamed))}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Validated>()) {
|
||||
// Avro knows no validation.
|
||||
return type_to_avro_schema_type(*_t.type_, _definitions, _already_known,
|
||||
_num_unnamed);
|
||||
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Not all cases were covered.");
|
||||
}
|
||||
};
|
||||
|
||||
return rfl::visit(handle_variant, _type.variant_);
|
||||
}
|
||||
|
||||
std::string to_json_representation(
|
||||
const parsing::schema::Definition& _internal_schema) {
|
||||
std::set<std::string> already_known;
|
||||
size_t num_unnamed = 0;
|
||||
const auto avro_schema = type_to_avro_schema_type(
|
||||
_internal_schema.root_, _internal_schema.definitions_, &already_known,
|
||||
&num_unnamed);
|
||||
return rfl::json::write(avro_schema);
|
||||
}
|
||||
|
||||
} // namespace rfl::avro
|
||||
@@ -191,7 +191,8 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
|
||||
std::is_same<T, Type::Double>()) {
|
||||
return schema::Type{.value = schema::Type::Number{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::String>()) {
|
||||
} else if constexpr (std::is_same<T, Type::String>() ||
|
||||
std::is_same<T, Type::Bytestring>()) {
|
||||
return schema::Type{.value = schema::Type::String{}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::AnyOf>()) {
|
||||
@@ -221,7 +222,7 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) {
|
||||
schema::Type::StringEnum{.values = _t.values_}};
|
||||
|
||||
} else if constexpr (std::is_same<T, Type::Object>()) {
|
||||
auto properties = std::map<std::string, schema::Type>();
|
||||
auto properties = rfl::Object<schema::Type>();
|
||||
auto required = std::vector<std::string>();
|
||||
for (const auto& [k, v] : _t.types_) {
|
||||
properties[k] = type_to_json_schema_type(v);
|
||||
|
||||
Reference in New Issue
Block a user