adapter.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #pragma once
  2. #include <cstdint>
  3. #include <optional>
  4. #include <jvalidate/detail/array_iterator.h>
  5. #include <jvalidate/detail/number.h>
  6. #include <jvalidate/detail/object_iterator.h>
  7. #include <jvalidate/enum.h>
  8. #include <jvalidate/forward.h>
  9. #include <jvalidate/status.h>
  10. namespace jvalidate::adapter {
  11. /**
  12. * @brief An interface for a type-erased reference-wrapper around a JSON node.
  13. *
  14. * Unlike languages like python, there are dozens of different C++ Libraries
  15. * for JSON parsing/construction. Each of these libraries has its own set of
  16. * getter functions, rules for handling missing values, and degree to which it
  17. * can engage in fuzziness of types.
  18. *
  19. * Adapter has two main groups of methods:
  20. * - as_*() and *_size() virtual functions
  21. * - maybe_*() concrete functions
  22. *
  23. * Most interaction with Adapter will be done via the maybe_*() functions,
  24. * with or without strictness enabled depending on what constraint is being
  25. * checked.
  26. */
  27. class Adapter {
  28. public:
  29. virtual ~Adapter() = default;
  30. /**
  31. * @brief Get the jvalidate::adapter::Type that this adapter represents.
  32. * This represents the types recognized by json-schema:
  33. * null, bool, integer, number, string, array, object
  34. * This function is meant to be used internally - and not by any of the
  35. * Constraint objects.
  36. */
  37. virtual Type type() const = 0;
  38. /**
  39. * @brief Obtain an immutable copy of the current node.
  40. * Because an Adapter stores a reference to the underlying JSON, it cannot
  41. * be stored by e.g. a Const/Enum Constraint without risking a Segfault.
  42. */
  43. virtual std::unique_ptr<Const const> freeze() const = 0;
  44. /**
  45. * @brief Extract a boolean value from this JSON node.
  46. * @pre type() == Type::Boolean
  47. *
  48. * @throws If the pre-condition is not valid, then this function may throw
  49. * or produce other undefined behavior, depending on the implementation
  50. * details of the underlying type.
  51. */
  52. virtual bool as_boolean() const = 0;
  53. /**
  54. * @brief Extract an integer value from this JSON node.
  55. * @pre type() == Type::Integer
  56. *
  57. * @throws If the pre-condition is not valid, then this function may throw
  58. * or produce other undefined behavior, depending on the implementation
  59. * details of the underlying type.
  60. */
  61. virtual int64_t as_integer() const = 0;
  62. /**
  63. * @brief Extract a decimal value from this JSON node.
  64. * @pre type() == Type::Number
  65. *
  66. * @throws If the pre-condition is not valid, then this function may throw
  67. * or produce other undefined behavior, depending on the implementation
  68. * details of the underlying type.
  69. */
  70. virtual double as_number() const = 0;
  71. /**
  72. * @brief Extract a string value from this JSON node.
  73. * @pre type() == Type::String
  74. *
  75. * @throws If the pre-condition is not valid, then this function may throw
  76. * or produce other undefined behavior, depending on the implementation
  77. * details of the underlying type.
  78. */
  79. virtual std::string as_string() const = 0;
  80. /**
  81. * @brief Get the size of the JSON array in this node.
  82. * @pre type() == Type::Array
  83. *
  84. * @throws If the pre-condition is not valid, then this function may throw
  85. * or produce other undefined behavior, depending on the implementation
  86. * details of the underlying type.
  87. */
  88. virtual size_t array_size() const = 0;
  89. /**
  90. * @brief Get the size of the JSON object in this node.
  91. * @pre type() == Type::Object
  92. *
  93. * @throws If the pre-condition is not valid, then this function may throw
  94. * or produce other undefined behavior, depending on the implementation
  95. * details of the underlying type.
  96. */
  97. virtual size_t object_size() const = 0;
  98. /**
  99. * @brief Loop through every element of the JSON array in this node, applying
  100. * the given callback function to them.
  101. *
  102. * @param cb A callback of the form Adapter => Status
  103. *
  104. * @return Status::Accept iff there are no errors
  105. */
  106. virtual Status apply_array(AdapterCallback const & cb) const = 0;
  107. /**
  108. * @brief Loop through every element of the JSON object in this node, applying
  109. * the given callback function to them.
  110. *
  111. * @param cb A callback of the form (string, Adapter) => Status
  112. *
  113. * @return Status::Accept iff there are no errors
  114. */
  115. virtual Status apply_object(ObjectAdapterCallback const & cb) const = 0;
  116. virtual bool equals(Adapter const & lhs, bool strict) const = 0;
  117. /**
  118. * @brief Test if this object is null-like
  119. *
  120. * @param strict Does this function allow for fuzzy comparisons with strings?
  121. */
  122. bool maybe_null(bool strict) const {
  123. switch (type()) {
  124. case Type::Null:
  125. return true;
  126. case Type::String:
  127. return not strict and as_string().empty();
  128. default:
  129. return false;
  130. }
  131. }
  132. /**
  133. * @brief Attempts to extract a boolean value from this JSON node
  134. *
  135. * @param strict Does this function allow for fuzzy comparisons with strings?
  136. *
  137. * @return The boolean value contained if it is possible to deduce
  138. * (or type() == Type::Boolean), else nullopt.
  139. */
  140. std::optional<bool> maybe_boolean(bool strict) const {
  141. switch (type()) {
  142. case Type::Boolean:
  143. return as_boolean();
  144. case Type::String:
  145. if (not strict) {
  146. auto str = as_string();
  147. if (str == "true") {
  148. return true;
  149. }
  150. if (str == "false") {
  151. return false;
  152. }
  153. }
  154. return std::nullopt;
  155. default:
  156. return std::nullopt;
  157. }
  158. }
  159. /**
  160. * @brief Attempts to extract a integer value from this JSON node
  161. *
  162. * @param strict Does this function allow for fuzzy comparisons with strings
  163. * and/or booleans?
  164. *
  165. * @return The integer value contained if it is possible to deduce from an
  166. * integer, number, boolean, or string. Else nullopt
  167. */
  168. std::optional<int64_t> maybe_integer(bool strict) const {
  169. switch (type()) {
  170. case Type::Number:
  171. if (double value = as_number(); jvalidate::detail::fits_in_integer(value)) {
  172. return static_cast<int64_t>(value);
  173. }
  174. return std::nullopt;
  175. case Type::Integer:
  176. return as_integer();
  177. case Type::Boolean:
  178. if (not strict) {
  179. return as_boolean() ? 1 : 0;
  180. }
  181. return std::nullopt;
  182. case Type::String:
  183. if (not strict) {
  184. auto str = as_string();
  185. size_t end = 0;
  186. int64_t rval = std::stoll(str, &end);
  187. if (end == str.size()) {
  188. return rval;
  189. }
  190. }
  191. return std::nullopt;
  192. default:
  193. return std::nullopt;
  194. }
  195. }
  196. /**
  197. * @brief Attempts to extract a number value from this JSON node
  198. *
  199. * @param strict Does this function allow for fuzzy comparisons with strings?
  200. *
  201. * @return The number value contained if it is possible to deduce
  202. * (or type() == Type::Integer || type() == Type::Number), else nullopt.
  203. */
  204. std::optional<double> maybe_number(bool strict) const {
  205. switch (type()) {
  206. case Type::Number:
  207. return as_number();
  208. case Type::Integer:
  209. return as_integer();
  210. case Type::String:
  211. if (not strict) {
  212. auto str = as_string();
  213. size_t end = 0;
  214. double rval = std::stod(str, &end);
  215. if (end == str.size()) {
  216. return rval;
  217. }
  218. }
  219. return std::nullopt;
  220. default:
  221. return std::nullopt;
  222. }
  223. }
  224. /**
  225. * @brief Attempts to extract a string value from this JSON node
  226. *
  227. * @param strict Does this function allow for fuzzy comparisons with other
  228. * types?
  229. *
  230. * @return The string value contained if it is possible to deduce from a
  231. * scalar type, else nullopt.
  232. */
  233. std::optional<std::string> maybe_string(bool strict) const {
  234. switch (type()) {
  235. case Type::Null:
  236. return strict ? std::nullopt : std::make_optional("");
  237. case Type::String:
  238. return as_string();
  239. case Type::Number:
  240. if (not strict) {
  241. return std::to_string(as_number());
  242. }
  243. return std::nullopt;
  244. case Type::Integer:
  245. if (not strict) {
  246. return std::to_string(as_integer());
  247. }
  248. return std::nullopt;
  249. case Type::Boolean:
  250. if (not strict) {
  251. return as_boolean() ? "true" : "false";
  252. }
  253. return std::nullopt;
  254. default:
  255. return std::nullopt;
  256. }
  257. }
  258. /**
  259. * @brief Attempts to extract the array length from this JSON node
  260. *
  261. * @param strict Does this function allow for fuzzy comparisons with other
  262. * types?
  263. *
  264. * @return array_size() if this is an array, else 0 or nullopt, depending
  265. * on some factors.
  266. */
  267. std::optional<size_t> maybe_array_size(bool strict) const {
  268. switch (type()) {
  269. case Type::Array:
  270. return array_size();
  271. case Type::Null:
  272. return strict ? std::nullopt : std::make_optional(0UL);
  273. case Type::Object:
  274. return (strict || object_size() != 0) ? std::nullopt : std::make_optional(0UL);
  275. default:
  276. return std::nullopt;
  277. }
  278. }
  279. /**
  280. * @brief Attempts to extract the object length from this JSON node
  281. *
  282. * @param strict Does this function allow for fuzzy comparisons with other
  283. * types?
  284. *
  285. * @return object_size() if this is an object, else 0 or nullopt, depending
  286. * on some factors.
  287. */
  288. std::optional<size_t> maybe_object_size(bool strict) const {
  289. switch (type()) {
  290. case Type::Object:
  291. return object_size();
  292. case Type::Null:
  293. return strict ? std::nullopt : std::make_optional(0UL);
  294. case Type::Array:
  295. return (strict || array_size() != 0) ? std::nullopt : std::make_optional(0UL);
  296. default:
  297. return std::nullopt;
  298. }
  299. }
  300. };
  301. /**
  302. * @brief An interface for an immutable, owning handle to a type-erased JSON
  303. * node. {@see Adapter::freeze} for more explaination why this is necessary.
  304. */
  305. class Const {
  306. public:
  307. virtual ~Const() = default;
  308. /**
  309. * @brief Perform an action on this object, such as copying or testing
  310. * equality.
  311. *
  312. * @param cb A callback function of the form Adapter => Status
  313. *
  314. * @return the result of cb on the contained JSON
  315. */
  316. virtual Status apply(AdapterCallback const & cb) const = 0;
  317. };
  318. }
  319. namespace jvalidate::adapter::detail {
  320. /**
  321. * @brief The simplest implementation of Const.
  322. * Depends on the AdapterTraits struct.
  323. *
  324. * @tparam JSON The type being adapted
  325. */
  326. template <typename JSON> class GenericConst final : public Const {
  327. public:
  328. explicit GenericConst(JSON const & value) : value_(value) {}
  329. Status apply(AdapterCallback const & cb) const {
  330. return cb(typename AdapterTraits<JSON>::ConstAdapter(value_));
  331. }
  332. JSON const & value() const { return value_; }
  333. private:
  334. JSON value_;
  335. };
  336. }