Skip to content

Commit

Permalink
feat(schema): template dict in schema visitor, ref #149
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Dec 3, 2024
1 parent b1a0ee0 commit 5fd3ffd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
33 changes: 16 additions & 17 deletions schema/code/ac/schema/SchemaVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

namespace ac::local::schema {

using OrderedDict = acnl::ordered_json;

template <typename Dict = ac::Dict>
struct SchemaVisitor {
OrderedDict& out;
OrderedDict* props;
Dict& out;
Dict* props;
std::vector<std::string_view> req;

~SchemaVisitor() {
Expand All @@ -20,50 +19,50 @@ struct SchemaVisitor {
}
}

SchemaVisitor(OrderedDict& out) : out(out) {
SchemaVisitor(Dict& out) : out(out) {
out["type"] = "object";
props = &out["properties"];
}

template <std::signed_integral I>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "integer";
}

template <std::unsigned_integral I>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "integer";
}

template <std::floating_point F>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "number";
}

template <std::same_as<std::string> S>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "string";
}

template <std::same_as<bool> B>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "boolean";
}

template <std::same_as<std::nullptr_t> N>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "null";
}

template <Visitable V>
static void describeField(OrderedDict& d) {
static void describeField(Dict& d) {
SchemaVisitor v(d);
V schema;
schema.visitFields(v);
}

template <typename Vec>
static void describeField(OrderedDict& obj) {
static void describeField(Dict& obj) {
obj["type"] = "array";
describeField<typename Vec::value_type>(obj["items"]);
}
Expand All @@ -77,17 +76,17 @@ struct SchemaVisitor {
obj["description"] = desc;
}
if (t.defaultSet()) {
ToDictVisitor<OrderedDict>::writeToDict(obj["default"], t.value());
ToDictVisitor<Dict>::writeToDict(obj["default"], t.value());
}
else if (t.required()) {
req.push_back(name);
}
}
};

template <typename T>
void Struct_toSchema(OrderedDict& out) {
SchemaVisitor::describeField<T>(out);
template <typename T, typename Dict>
void Struct_toSchema(Dict& out) {
SchemaVisitor<Dict>::describeField<T>(out);
}

} // namespace ac::local::schema
Expand Down
2 changes: 1 addition & 1 deletion schema/test/t-visitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TEST_CASE("io visitors") {
}

TEST_CASE("schema") {
OrderedDict dict;
acnl::ordered_json dict;
Struct_toSchema<Company>(dict);

auto schema = dict.dump(2);
Expand Down

0 comments on commit 5fd3ffd

Please sign in to comment.