Skip to content

Commit

Permalink
fable: Add to_json() method to all schema types
Browse files Browse the repository at this point in the history
  • Loading branch information
cassava committed Feb 22, 2021
1 parent d771921 commit a97ee64
Show file tree
Hide file tree
Showing 19 changed files with 31 additions and 6 deletions.
7 changes: 1 addition & 6 deletions fable/include/fable/schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,10 @@ class Schema : public schema::Interface {
return j;
}

Json to_json() const {
Json j;
to_json(j);
return j;
}

friend void to_json(Json& j, const Schema& s) { s.impl_->to_json(j); }

public: // Overrides
using Interface::to_json;
operator schema::Box() const { return schema::Box{impl_}; }
Interface* clone() const override { return impl_->clone(); }
JsonType type() const override { return impl_->type(); }
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Array : public Base<Array<T, P>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Boolean : public Base<Boolean> {

void validate(const Conf& c) const override { this->validate_type(c); }

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/confable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class FromConfable : public Base<FromConfable<T>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
ptr_->to_json(j);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Const : public Base<Const<T, P>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override { j = serialize(constant_); }

void from_conf(const Conf& c) override { validate(c); }
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/duration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Duration : public Base<Duration<T, Period>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Enum : public Base<Enum<T>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class FactoryBase : public Base<CRTP> {
throw std::logic_error("FactoryBase::from_conf() should not be used");
}

using Interface::to_json;
void to_json(Json& j) const override {
throw std::logic_error("FactoryBase::to_json() should not be used");
}
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/ignore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Ignore : public Base<Ignore> {
}

void validate(const Conf&) const override {}
using Interface::to_json;
void to_json(Json& j) const override { j = nullptr; }
void from_conf(const Conf&) override {}
void reset_ptr() override {}
Expand Down
13 changes: 13 additions & 0 deletions fable/include/fable/schema/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ class Interface {
return true;
}

/**
* Return the current value of the destination.
*
* Warning: This is NOT an efficient operation, but it can be useful for
* cases where speed is not important.
*/
virtual Json to_json() const {
Json j;
to_json(j);
return j;
}

/**
* Return the current value of the destination.
*
Expand Down Expand Up @@ -306,6 +318,7 @@ class Box : public Interface {
}

public: // Overrides
using Interface::to_json;
Interface* clone() const override { return impl_->clone(); }
JsonType type() const override { return impl_->type(); }
std::string type_string() const override { return impl_->type_string(); }
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class FromJson : public Base<FromJson<T>> {

void validate(const Conf& c) const override { this->validate_type(c); }

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = static_cast<const Type&>(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Map : public Base<Map<T, P>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Number : public Base<Number<T>> {
}
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Optional : public Base<Optional<T, P>> {
prototype_.validate(c);
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/passthru.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Passthru : public Base<Passthru<P>> {
prototype_.validate(c);
}

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = **ptr_;
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class Path : public Base<Path> {
Json json_schema() const override;
void validate(const Conf& c) const override;

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class String : public Base<String> {
Json json_schema() const override;
void validate(const Conf& c) const override;

using Interface::to_json;
void to_json(Json& j) const override {
assert(ptr_ != nullptr);
j = serialize(*ptr_);
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class Struct : public Base<Struct> {
void reset_ptr() override;

public: // Overrides
using Interface::to_json;
Json usage() const override;
Json json_schema() const override;
void validate(const Conf& c) const override;
Expand Down
1 change: 1 addition & 0 deletions fable/include/fable/schema/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Variant : public Interface {
}

public: // Overrides
using Interface::to_json;
Json json_schema() const override;
void validate(const Conf& c) const override { validate_index(c); }
void to_json(Json& j) const override { schemas_[0].to_json(j); }
Expand Down

0 comments on commit a97ee64

Please sign in to comment.