util.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
  5. #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/values.h"
  11. namespace json_schema_compiler {
  12. namespace util {
  13. // Populates the item |out| from the value |from|. These are used by template
  14. // specializations of |Get(Optional)ArrayFromList|.
  15. bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
  16. bool PopulateItem(const base::Value& from, int* out);
  17. bool PopulateItem(const base::Value& from, int* out, base::string16* error);
  18. bool PopulateItem(const base::Value& from, bool* out);
  19. bool PopulateItem(const base::Value& from, bool* out, base::string16* error);
  20. bool PopulateItem(const base::Value& from, double* out);
  21. bool PopulateItem(const base::Value& from, double* out, base::string16* error);
  22. bool PopulateItem(const base::Value& from, std::string* out);
  23. bool PopulateItem(const base::Value& from,
  24. std::string* out,
  25. base::string16* error);
  26. bool PopulateItem(const base::Value& from, std::vector<uint8_t>* out);
  27. bool PopulateItem(const base::Value& from,
  28. std::vector<uint8_t>* out,
  29. base::string16* error);
  30. bool PopulateItem(const base::Value& from,
  31. std::unique_ptr<base::Value>* out,
  32. base::string16* error);
  33. bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
  34. // This template is used for types generated by tools/json_schema_compiler.
  35. template <class T>
  36. bool PopulateItem(const base::Value& from, std::unique_ptr<T>* out) {
  37. const base::DictionaryValue* dict = nullptr;
  38. if (!from.GetAsDictionary(&dict))
  39. return false;
  40. std::unique_ptr<T> obj(new T());
  41. if (!T::Populate(*dict, obj.get()))
  42. return false;
  43. *out = std::move(obj);
  44. return true;
  45. }
  46. // This template is used for types generated by tools/json_schema_compiler.
  47. template <class T>
  48. bool PopulateItem(const base::Value& from, T* out) {
  49. const base::DictionaryValue* dict = nullptr;
  50. if (!from.GetAsDictionary(&dict))
  51. return false;
  52. T obj;
  53. if (!T::Populate(*dict, &obj))
  54. return false;
  55. *out = std::move(obj);
  56. return true;
  57. }
  58. // This template is used for types generated by tools/json_schema_compiler with
  59. // error generation enabled.
  60. template <class T>
  61. bool PopulateItem(const base::Value& from,
  62. std::unique_ptr<T>* out,
  63. base::string16* error) {
  64. const base::DictionaryValue* dict = nullptr;
  65. if (!from.GetAsDictionary(&dict))
  66. return false;
  67. std::unique_ptr<T> obj(new T());
  68. if (!T::Populate(*dict, obj.get(), error))
  69. return false;
  70. *out = std::move(obj);
  71. return true;
  72. }
  73. // This template is used for types generated by tools/json_schema_compiler with
  74. // error generation enabled.
  75. template <class T>
  76. bool PopulateItem(const base::Value& from, T* out, base::string16* error) {
  77. const base::DictionaryValue* dict = nullptr;
  78. if (!from.GetAsDictionary(&dict))
  79. return false;
  80. T obj;
  81. if (!T::Populate(*dict, &obj, error))
  82. return false;
  83. *out = std::move(obj);
  84. return true;
  85. }
  86. // Populates |out| with |list|. Returns false if there is no list at the
  87. // specified key or if the list has anything other than |T|.
  88. template <class T>
  89. bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
  90. out->clear();
  91. T item;
  92. for (const auto& value : list) {
  93. if (!PopulateItem(value, &item))
  94. return false;
  95. // T might not be movable, but in that case it should be copyable, and this
  96. // will still work.
  97. out->push_back(std::move(item));
  98. }
  99. return true;
  100. }
  101. // Populates |out| with |list|. Returns false and sets |error| if there is no
  102. // list at the specified key or if the list has anything other than |T|.
  103. template <class T>
  104. bool PopulateArrayFromList(const base::ListValue& list,
  105. std::vector<T>* out,
  106. base::string16* error) {
  107. out->clear();
  108. T item;
  109. for (const auto& value : list) {
  110. if (!PopulateItem(value, &item, error))
  111. return false;
  112. out->push_back(std::move(item));
  113. }
  114. return true;
  115. }
  116. // Creates a new vector containing |list| at |out|. Returns
  117. // true on success or if there is nothing at the specified key. Returns false
  118. // if anything other than a list of |T| is at the specified key.
  119. template <class T>
  120. bool PopulateOptionalArrayFromList(const base::ListValue& list,
  121. std::unique_ptr<std::vector<T>>* out) {
  122. out->reset(new std::vector<T>());
  123. if (!PopulateArrayFromList(list, out->get())) {
  124. out->reset();
  125. return false;
  126. }
  127. return true;
  128. }
  129. template <class T>
  130. bool PopulateOptionalArrayFromList(const base::ListValue& list,
  131. std::unique_ptr<std::vector<T>>* out,
  132. base::string16* error) {
  133. out->reset(new std::vector<T>());
  134. if (!PopulateArrayFromList(list, out->get(), error)) {
  135. out->reset();
  136. return false;
  137. }
  138. return true;
  139. }
  140. // Appends a Value newly created from |from| to |out|. These used by template
  141. // specializations of |Set(Optional)ArrayToList|.
  142. void AddItemToList(const int from, base::ListValue* out);
  143. void AddItemToList(const bool from, base::ListValue* out);
  144. void AddItemToList(const double from, base::ListValue* out);
  145. void AddItemToList(const std::string& from, base::ListValue* out);
  146. void AddItemToList(const std::vector<uint8_t>& from, base::ListValue* out);
  147. void AddItemToList(const std::unique_ptr<base::Value>& from,
  148. base::ListValue* out);
  149. void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from,
  150. base::ListValue* out);
  151. // This template is used for types generated by tools/json_schema_compiler.
  152. template <class T>
  153. void AddItemToList(const std::unique_ptr<T>& from, base::ListValue* out) {
  154. out->Append(from->ToValue());
  155. }
  156. // This template is used for types generated by tools/json_schema_compiler.
  157. template <class T>
  158. void AddItemToList(const T& from, base::ListValue* out) {
  159. out->Append(from.ToValue());
  160. }
  161. // Set |out| to the the contents of |from|. Requires PopulateItem to be
  162. // implemented for |T|.
  163. template <class T>
  164. void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
  165. out->Clear();
  166. for (const T& item : from)
  167. AddItemToList(item, out);
  168. }
  169. // Set |out| to the the contents of |from| if |from| is not null. Requires
  170. // PopulateItem to be implemented for |T|.
  171. template <class T>
  172. void PopulateListFromOptionalArray(const std::unique_ptr<std::vector<T>>& from,
  173. base::ListValue* out) {
  174. if (from)
  175. PopulateListFromArray(*from, out);
  176. }
  177. template <class T>
  178. std::unique_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
  179. std::unique_ptr<base::ListValue> list(new base::ListValue());
  180. PopulateListFromArray(from, list.get());
  181. return std::move(list);
  182. }
  183. template <class T>
  184. std::unique_ptr<base::Value> CreateValueFromOptionalArray(
  185. const std::unique_ptr<std::vector<T>>& from) {
  186. return from ? CreateValueFromArray(*from) : nullptr;
  187. }
  188. } // namespace util
  189. } // namespace json_schema_compiler
  190. #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_