cmJSONHelpers.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <algorithm>
  6. #include <functional>
  7. #include <iostream>
  8. #include <map>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/optional>
  13. #include <cm/string_view>
  14. #include <cm3p/json/value.h>
  15. #include "cmJSONState.h"
  16. #include "cmStringAlgorithms.h"
  17. template <typename T>
  18. using cmJSONHelper =
  19. std::function<bool(T& out, const Json::Value* value, cmJSONState* state)>;
  20. using ErrorGenerator = std::function<void(const Json::Value*, cmJSONState*)>;
  21. namespace JsonErrors {
  22. enum ObjectError
  23. {
  24. RequiredMissing,
  25. InvalidObject,
  26. ExtraField,
  27. MissingRequired
  28. };
  29. using ErrorGenerator = std::function<void(const Json::Value*, cmJSONState*)>;
  30. using ObjectErrorGenerator =
  31. std::function<ErrorGenerator(ObjectError, const Json::Value::Members&)>;
  32. ErrorGenerator EXPECTED_TYPE(const std::string& type);
  33. void INVALID_STRING(const Json::Value* value, cmJSONState* state);
  34. void INVALID_BOOL(const Json::Value* value, cmJSONState* state);
  35. void INVALID_INT(const Json::Value* value, cmJSONState* state);
  36. void INVALID_UINT(const Json::Value* value, cmJSONState* state);
  37. ObjectErrorGenerator INVALID_NAMED_OBJECT(
  38. const std::function<std::string(const Json::Value*, cmJSONState*)>&
  39. nameGenerator);
  40. ErrorGenerator INVALID_OBJECT(ObjectError errorType,
  41. const Json::Value::Members& extraFields);
  42. ErrorGenerator INVALID_NAMED_OBJECT_KEY(
  43. ObjectError errorType, const Json::Value::Members& extraFields);
  44. }
  45. struct cmJSONHelperBuilder
  46. {
  47. template <typename T>
  48. class Object
  49. {
  50. public:
  51. Object(JsonErrors::ObjectErrorGenerator error = JsonErrors::INVALID_OBJECT,
  52. bool allowExtra = true)
  53. : Error(std::move(error))
  54. , AllowExtra(allowExtra)
  55. {
  56. }
  57. template <typename U, typename M, typename F>
  58. Object& Bind(const cm::string_view& name, M U::*member, F func,
  59. bool required = true)
  60. {
  61. return this->BindPrivate(
  62. name,
  63. [func, member](T& out, const Json::Value* value, cmJSONState* state)
  64. -> bool { return func(out.*member, value, state); },
  65. required);
  66. }
  67. template <typename M, typename F>
  68. Object& Bind(const cm::string_view& name, std::nullptr_t, F func,
  69. bool required = true)
  70. {
  71. return this->BindPrivate(
  72. name,
  73. [func](T& /*out*/, const Json::Value* value,
  74. cmJSONState* state) -> bool {
  75. M dummy;
  76. return func(dummy, value, state);
  77. },
  78. required);
  79. }
  80. template <typename F>
  81. Object& Bind(const cm::string_view& name, F func, bool required = true)
  82. {
  83. return this->BindPrivate(name, MemberFunction(func), required);
  84. }
  85. bool operator()(T& out, const Json::Value* value, cmJSONState* state) const
  86. {
  87. Json::Value::Members extraFields;
  88. bool success = true;
  89. if (!value && this->AnyRequired) {
  90. Error(JsonErrors::ObjectError::RequiredMissing, extraFields)(value,
  91. state);
  92. return false;
  93. }
  94. if (value && !value->isObject()) {
  95. Error(JsonErrors::ObjectError::InvalidObject, extraFields)(value,
  96. state);
  97. return false;
  98. }
  99. if (value) {
  100. extraFields = value->getMemberNames();
  101. }
  102. for (auto const& m : this->Members) {
  103. std::string name(m.Name.data(), m.Name.size());
  104. state->push_stack(name, value);
  105. if (value && value->isMember(name)) {
  106. if (!m.Function(out, &(*value)[name], state)) {
  107. success = false;
  108. }
  109. extraFields.erase(
  110. std::find(extraFields.begin(), extraFields.end(), name));
  111. } else if (!m.Required) {
  112. if (!m.Function(out, nullptr, state)) {
  113. success = false;
  114. }
  115. } else {
  116. Error(JsonErrors::ObjectError::MissingRequired, extraFields)(value,
  117. state);
  118. success = false;
  119. }
  120. state->pop_stack();
  121. }
  122. if (!this->AllowExtra && !extraFields.empty()) {
  123. Error(JsonErrors::ObjectError::ExtraField, extraFields)(value, state);
  124. success = false;
  125. }
  126. return success;
  127. }
  128. private:
  129. // Not a true cmJSONHelper, it just happens to match the signature
  130. using MemberFunction = std::function<bool(T& out, const Json::Value* value,
  131. cmJSONState* state)>;
  132. struct Member
  133. {
  134. cm::string_view Name;
  135. MemberFunction Function;
  136. bool Required;
  137. };
  138. std::vector<Member> Members;
  139. bool AnyRequired = false;
  140. JsonErrors::ObjectErrorGenerator Error;
  141. bool AllowExtra;
  142. Object& BindPrivate(const cm::string_view& name, MemberFunction&& func,
  143. bool required)
  144. {
  145. Member m;
  146. m.Name = name;
  147. m.Function = std::move(func);
  148. m.Required = required;
  149. this->Members.push_back(std::move(m));
  150. if (required) {
  151. this->AnyRequired = true;
  152. }
  153. return *this;
  154. }
  155. };
  156. static cmJSONHelper<std::string> String(
  157. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_STRING,
  158. const std::string& defval = "")
  159. {
  160. return [error, defval](std::string& out, const Json::Value* value,
  161. cmJSONState* state) -> bool {
  162. if (!value) {
  163. out = defval;
  164. return true;
  165. }
  166. if (!value->isString()) {
  167. error(value, state);
  168. ;
  169. return false;
  170. }
  171. out = value->asString();
  172. return true;
  173. };
  174. };
  175. static cmJSONHelper<std::string> String(const std::string& defval)
  176. {
  177. return String(JsonErrors::INVALID_STRING, defval);
  178. };
  179. static cmJSONHelper<int> Int(
  180. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_INT,
  181. int defval = 0)
  182. {
  183. return [error, defval](int& out, const Json::Value* value,
  184. cmJSONState* state) -> bool {
  185. if (!value) {
  186. out = defval;
  187. return true;
  188. }
  189. if (!value->isInt()) {
  190. error(value, state);
  191. ;
  192. return false;
  193. }
  194. out = value->asInt();
  195. return true;
  196. };
  197. }
  198. static cmJSONHelper<int> Int(int defval)
  199. {
  200. return Int(JsonErrors::INVALID_INT, defval);
  201. };
  202. static cmJSONHelper<unsigned int> UInt(
  203. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_UINT,
  204. unsigned int defval = 0)
  205. {
  206. return [error, defval](unsigned int& out, const Json::Value* value,
  207. cmJSONState* state) -> bool {
  208. if (!value) {
  209. out = defval;
  210. return true;
  211. }
  212. if (!value->isUInt()) {
  213. error(value, state);
  214. ;
  215. return false;
  216. }
  217. out = value->asUInt();
  218. return true;
  219. };
  220. }
  221. static cmJSONHelper<unsigned int> UInt(unsigned int defval)
  222. {
  223. return UInt(JsonErrors::INVALID_UINT, defval);
  224. }
  225. static cmJSONHelper<bool> Bool(
  226. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_BOOL,
  227. bool defval = false)
  228. {
  229. return [error, defval](bool& out, const Json::Value* value,
  230. cmJSONState* state) -> bool {
  231. if (!value) {
  232. out = defval;
  233. return true;
  234. }
  235. if (!value->isBool()) {
  236. error(value, state);
  237. ;
  238. return false;
  239. }
  240. out = value->asBool();
  241. return true;
  242. };
  243. }
  244. static cmJSONHelper<bool> Bool(bool defval)
  245. {
  246. return Bool(JsonErrors::INVALID_BOOL, defval);
  247. }
  248. template <typename T, typename F, typename Filter>
  249. static cmJSONHelper<std::vector<T>> VectorFilter(
  250. const JsonErrors::ErrorGenerator& error, F func, Filter filter)
  251. {
  252. return [error, func, filter](std::vector<T>& out, const Json::Value* value,
  253. cmJSONState* state) -> bool {
  254. bool success = true;
  255. if (!value) {
  256. out.clear();
  257. return true;
  258. }
  259. if (!value->isArray()) {
  260. error(value, state);
  261. return false;
  262. }
  263. out.clear();
  264. int index = 0;
  265. for (auto const& item : *value) {
  266. state->push_stack(cmStrCat("$vector_item_", index++), &item);
  267. T t;
  268. if (!func(t, &item, state)) {
  269. success = false;
  270. }
  271. if (!filter(t)) {
  272. state->pop_stack();
  273. continue;
  274. }
  275. out.push_back(std::move(t));
  276. state->pop_stack();
  277. }
  278. return success;
  279. };
  280. }
  281. template <typename T, typename F>
  282. static cmJSONHelper<std::vector<T>> Vector(JsonErrors::ErrorGenerator error,
  283. F func)
  284. {
  285. return VectorFilter<T, F>(std::move(error), func,
  286. [](const T&) { return true; });
  287. }
  288. template <typename T, typename F, typename Filter>
  289. static cmJSONHelper<std::map<std::string, T>> MapFilter(
  290. const JsonErrors::ErrorGenerator& error, F func, Filter filter)
  291. {
  292. return [error, func, filter](std::map<std::string, T>& out,
  293. const Json::Value* value,
  294. cmJSONState* state) -> bool {
  295. bool success = true;
  296. if (!value) {
  297. out.clear();
  298. return true;
  299. }
  300. if (!value->isObject()) {
  301. error(value, state);
  302. ;
  303. return false;
  304. }
  305. out.clear();
  306. for (auto const& key : value->getMemberNames()) {
  307. state->push_stack(cmStrCat(key, ""), &(*value)[key]);
  308. if (!filter(key)) {
  309. state->pop_stack();
  310. continue;
  311. }
  312. T t;
  313. if (!func(t, &(*value)[key], state)) {
  314. success = false;
  315. }
  316. out[key] = std::move(t);
  317. state->pop_stack();
  318. }
  319. return success;
  320. };
  321. }
  322. template <typename T, typename F>
  323. static cmJSONHelper<std::map<std::string, T>> Map(
  324. const JsonErrors::ErrorGenerator& error, F func)
  325. {
  326. return MapFilter<T, F>(error, func,
  327. [](const std::string&) { return true; });
  328. }
  329. template <typename T, typename F>
  330. static cmJSONHelper<cm::optional<T>> Optional(F func)
  331. {
  332. return [func](cm::optional<T>& out, const Json::Value* value,
  333. cmJSONState* state) -> bool {
  334. if (!value) {
  335. out.reset();
  336. return true;
  337. }
  338. out.emplace();
  339. return func(*out, value, state);
  340. };
  341. }
  342. template <typename T, typename F>
  343. static cmJSONHelper<T> Required(const JsonErrors::ErrorGenerator& error,
  344. F func)
  345. {
  346. return [error, func](T& out, const Json::Value* value,
  347. cmJSONState* state) -> bool {
  348. if (!value) {
  349. error(value, state);
  350. ;
  351. return false;
  352. }
  353. return func(out, value, state);
  354. };
  355. }
  356. };