/* 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; return std::is_same() || std::is_same(); }); } schema::Type type_to_avro_schema_type( const parsing::schema::Type& _type, const std::map& _definitions, std::set* _already_known, size_t* _num_unnamed) { auto handle_variant = [&](const auto& _t) -> schema::Type { using T = std::remove_cvref_t; using Type = parsing::schema::Type; if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::Boolean{}}; } else if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::Bytes{}}; } else if constexpr (std::is_same() || std::is_same() || std::is_same() || std::is_same() || std::is_same()) { return schema::Type{.value = schema::Type::Int{}}; } else if constexpr (std::is_same() || std::is_same() || std::is_same()) { return schema::Type{.value = schema::Type::Long{}}; } else if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::Float{}}; } else if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::Double{}}; } else if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::String{}}; } else if constexpr (std::is_same()) { auto any_of = std::vector(); 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()) { // TODO: Return descriptions return type_to_avro_schema_type(*_t.type_, _definitions, _already_known, _num_unnamed); } else if constexpr (std::is_same()) { return schema::Type{ .value = schema::Type::Array{ .items = Ref::make(type_to_avro_schema_type( *_t.type_, _definitions, _already_known, _num_unnamed))}}; } else if constexpr (std::is_same()) { 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()) { 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::make(type_to_avro_schema_type( v, _definitions, _already_known, _num_unnamed))}); } return schema::Type{.value = record}; } else if constexpr (std::is_same()) { return schema::Type{ .value = std::vector( {type_to_avro_schema_type(*_t.type_, _definitions, _already_known, _num_unnamed), schema::Type{schema::Type::Null{}}})}; } else if constexpr (std::is_same()) { 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()) { return schema::Type{ .value = schema::Type::Map{ .values = Ref::make( type_to_avro_schema_type(*_t.value_type_, _definitions, _already_known, _num_unnamed))}}; } else if constexpr (std::is_same()) { return type_to_avro_schema_type( Type{parsing::schemaful::tuple_to_object(_t)}, _definitions, _already_known, _num_unnamed); } else if constexpr (std::is_same()) { return schema::Type{ .value = schema::Type::Array{ .items = Ref::make(type_to_avro_schema_type( *_t.type_, _definitions, _already_known, _num_unnamed))}}; } else if constexpr (std::is_same()) { // Avro knows no validation. return type_to_avro_schema_type(*_t.type_, _definitions, _already_known, _num_unnamed); } else { static_assert(rfl::always_false_v, "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 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