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,
43 std::enable_if_t<std::is_integral_v<X> && std::is_unsigned_v<X>,
int> = 0>
44 Duration(Type* ptr, std::string desc)
47 template <
typename X = T, std::enable_if_t<std::is_
integral_v<X> && std::is_
signed_v<X>,
int> = 0>
48 Duration(Type* ptr, std::string desc)
51 template <
typename X = T, std::enable_if_t<std::is_
floating_po
int_v<X>,
int> = 0>
52 Duration(Type* ptr, std::string desc)
56 [[nodiscard]] T minimum()
const {
return value_min_; }
57 [[nodiscard]]
bool exclusive_minimum()
const {
return exclusive_min_; }
60 exclusive_min_ =
false;
61 return std::move(*
this);
65 exclusive_min_ =
true;
66 return std::move(*
this);
69 [[nodiscard]] T maximum()
const {
return value_max_; }
70 [[nodiscard]]
bool exclusive_maximum()
const {
return exclusive_max_; }
73 exclusive_max_ =
false;
74 return std::move(*
this);
78 exclusive_max_ =
true;
79 return std::move(*
this);
82 [[nodiscard]] std::pair<T, T> bounds()
const {
return std::make_pair(value_min_, value_max_); }
84 exclusive_min_ =
false;
86 exclusive_max_ =
false;
88 return std::move(*
this);
95 {exclusive_min_ ?
"exclusiveMinimum" :
"minimum", value_min_},
96 {exclusive_max_ ?
"exclusiveMaximum" :
"maximum", value_max_},
98 this->augment_schema(j);
102 bool validate(
const Conf& c, std::optional<SchemaError>& err)
const override {
104 case JsonType::number_unsigned:
105 return validate_bounds<uint64_t>(c, err);
106 case JsonType::number_integer:
107 return validate_bounds<int64_t>(c, err);
108 case JsonType::number_float:
109 if (this->
type() != JsonType::number_float) {
110 return this->set_wrong_type(err, c);
112 return validate_bounds<double>(c, err);
115 return this->set_wrong_type(err, c);
121 assert(ptr_ !=
nullptr);
122 j = serialize(*ptr_);
126 assert(ptr_ !=
nullptr);
127 *ptr_ = deserialize(c);
130 [[nodiscard]]
Json serialize(
const Type& x)
const {
return x.count(); }
132 [[nodiscard]] Type deserialize(
const Conf& c)
const {
return Type(c.
get<T>()); }
134 void serialize_into(Json& j,
const Type& x)
const { j = x.count(); }
136 void deserialize_into(
const Conf& c, Type& x)
const { x = deserialize(c); }
144 template <
typename B>
145 bool validate_bounds(
const Conf& c, std::optional<SchemaError>& err)
const {
146 auto original = c.
get<B>();
147 auto value =
static_cast<T
>(original);
148 if constexpr (!std::is_floating_point_v<T>) {
149 if (!is_cast_safe<T>(original)) {
150 return this->set_error(err, c,
151 "failed to convert input to destination type {}, got {}( {} ) = {}",
157 if (exclusive_min_) {
158 if (value <= value_min_) {
159 return this->set_error(err, c,
"expected exclusive minimum of {}, got {}", value_min_,
163 if (value < value_min_) {
164 return this->set_error(err, c,
"expected minimum of {}, got {}", value_min_, value);
168 if (exclusive_max_) {
169 if (value >= value_max_) {
170 return this->set_error(err, c,
"expected exclusive maximum of {}, got {}", value_max_,
174 if (value > value_max_) {
175 return this->set_error(err, c,
"expected maximum of {}, got {}", value_max_, value);
183 bool exclusive_min_{
false};
184 bool exclusive_max_{
false};
185 T value_min_{std::numeric_limits<T>::lowest()};
186 T value_max_{std::numeric_limits<T>::max()};
190 template <
typename Rep,
typename Period>
191 inline Duration<Rep, Period> make_schema(std::chrono::duration<Rep, Period>* ptr,
193 return Duration<Rep, Period>(ptr, std::move(desc));
T get() const
Definition: conf.hpp:166
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:120
void from_conf(const Conf &c) override
Definition: duration.hpp:125
Json json_schema() const override
Definition: duration.hpp:92
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: duration.hpp:102
void reset_ptr() override
Definition: duration.hpp:138
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35
Definition: templates.hpp:66