Skip to content

Commit

Permalink
Made sure that the schema generation supports NoFieldNames
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jun 14, 2024
1 parent 618b319 commit 07cad4c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 19 deletions.
52 changes: 33 additions & 19 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ struct NamedTupleParser {
std::conditional_t<_no_field_names, typename W::OutputArrayType,
typename W::OutputObjectType>;

using SchemaType = std::conditional_t<_no_field_names, schema::Type::Tuple,
schema::Type::Object>;

static constexpr size_t size_ = NamedTupleType::size();

public:
Expand Down Expand Up @@ -109,26 +112,13 @@ struct NamedTupleParser {
}
}

template <size_t _i = 0>
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions,
std::map<std::string, schema::Type> _values = {}) {
using Type = schema::Type;
using T = NamedTuple<FieldTypes...>;
constexpr size_t size = T::size();
if constexpr (_i == size) {
return Type{Type::Object{_values}};
} else {
using F =
std::tuple_element_t<_i, typename NamedTuple<FieldTypes...>::Fields>;
using U = typename F::Type;
if constexpr (!internal::is_skip_v<U>) {
_values[std::string(F::name())] =
Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
}
return to_schema<_i + 1>(_definitions, _values);
}
};
std::map<std::string, schema::Type>* _definitions) noexcept {
SchemaType schema;
build_schema(_definitions, &schema,
std::make_integer_sequence<int, size_>());
return schema::Type{schema};
}

private:
template <int _i>
Expand Down Expand Up @@ -161,13 +151,37 @@ struct NamedTupleParser {
}
}

template <size_t _i>
static void add_field_to_schema(
std::map<std::string, schema::Type>* _definitions,
SchemaType* _schema) noexcept {
using F =
std::tuple_element_t<_i, typename NamedTuple<FieldTypes...>::Fields>;
using U = typename F::Type;
if constexpr (!internal::is_skip_v<U>) {
auto s = Parser<R, W, U, ProcessorsType>::to_schema(_definitions);
if constexpr (_no_field_names) {
_schema->types_.emplace_back(std::move(s));
} else {
_schema->types_[std::string(F::name())] = std::move(s);
}
}
};

template <int... _is>
static void build_object(const W& _w, const NamedTuple<FieldTypes...>& _tup,
OutputObjectOrArrayType* _ptr,
std::integer_sequence<int, _is...>) noexcept {
(add_field_to_object<_is>(_w, _tup, _ptr), ...);
}

template <int... _is>
static void build_schema(std::map<std::string, schema::Type>* _definitions,
SchemaType* _schema,
std::integer_sequence<int, _is...>) noexcept {
(add_field_to_schema<_is>(_definitions, _schema), ...);
}

/// Generates error messages for when fields are missing.
template <int _i>
static void handle_one_missing_field(const std::array<bool, size_>& _found,
Expand Down
73 changes: 73 additions & 0 deletions tests/json/test_json_schema3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <tuple>
#include <variant>
#include <vector>

#include "write_and_read.hpp"

namespace test_json_schema3 {

enum class Color { red, green, blue };

struct Rectangle {
double width;
double height;
};

struct Square {
double width;
};

struct Circle {
double radius;
};

using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;

using FieldVariant =
rfl::Variant<rfl::Field<"rectangle", Rectangle>,
rfl::Field<"square", Square>, rfl::Field<"circle", Circle>>;

struct Person {
std::string first_name;
std::string last_name = "Simpson";
rfl::Description<"Must be a proper email in the form xxx@xxx.xxx.",
rfl::Email>
email;
std::string town = "Springfield";
Color color;
Age age;
float salary;
rfl::Description<
"The person's children. Pass an empty array for no children.",
std::vector<Person>>
children;
std::variant<Color, std::vector<Person>, int> variant;
std::tuple<Color, std::vector<Person>, int> tuple;
rfl::Rename<"taggedUnion",
rfl::TaggedUnion<"shape", Rectangle, Square, Circle>>
tagged_union;
rfl::Rename<"fieldVariant", FieldVariant> field_variant;
};

TEST(json, test_json_schema3) {
using DescribedType = rfl::Description<
"JSON schema that describes the required "
"attributes for the person class.",
Person>;

const auto json_schema =
rfl::json::to_schema<DescribedType, rfl::NoFieldNames>();

std::cout << json_schema << std::endl << std::endl;

const std::string expected =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema3__Person","description":"JSON schema that describes the required attributes for the person class.","definitions":{"test_json_schema3__Circle":{"type":"array","prefixItems":[{"type":"number"}],"items":false},"test_json_schema3__Circle__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Circle"]},{"type":"number"}],"items":false},"test_json_schema3__Person":{"type":"array","prefixItems":[{"type":"string"},{"type":"string"},{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},{"type":"string"},{"type":"string","enum":["red","green","blue"]},{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},{"type":"number"},{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema3__Person"}},{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema3__Person"}},{"type":"integer"}]},{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema3__Person"}},{"type":"integer"}],"items":false},{"anyOf":[{"$ref":"#/definitions/test_json_schema3__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema3__Square__tagged"},{"$ref":"#/definitions/test_json_schema3__Circle__tagged"}]},{"anyOf":[{"type":"array","prefixItems":[{"$ref":"#/definitions/test_json_schema3__Rectangle"}],"items":false},{"type":"array","prefixItems":[{"$ref":"#/definitions/test_json_schema3__Square"}],"items":false},{"type":"array","prefixItems":[{"$ref":"#/definitions/test_json_schema3__Circle"}],"items":false}]}],"items":false},"test_json_schema3__Rectangle":{"type":"array","prefixItems":[{"type":"number"},{"type":"number"}],"items":false},"test_json_schema3__Rectangle__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Rectangle"]},{"type":"number"},{"type":"number"}],"items":false},"test_json_schema3__Square":{"type":"array","prefixItems":[{"type":"number"}],"items":false},"test_json_schema3__Square__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Square"]},{"type":"number"}],"items":false}}})";

EXPECT_EQ(json_schema, expected);
}
} // namespace test_json_schema3

0 comments on commit 07cad4c

Please sign in to comment.