26 #ifndef FABLE_SCHEMA_ARRAY_HPP_ 27 #define FABLE_SCHEMA_ARRAY_HPP_ 32 #include <type_traits> 41 template <
typename T,
typename P>
44 using Type = std::vector<T>;
45 using PrototypeSchema = P;
47 Array(Type* ptr, std::string&& desc);
48 Array(Type* ptr,
const PrototypeSchema& prototype)
50 Array(Type* ptr,
const PrototypeSchema& prototype, std::string&& desc)
51 :
Base<Array<T, P>>(JsonType::array, std::move(desc)), prototype_(prototype), ptr_(ptr) {}
55 Array(Type* ptr, std::string&& desc)
56 :
Array(ptr, make_prototype<T>(), std::move(desc)) {}
66 bool extend()
const {
return option_extend_; }
77 option_extend_ = value;
78 return std::move(*
this);
81 size_t min_items()
const {
return min_items_; }
82 void set_min_items(
size_t value) { min_items_ = value; }
85 return std::move(*
this);
88 size_t max_items()
const {
return max_items_; }
89 void set_max_items(
size_t value) { max_items_ = value; }
92 return std::move(*
this);
96 std::string
type_string()
const override {
return "array of " + prototype_.type_string(); }
101 {
"items", prototype_.json_schema()},
103 if (min_items_ != 0) {
104 j[
"minItems"] = min_items_;
106 if (max_items_ != std::numeric_limits<size_t>::max()) {
107 j[
"maxItems"] = max_items_;
109 this->augment_schema(j);
114 this->validate_type(c);
115 if (c->size() < min_items_) {
116 this->throw_error(c,
"require at least {} items in array, got {}", min_items_, c->size());
118 if (c->size() > max_items_) {
119 this->throw_error(c,
"expect at most {} items in array, got {}", max_items_, c->size());
122 for (
const auto& x : c.
to_array()) {
123 prototype_.validate(x);
129 assert(ptr_ !=
nullptr);
130 j = serialize(*ptr_);
134 assert(ptr_ !=
nullptr);
135 assert(c->type() == this->type_);
137 if (!option_extend_) {
141 ptr_->reserve(c->size() + ptr_->size());
145 Json serialize(
const Type& xs)
const {
146 Json j = Json::array();
147 for (
const auto& x : xs) {
148 j.emplace_back(prototype_.serialize(x));
153 Type deserialize(
const Conf& c)
const {
155 vec.reserve(c->size());
163 void fill(Type& vec,
const Conf& c)
const {
164 for (
const auto& x : c.
to_array()) {
165 T inst = prototype_.deserialize(x);
166 vec.emplace_back(std::move(inst));
171 bool option_extend_{
false};
172 size_t min_items_{0};
173 size_t max_items_{std::numeric_limits<size_t>::max()};
174 PrototypeSchema prototype_{};
178 template <
typename T,
typename P>
179 Array<T, P> make_schema(std::vector<T>* ptr,
const P& prototype, std::string&& desc) {
180 return Array<T, P>(ptr, prototype, std::move(desc));
186 #endif // FABLE_SCHEMA_ARRAY_HPP_
void to_json(Json &j) const override
Definition: array.hpp:128
void validate(const Conf &c) const override
Definition: array.hpp:113
nlohmann::json Json
Definition: json.hpp:62
void reset_ptr() override
Definition: array.hpp:160
virtual Json to_json() const
Definition: interface.hpp:220
Json json_schema() const override
Definition: array.hpp:98
void set_extend(bool value)
Definition: array.hpp:71
std::vector< Conf > to_array() const
Definition: conf.cpp:92
Array< T, P > extend(bool value) &&
Definition: array.hpp:76
std::string type_string() const override
Definition: array.hpp:96
void from_conf(const Conf &c) override
Definition: array.hpp:133
Definition: interface.hpp:354
bool extend() const
Definition: array.hpp:66