cmJSONHelpers.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. if (state->allowComments) {
  103. extraFields.erase(
  104. std::remove(extraFields.begin(), extraFields.end(), "$comment"),
  105. extraFields.end());
  106. }
  107. for (auto const& m : this->Members) {
  108. std::string name(m.Name.data(), m.Name.size());
  109. state->push_stack(name, value);
  110. if (value && value->isMember(name)) {
  111. if (!m.Function(out, &(*value)[name], state)) {
  112. success = false;
  113. }
  114. extraFields.erase(
  115. std::find(extraFields.begin(), extraFields.end(), name));
  116. } else if (!m.Required) {
  117. if (!m.Function(out, nullptr, state)) {
  118. success = false;
  119. }
  120. } else {
  121. Error(JsonErrors::ObjectError::MissingRequired, extraFields)(value,
  122. state);
  123. success = false;
  124. }
  125. state->pop_stack();
  126. }
  127. if (!this->AllowExtra && !extraFields.empty()) {
  128. Error(JsonErrors::ObjectError::ExtraField, extraFields)(value, state);
  129. success = false;
  130. }
  131. return success;
  132. }
  133. private:
  134. // Not a true cmJSONHelper, it just happens to match the signature
  135. using MemberFunction = std::function<bool(T& out, const Json::Value* value,
  136. cmJSONState* state)>;
  137. struct Member
  138. {
  139. cm::string_view Name;
  140. MemberFunction Function;
  141. bool Required;
  142. };
  143. std::vector<Member> Members;
  144. bool AnyRequired = false;
  145. JsonErrors::ObjectErrorGenerator Error;
  146. bool AllowExtra;
  147. Object& BindPrivate(const cm::string_view& name, MemberFunction&& func,
  148. bool required)
  149. {
  150. Member m;
  151. m.Name = name;
  152. m.Function = std::move(func);
  153. m.Required = required;
  154. this->Members.push_back(std::move(m));
  155. if (required) {
  156. this->AnyRequired = true;
  157. }
  158. return *this;
  159. }
  160. };
  161. static cmJSONHelper<std::string> String(
  162. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_STRING,
  163. const std::string& defval = "")
  164. {
  165. return [error, defval](std::string& out, const Json::Value* value,
  166. cmJSONState* state) -> bool {
  167. if (!value) {
  168. out = defval;
  169. return true;
  170. }
  171. if (!value->isString()) {
  172. error(value, state);
  173. ;
  174. return false;
  175. }
  176. out = value->asString();
  177. return true;
  178. };
  179. };
  180. static cmJSONHelper<std::string> String(const std::string& defval)
  181. {
  182. return String(JsonErrors::INVALID_STRING, defval);
  183. };
  184. static cmJSONHelper<int> Int(
  185. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_INT,
  186. int defval = 0)
  187. {
  188. return [error, defval](int& out, const Json::Value* value,
  189. cmJSONState* state) -> bool {
  190. if (!value) {
  191. out = defval;
  192. return true;
  193. }
  194. if (!value->isInt()) {
  195. error(value, state);
  196. ;
  197. return false;
  198. }
  199. out = value->asInt();
  200. return true;
  201. };
  202. }
  203. static cmJSONHelper<int> Int(int defval)
  204. {
  205. return Int(JsonErrors::INVALID_INT, defval);
  206. };
  207. static cmJSONHelper<unsigned int> UInt(
  208. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_UINT,
  209. unsigned int defval = 0)
  210. {
  211. return [error, defval](unsigned int& out, const Json::Value* value,
  212. cmJSONState* state) -> bool {
  213. if (!value) {
  214. out = defval;
  215. return true;
  216. }
  217. if (!value->isUInt()) {
  218. error(value, state);
  219. ;
  220. return false;
  221. }
  222. out = value->asUInt();
  223. return true;
  224. };
  225. }
  226. static cmJSONHelper<unsigned int> UInt(unsigned int defval)
  227. {
  228. return UInt(JsonErrors::INVALID_UINT, defval);
  229. }
  230. static cmJSONHelper<bool> Bool(
  231. const JsonErrors::ErrorGenerator& error = JsonErrors::INVALID_BOOL,
  232. bool defval = false)
  233. {
  234. return [error, defval](bool& out, const Json::Value* value,
  235. cmJSONState* state) -> bool {
  236. if (!value) {
  237. out = defval;
  238. return true;
  239. }
  240. if (!value->isBool()) {
  241. error(value, state);
  242. ;
  243. return false;
  244. }
  245. out = value->asBool();
  246. return true;
  247. };
  248. }
  249. static cmJSONHelper<bool> Bool(bool defval)
  250. {
  251. return Bool(JsonErrors::INVALID_BOOL, defval);
  252. }
  253. template <typename T, typename F, typename Filter>
  254. static cmJSONHelper<std::vector<T>> VectorFilter(
  255. const JsonErrors::ErrorGenerator& error, F func, Filter filter)
  256. {
  257. return [error, func, filter](std::vector<T>& out, const Json::Value* value,
  258. cmJSONState* state) -> bool {
  259. bool success = true;
  260. if (!value) {
  261. out.clear();
  262. return true;
  263. }
  264. if (!value->isArray()) {
  265. error(value, state);
  266. return false;
  267. }
  268. out.clear();
  269. int index = 0;
  270. for (auto const& item : *value) {
  271. state->push_stack(cmStrCat("$vector_item_", index++), &item);
  272. T t;
  273. if (!func(t, &item, state)) {
  274. success = false;
  275. }
  276. if (!filter(t)) {
  277. state->pop_stack();
  278. continue;
  279. }
  280. out.push_back(std::move(t));
  281. state->pop_stack();
  282. }
  283. return success;
  284. };
  285. }
  286. template <typename T, typename F>
  287. static cmJSONHelper<std::vector<T>> Vector(JsonErrors::ErrorGenerator error,
  288. F func)
  289. {
  290. return VectorFilter<T, F>(std::move(error), func,
  291. [](const T&) { return true; });
  292. }
  293. template <typename T, typename F, typename Filter>
  294. static cmJSONHelper<std::map<std::string, T>> MapFilter(
  295. const JsonErrors::ErrorGenerator& error, F func, Filter filter)
  296. {
  297. return [error, func, filter](std::map<std::string, T>& out,
  298. const Json::Value* value,
  299. cmJSONState* state) -> bool {
  300. bool success = true;
  301. if (!value) {
  302. out.clear();
  303. return true;
  304. }
  305. if (!value->isObject()) {
  306. error(value, state);
  307. ;
  308. return false;
  309. }
  310. out.clear();
  311. for (auto const& key : value->getMemberNames()) {
  312. state->push_stack(cmStrCat(key, ""), &(*value)[key]);
  313. if (!filter(key)) {
  314. state->pop_stack();
  315. continue;
  316. }
  317. T t;
  318. if (!func(t, &(*value)[key], state)) {
  319. success = false;
  320. }
  321. out[key] = std::move(t);
  322. state->pop_stack();
  323. }
  324. return success;
  325. };
  326. }
  327. template <typename T, typename F>
  328. static cmJSONHelper<std::map<std::string, T>> Map(
  329. const JsonErrors::ErrorGenerator& error, F func)
  330. {
  331. return MapFilter<T, F>(error, func,
  332. [](const std::string&) { return true; });
  333. }
  334. template <typename T, typename F>
  335. static cmJSONHelper<cm::optional<T>> Optional(F func)
  336. {
  337. return [func](cm::optional<T>& out, const Json::Value* value,
  338. cmJSONState* state) -> bool {
  339. if (!value) {
  340. out.reset();
  341. return true;
  342. }
  343. out.emplace();
  344. return func(*out, value, state);
  345. };
  346. }
  347. template <typename T, typename F>
  348. static cmJSONHelper<T> Required(const JsonErrors::ErrorGenerator& error,
  349. F func)
  350. {
  351. return [error, func](T& out, const Json::Value* value,
  352. cmJSONState* state) -> bool {
  353. if (!value) {
  354. error(value, state);
  355. ;
  356. return false;
  357. }
  358. return func(out, value, state);
  359. };
  360. }
  361. };