| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| 6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 | 14 |
| 15 namespace json_schema_compiler { | 15 namespace json_schema_compiler { |
| 16 | 16 |
| 17 namespace util { | 17 namespace util { |
| 18 | 18 |
| 19 // Populates the item |out| from the value |from|. These are used by template | 19 // Populates the item |out| from the value |from|. These are used by template |
| 20 // specializations of |Get(Optional)ArrayFromList|. | 20 // specializations of |Get(Optional)ArrayFromList|. |
| 21 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); |
| 22 |
| 21 bool PopulateItem(const base::Value& from, int* out); | 23 bool PopulateItem(const base::Value& from, int* out); |
| 24 bool PopulateItem(const base::Value& from, int* out, base::string16* error); |
| 22 bool PopulateItem(const base::Value& from, bool* out); | 25 bool PopulateItem(const base::Value& from, bool* out); |
| 26 bool PopulateItem(const base::Value& from, bool* out, base::string16* error); |
| 23 bool PopulateItem(const base::Value& from, double* out); | 27 bool PopulateItem(const base::Value& from, double* out); |
| 28 bool PopulateItem(const base::Value& from, double* out, base::string16* error); |
| 24 bool PopulateItem(const base::Value& from, std::string* out); | 29 bool PopulateItem(const base::Value& from, std::string* out); |
| 30 bool PopulateItem(const base::Value& from, |
| 31 std::string* out, |
| 32 base::string16* error); |
| 25 bool PopulateItem(const base::Value& from, std::vector<char>* out); | 33 bool PopulateItem(const base::Value& from, std::vector<char>* out); |
| 34 bool PopulateItem(const base::Value& from, |
| 35 std::vector<char>* out, |
| 36 base::string16* error); |
| 37 bool PopulateItem(const base::Value& from, |
| 38 linked_ptr<base::Value>* out, |
| 39 base::string16* error); |
| 26 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); | 40 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out); |
| 27 bool PopulateItem(const base::Value& from, | 41 bool PopulateItem(const base::Value& from, |
| 28 linked_ptr<base::DictionaryValue>* out); | 42 linked_ptr<base::DictionaryValue>* out); |
| 43 bool PopulateItem(const base::Value& from, |
| 44 linked_ptr<base::DictionaryValue>* out, |
| 45 base::string16* error); |
| 29 | 46 |
| 30 // This template is used for types generated by tools/json_schema_compiler. | 47 // This template is used for types generated by tools/json_schema_compiler. |
| 31 template <class T> | 48 template <class T> |
| 32 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) { | 49 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) { |
| 33 const base::DictionaryValue* dict = nullptr; | 50 const base::DictionaryValue* dict = nullptr; |
| 34 if (!from.GetAsDictionary(&dict)) | 51 if (!from.GetAsDictionary(&dict)) |
| 35 return false; | 52 return false; |
| 36 scoped_ptr<T> obj(new T()); | 53 scoped_ptr<T> obj(new T()); |
| 37 if (!T::Populate(*dict, obj.get())) | 54 if (!T::Populate(*dict, obj.get())) |
| 38 return false; | 55 return false; |
| 39 *out = linked_ptr<T>(obj.release()); | 56 *out = linked_ptr<T>(obj.release()); |
| 40 return true; | 57 return true; |
| 41 } | 58 } |
| 42 | 59 |
| 60 // This template is used for types generated by tools/json_schema_compiler with |
| 61 // error generation enabled. |
| 62 template <class T> |
| 63 bool PopulateItem(const base::Value& from, |
| 64 linked_ptr<T>* out, |
| 65 base::string16* error) { |
| 66 const base::DictionaryValue* dict = nullptr; |
| 67 if (!from.GetAsDictionary(&dict)) |
| 68 return false; |
| 69 scoped_ptr<T> obj(new T()); |
| 70 if (!T::Populate(*dict, obj.get(), error)) |
| 71 return false; |
| 72 *out = linked_ptr<T>(obj.release()); |
| 73 return true; |
| 74 } |
| 75 |
| 43 // Populates |out| with |list|. Returns false if there is no list at the | 76 // Populates |out| with |list|. Returns false if there is no list at the |
| 44 // specified key or if the list has anything other than |T|. | 77 // specified key or if the list has anything other than |T|. |
| 45 template <class T> | 78 template <class T> |
| 46 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { | 79 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) { |
| 47 out->clear(); | 80 out->clear(); |
| 48 T item; | 81 T item; |
| 49 for (const base::Value* value : list) { | 82 for (const base::Value* value : list) { |
| 50 if (!PopulateItem(*value, &item)) | 83 if (!PopulateItem(*value, &item)) |
| 51 return false; | 84 return false; |
| 52 out->push_back(item); | 85 out->push_back(item); |
| 53 } | 86 } |
| 54 | 87 |
| 55 return true; | 88 return true; |
| 56 } | 89 } |
| 57 | 90 |
| 91 // Populates |out| with |list|. Returns false and sets |error| if there is no |
| 92 // list at the specified key or if the list has anything other than |T|. |
| 93 template <class T> |
| 94 bool PopulateArrayFromList(const base::ListValue& list, |
| 95 std::vector<T>* out, |
| 96 base::string16* error) { |
| 97 out->clear(); |
| 98 T item; |
| 99 for (const base::Value* value : list) { |
| 100 if (!PopulateItem(*value, &item, error)) |
| 101 return false; |
| 102 out->push_back(item); |
| 103 } |
| 104 |
| 105 return true; |
| 106 } |
| 107 |
| 58 // Creates a new vector containing |list| at |out|. Returns | 108 // Creates a new vector containing |list| at |out|. Returns |
| 59 // true on success or if there is nothing at the specified key. Returns false | 109 // true on success or if there is nothing at the specified key. Returns false |
| 60 // if anything other than a list of |T| is at the specified key. | 110 // if anything other than a list of |T| is at the specified key. |
| 61 template <class T> | 111 template <class T> |
| 62 bool PopulateOptionalArrayFromList(const base::ListValue& list, | 112 bool PopulateOptionalArrayFromList(const base::ListValue& list, |
| 63 scoped_ptr<std::vector<T>>* out) { | 113 scoped_ptr<std::vector<T>>* out) { |
| 64 out->reset(new std::vector<T>()); | 114 out->reset(new std::vector<T>()); |
| 65 if (!PopulateArrayFromList(list, out->get())) { | 115 if (!PopulateArrayFromList(list, out->get())) { |
| 66 out->reset(); | 116 out->reset(); |
| 67 return false; | 117 return false; |
| 68 } | 118 } |
| 69 return true; | 119 return true; |
| 70 } | 120 } |
| 71 | 121 |
| 122 template <class T> |
| 123 bool PopulateOptionalArrayFromList(const base::ListValue& list, |
| 124 scoped_ptr<std::vector<T>>* out, |
| 125 base::string16* error) { |
| 126 out->reset(new std::vector<T>()); |
| 127 if (!PopulateArrayFromList(list, out->get(), error)) { |
| 128 out->reset(); |
| 129 return false; |
| 130 } |
| 131 return true; |
| 132 } |
| 133 |
| 72 // Appends a Value newly created from |from| to |out|. These used by template | 134 // Appends a Value newly created from |from| to |out|. These used by template |
| 73 // specializations of |Set(Optional)ArrayToList|. | 135 // specializations of |Set(Optional)ArrayToList|. |
| 74 void AddItemToList(const int from, base::ListValue* out); | 136 void AddItemToList(const int from, base::ListValue* out); |
| 75 void AddItemToList(const bool from, base::ListValue* out); | 137 void AddItemToList(const bool from, base::ListValue* out); |
| 76 void AddItemToList(const double from, base::ListValue* out); | 138 void AddItemToList(const double from, base::ListValue* out); |
| 77 void AddItemToList(const std::string& from, base::ListValue* out); | 139 void AddItemToList(const std::string& from, base::ListValue* out); |
| 78 void AddItemToList(const std::vector<char>& from, base::ListValue* out); | 140 void AddItemToList(const std::vector<char>& from, base::ListValue* out); |
| 79 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out); | 141 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out); |
| 80 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, | 142 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, |
| 81 base::ListValue* out); | 143 base::ListValue* out); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 return CreateValueFromArray(*from); | 180 return CreateValueFromArray(*from); |
| 119 return scoped_ptr<base::Value>(); | 181 return scoped_ptr<base::Value>(); |
| 120 } | 182 } |
| 121 | 183 |
| 122 std::string ValueTypeToString(base::Value::Type type); | 184 std::string ValueTypeToString(base::Value::Type type); |
| 123 | 185 |
| 124 } // namespace util | 186 } // namespace util |
| 125 } // namespace json_schema_compiler | 187 } // namespace json_schema_compiler |
| 126 | 188 |
| 127 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ | 189 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ |
| OLD | NEW |