util.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, T* out) {
  37. const base::DictionaryValue* dict = nullptr;
  38. if (!from.GetAsDictionary(&dict))
  39. return false;
  40. T obj;
  41. if (!T::Populate(*dict, &obj))
  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 with
  47. // error generation enabled.
  48. template <class T>
  49. bool PopulateItem(const base::Value& from, T* out, base::string16* error) {
  50. T obj;
  51. if (!T::Populate(from, &obj, error))
  52. return false;
  53. *out = std::move(obj);
  54. return true;
  55. }
  56. // Populates |out| with |list|. Returns false if there is no list at the
  57. // specified key or if the list has anything other than |T|.
  58. template <class T>
  59. bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
  60. out->clear();
  61. T item;
  62. for (const auto& value : list) {
  63. if (!PopulateItem(value, &item))
  64. return false;
  65. // T might not be movable, but in that case it should be copyable, and this
  66. // will still work.
  67. out->push_back(std::move(item));
  68. }
  69. return true;
  70. }
  71. // Populates |out| with |list|. Returns false and sets |error| if there is no
  72. // list at the specified key or if the list has anything other than |T|.
  73. template <class T>
  74. bool PopulateArrayFromList(const base::ListValue& list,
  75. std::vector<T>* out,
  76. base::string16* error) {
  77. out->clear();
  78. T item;
  79. for (const auto& value : list) {
  80. if (!PopulateItem(value, &item, error))
  81. return false;
  82. out->push_back(std::move(item));
  83. }
  84. return true;
  85. }
  86. // Creates a new vector containing |list| at |out|. Returns
  87. // true on success or if there is nothing at the specified key. Returns false
  88. // if anything other than a list of |T| is at the specified key.
  89. template <class T>
  90. bool PopulateOptionalArrayFromList(const base::ListValue& list,
  91. std::unique_ptr<std::vector<T>>* out) {
  92. out->reset(new std::vector<T>());
  93. if (!PopulateArrayFromList(list, out->get())) {
  94. out->reset();
  95. return false;
  96. }
  97. return true;
  98. }
  99. template <class T>
  100. bool PopulateOptionalArrayFromList(const base::ListValue& list,
  101. std::unique_ptr<std::vector<T>>* out,
  102. base::string16* error) {
  103. out->reset(new std::vector<T>());
  104. if (!PopulateArrayFromList(list, out->get(), error)) {
  105. out->reset();
  106. return false;
  107. }
  108. return true;
  109. }
  110. // Appends a Value newly created from |from| to |out|. These used by template
  111. // specializations of |Set(Optional)ArrayToList|.
  112. void AddItemToList(const int from, base::ListValue* out);
  113. void AddItemToList(const bool from, base::ListValue* out);
  114. void AddItemToList(const double from, base::ListValue* out);
  115. void AddItemToList(const std::string& from, base::ListValue* out);
  116. void AddItemToList(const std::vector<uint8_t>& from, base::ListValue* out);
  117. void AddItemToList(const std::unique_ptr<base::Value>& from,
  118. base::ListValue* out);
  119. void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from,
  120. base::ListValue* out);
  121. // This template is used for types generated by tools/json_schema_compiler.
  122. template <class T>
  123. void AddItemToList(const std::unique_ptr<T>& from, base::ListValue* out) {
  124. out->Append(from->ToValue());
  125. }
  126. // This template is used for types generated by tools/json_schema_compiler.
  127. template <class T>
  128. void AddItemToList(const T& from, base::ListValue* out) {
  129. out->Append(from.ToValue());
  130. }
  131. // Set |out| to the the contents of |from|. Requires PopulateItem to be
  132. // implemented for |T|.
  133. template <class T>
  134. void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
  135. out->Clear();
  136. for (const T& item : from)
  137. AddItemToList(item, out);
  138. }
  139. // Set |out| to the the contents of |from| if |from| is not null. Requires
  140. // PopulateItem to be implemented for |T|.
  141. template <class T>
  142. void PopulateListFromOptionalArray(const std::unique_ptr<std::vector<T>>& from,
  143. base::ListValue* out) {
  144. if (from)
  145. PopulateListFromArray(*from, out);
  146. }
  147. template <class T>
  148. std::unique_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
  149. std::unique_ptr<base::ListValue> list(new base::ListValue());
  150. PopulateListFromArray(from, list.get());
  151. return std::move(list);
  152. }
  153. template <class T>
  154. std::unique_ptr<base::Value> CreateValueFromOptionalArray(
  155. const std::unique_ptr<std::vector<T>>& from) {
  156. return from ? CreateValueFromArray(*from) : nullptr;
  157. }
  158. } // namespace util
  159. } // namespace json_schema_compiler
  160. #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_