33 #include <fable/utility/templates.hpp>
35 namespace fable::schema {
37 template <
typename T,
typename Period>
40 using Type = std::chrono::duration<T, Period>;
42 template <
typename X = T, std::enable_if_t<std::is_
integral_v<X> && std::is_
unsigned_v<X>,
int> = 0>
43 Duration(Type* ptr, std::string desc)
46 template <
typename X = T, std::enable_if_t<std::is_
integral_v<X> && std::is_
signed_v<X>,
int> = 0>
47 Duration(Type* ptr, std::string desc)
50 template <
typename X = T, std::enable_if_t<std::is_
floating_po
int_v<X>,
int> = 0>
51 Duration(Type* ptr, std::string desc)
55 [[nodiscard]] T minimum()
const {
return value_min_; }
57 void set_minimum(T value) {
59 exclusive_min_ =
false;
64 return std::move(*
this);
67 [[nodiscard]]
bool exclusive_minimum()
const {
return exclusive_min_; }
69 void set_exclusive_minimum(T value) {
71 exclusive_min_ =
true;
75 set_exclusive_minimum(value);
76 return std::move(*
this);
79 [[nodiscard]] T maximum()
const {
return value_max_; }
81 void set_maximum(T value) {
83 exclusive_max_ =
false;
88 exclusive_max_ =
false;
89 return std::move(*
this);
92 [[nodiscard]]
bool exclusive_maximum()
const {
return exclusive_max_; }
94 void set_exclusive_maximum(T value) {
96 exclusive_max_ =
true;
101 exclusive_max_ =
true;
102 return std::move(*
this);
105 [[nodiscard]] std::pair<T, T> bounds()
const {
return std::make_pair(value_min_, value_max_); }
107 void set_bounds(T min, T max) {
108 exclusive_min_ =
false;
110 exclusive_max_ =
false;
115 set_bounds(min, max);
116 return std::move(*
this);
123 {exclusive_min_ ?
"exclusiveMinimum" :
"minimum", value_min_},
124 {exclusive_max_ ?
"exclusiveMaximum" :
"maximum", value_max_},
126 this->augment_schema(j);
130 bool validate(
const Conf& c, std::optional<SchemaError>& err)
const override {
132 case JsonType::number_unsigned:
133 return validate_bounds<uint64_t>(c, err);
134 case JsonType::number_integer:
135 return validate_bounds<int64_t>(c, err);
136 case JsonType::number_float:
137 if (this->
type() != JsonType::number_float) {
138 return this->set_wrong_type(err, c);
140 return validate_bounds<double>(c, err);
143 return this->set_wrong_type(err, c);
150 assert(ptr_ !=
nullptr);
151 j = serialize(*ptr_);
155 assert(ptr_ !=
nullptr);
156 *ptr_ = deserialize(c);
159 [[nodiscard]]
Json serialize(
const Type& x)
const {
return x.count(); }
161 [[nodiscard]] Type deserialize(
const Conf& c)
const {
return Type(c.
get<T>()); }
163 void serialize_into(Json& j,
const Type& x)
const { j = x.count(); }
165 void deserialize_into(
const Conf& c, Type& x)
const { x = deserialize(c); }
173 template <
typename B>
174 bool validate_bounds(
const Conf& c, std::optional<SchemaError>& err)
const {
175 auto original = c.
get<B>();
176 auto value =
static_cast<T
>(original);
177 if constexpr (!std::is_floating_point_v<T>) {
178 if (!is_cast_safe<T>(original)) {
179 return this->set_error(err, c,
180 "failed to convert input to destination type {}, got {}( {} ) = {}",
186 if (exclusive_min_) {
187 if (value <= value_min_) {
188 return this->set_error(err, c,
"expected exclusive minimum of {}, got {}", value_min_,
192 if (value < value_min_) {
193 return this->set_error(err, c,
"expected minimum of {}, got {}", value_min_, value);
197 if (exclusive_max_) {
198 if (value >= value_max_) {
199 return this->set_error(err, c,
"expected exclusive maximum of {}, got {}", value_max_,
203 if (value > value_max_) {
204 return this->set_error(err, c,
"expected maximum of {}, got {}", value_max_, value);
212 bool exclusive_min_{
false};
213 bool exclusive_max_{
false};
214 T value_min_{std::numeric_limits<T>::lowest()};
215 T value_max_{std::numeric_limits<T>::max()};
219 template <
typename Rep,
typename Period,
typename S>
220 Duration<Rep, Period> make_schema(std::chrono::duration<Rep, Period>* ptr, S&& desc) {
221 return {ptr, std::forward<S>(desc)};
T get() const
Definition: conf.hpp:297
Definition: interface.hpp:398
JsonType type() const override
Definition: interface.hpp:418
std::string type_string() const override
Definition: interface.hpp:419
Definition: duration.hpp:38
void to_json(Json &j) const override
Definition: duration.hpp:149
void from_conf(const Conf &c) override
Definition: duration.hpp:154
Json json_schema() const override
Definition: duration.hpp:120
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: duration.hpp:130
void reset_ptr() override
Definition: duration.hpp:167
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35
Definition: templates.hpp:66