Skip to content

Commit

Permalink
Basic to_schema
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Nov 26, 2024
1 parent 05ddb88 commit 1bc8928
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/rfl/avro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "avro/load.hpp"
#include "avro/read.hpp"
#include "avro/save.hpp"
#include "avro/to_schema.hpp"
#include "avro/write.hpp"

#endif
11 changes: 6 additions & 5 deletions include/rfl/avro/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct Type {

struct RecordField {
std::string name;
rfl::Variant<std::string, std::vector<std::string>> type;
rfl::Ref<Type> type;
};

struct Record {
Expand All @@ -65,18 +65,19 @@ struct Type {

struct Array {
Literal<"array"> type;
rfl::Variant<std::string, std::vector<std::string>> items;
rfl::Ref<Type> items;
Rename<"default", std::vector<std::string>> default_;
};

struct Map {
Literal<"map"> type;
rfl::Variant<std::string, std::vector<std::string>> values;
rfl::Ref<Type> values;
Rename<"default", std::map<std::string, std::string>> default_;
};

using ReflectionType = rfl::Variant<Null, Boolean, Int, Long, Float, Double,
Bytes, String, Record, Enum, Array, Map>;
using ReflectionType =
rfl::Variant<Null, Boolean, Int, Long, Float, Double, Bytes, String,
Record, Enum, Array, Map, std::string, std::vector<Type>>;

const auto& reflection() const { return value; }

Expand Down
26 changes: 9 additions & 17 deletions include/rfl/avro/to_schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,24 @@
#include "../parsing/schema/ValidationType.hpp"
#include "../parsing/schema/make.hpp"
#include "Reader.hpp"
#include "Schema.hpp"
#include "Writer.hpp"
#include "schema/JSONSchema.hpp"
#include "schema/Type.hpp"
#include "write.hpp"

namespace rfl::json {
namespace rfl::avro {

template <class T>
struct TypeHelper {};
std::string to_json_representation(
const parsing::schema::Definition& internal_schema);

template <class... Ts>
struct TypeHelper<rfl::Variant<Ts...>> {
using JSONSchemaType = rfl::Variant<schema::JSONSchema<Ts>...>;
};

std::string to_schema_internal_schema(
const parsing::schema::Definition& internal_schema,
const yyjson_write_flag);

/// Returns the JSON schema for a class.
/// Returns the Avro schema for a class.
template <class T, class... Ps>
std::string to_schema(const yyjson_write_flag _flag = 0) {
Schema<T> to_schema() {
const auto internal_schema =
parsing::schema::make<Reader, Writer, T, Processors<Ps...>>();
return to_schema_internal_schema(internal_schema, _flag);
const auto json_str = to_json_representation(internal_schema);
return Schema<T>::from_json(json_str).value();
}
} // namespace rfl::json
} // namespace rfl::avro

#endif
1 change: 1 addition & 0 deletions src/reflectcpp_avro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ SOFTWARE.
#include "rfl/avro/Reader.cpp"
#include "rfl/avro/SchemaImpl.cpp"
#include "rfl/avro/Writer.cpp"
#include "rfl/avro/to_schema.cpp"
130 changes: 130 additions & 0 deletions src/rfl/avro/to_schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
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"

namespace rfl::avro {

schema::Type type_to_avro_schema_type(const parsing::schema::Type& _type) {
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::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));
}
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_);

} 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_))}};

} else if constexpr (std::is_same<T, Type::Literal>()) {
return schema::Type{.value = schema::Type::Enum{.symbols = _t.values_}};

} else if constexpr (std::is_same<T, Type::Object>()) {
auto record = schema::Type::Record{};
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))});
}
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_),
schema::Type{schema::Type::Null{}}})};

} else if constexpr (std::is_same<T, Type::Reference>()) {
return schema::Type{.value = _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_))}};

} else if constexpr (std::is_same<T, Type::Tuple>()) {
// TODO: Handle tuples.
return schema::Type{.value = schema::Type::Record{}};

} 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_))}};

} else if constexpr (std::is_same<T, Type::Validated>()) {
// Avro knows no validation.
return type_to_avro_schema_type(*_t.type_);

} 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) {
const auto avro_schema = type_to_avro_schema_type(internal_schema.root_);
return rfl::json::write(avro_schema);
}

} // namespace rfl::avro

0 comments on commit 1bc8928

Please sign in to comment.