mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-11-29 22:36:19 +08:00
This commit is contained in:
@@ -193,7 +193,9 @@ struct Parser {
|
|||||||
} else if constexpr (internal::is_validator_v<U>) {
|
} else if constexpr (internal::is_validator_v<U>) {
|
||||||
return make_validated<U>(_definitions);
|
return make_validated<U>(_definitions);
|
||||||
|
|
||||||
} else if constexpr (internal::has_reflection_type_v<U>) {
|
} else if constexpr (internal::has_reflection_type_v<U> ||
|
||||||
|
internal::has_read_reflector<U> ||
|
||||||
|
internal::has_write_reflector<U>) {
|
||||||
return make_reference<U>(_definitions);
|
return make_reference<U>(_definitions);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -235,13 +237,21 @@ struct Parser {
|
|||||||
std::map<std::string, schema::Type>* _definitions) {
|
std::map<std::string, schema::Type>* _definitions) {
|
||||||
using Type = schema::Type;
|
using Type = schema::Type;
|
||||||
const auto name = make_type_name<U>();
|
const auto name = make_type_name<U>();
|
||||||
|
|
||||||
if (_definitions->find(name) == _definitions->end()) {
|
if (_definitions->find(name) == _definitions->end()) {
|
||||||
(*_definitions)[name] =
|
(*_definitions)[name] =
|
||||||
Type{Type::Integer{}}; // Placeholder to avoid infinite loop.
|
Type{Type::Integer{}}; // Placeholder to avoid infinite loop.
|
||||||
|
|
||||||
if constexpr (internal::has_reflection_type_v<U>) {
|
if constexpr (internal::has_reflection_type_v<U>) {
|
||||||
(*_definitions)[name] =
|
(*_definitions)[name] =
|
||||||
Parser<R, W, typename U::ReflectionType, ProcessorsType>::to_schema(
|
Parser<R, W, typename U::ReflectionType, ProcessorsType>::to_schema(
|
||||||
_definitions);
|
_definitions);
|
||||||
|
|
||||||
|
} else if constexpr (internal::has_read_reflector<U> ||
|
||||||
|
internal::has_write_reflector<U>) {
|
||||||
|
(*_definitions)[name] = Parser<R, W, typename Reflector<U>::ReflType,
|
||||||
|
ProcessorsType>::to_schema(_definitions);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
using NamedTupleType = internal::processed_t<U, ProcessorsType>;
|
using NamedTupleType = internal::processed_t<U, ProcessorsType>;
|
||||||
(*_definitions)[name] =
|
(*_definitions)[name] =
|
||||||
|
|||||||
@@ -4,8 +4,7 @@
|
|||||||
#include "../type_name_t.hpp"
|
#include "../type_name_t.hpp"
|
||||||
#include "is_tagged_union_wrapper.hpp"
|
#include "is_tagged_union_wrapper.hpp"
|
||||||
|
|
||||||
namespace rfl {
|
namespace rfl::parsing {
|
||||||
namespace parsing {
|
|
||||||
|
|
||||||
inline std::string replace_non_alphanumeric(std::string _str) {
|
inline std::string replace_non_alphanumeric(std::string _str) {
|
||||||
for (auto& ch : _str) {
|
for (auto& ch : _str) {
|
||||||
@@ -24,7 +23,6 @@ static std::string make_type_name() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace parsing
|
} // namespace rfl::parsing
|
||||||
} // namespace rfl
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <rfl.hpp>
|
||||||
|
#include <rfl/json.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "write_and_read.hpp"
|
||||||
|
|
||||||
|
namespace test_reflector_schema {
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
std::string first_name = "Homer";
|
||||||
|
std::string last_name = "Simpson";
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Parent : Person {
|
||||||
|
public:
|
||||||
|
std::vector<Person> children;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace test_reflector_schema
|
||||||
|
|
||||||
|
namespace rfl {
|
||||||
|
template <>
|
||||||
|
struct Reflector<test_reflector_schema::Parent> {
|
||||||
|
struct ReflType {
|
||||||
|
std::string first_name;
|
||||||
|
std::string last_name;
|
||||||
|
std::vector<test_reflector_schema::Person> children;
|
||||||
|
};
|
||||||
|
|
||||||
|
static ReflType from(const test_reflector_schema::Parent& v) {
|
||||||
|
return {v.first_name, v.last_name, v.children};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace rfl
|
||||||
|
|
||||||
|
namespace test_reflector_schema {
|
||||||
|
|
||||||
|
TEST(json, test_reflector_schema) {
|
||||||
|
const std::string expected =
|
||||||
|
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_reflector_schema__Parent","$defs":{"rfl__Reflector_test_reflector_schema__Parent___ReflType":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"},"children":{"type":"array","items":{"$ref":"#/$defs/test_reflector_schema__Person"}}},"required":["first_name","last_name","children"]},"test_reflector_schema__Parent":{"$ref":"#/$defs/rfl__Reflector_test_reflector_schema__Parent___ReflType"},"test_reflector_schema__Person":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"}},"required":["first_name","last_name"]}}})";
|
||||||
|
|
||||||
|
const std::string expected_windows =
|
||||||
|
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_reflector_schema__Parent","$defs":{"rfl__Reflector_struct_test_reflector_schema__Parent___ReflType":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"},"children":{"type":"array","items":{"$ref":"#/$defs/test_reflector_schema__Person"}}},"required":["first_name","last_name","children"]},"test_reflector_schema__Parent":{"$ref":"#/$defs/rfl__Reflector_struct_test_reflector_schema__Parent___ReflType"},"test_reflector_schema__Person":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"}},"required":["first_name","last_name"]}}})";
|
||||||
|
|
||||||
|
EXPECT_TRUE(rfl::json::to_schema<Parent>() == expected ||
|
||||||
|
rfl::json::to_schema<Parent>() == expected_windows);
|
||||||
|
}
|
||||||
|
} // namespace test_reflector_schema
|
||||||
Reference in New Issue
Block a user