Migrate SiteSettingsPreferencesTest from ChromeShell to ChromePublic.
[chromium-blink-merge.git] / tools / json_schema_compiler / util.h
blob59d65d01031d8ea9879da482e5bcc5fafc3180db
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.
5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
8 #include <string>
9 #include <vector>
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
15 namespace json_schema_compiler {
17 namespace util {
19 // Populates the item |out| from the value |from|. These are used by template
20 // specializations of |Get(Optional)ArrayFromList|.
21 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out);
23 bool PopulateItem(const base::Value& from, int* out);
24 bool PopulateItem(const base::Value& from, int* out, base::string16* error);
25 bool PopulateItem(const base::Value& from, bool* out);
26 bool PopulateItem(const base::Value& from, bool* out, base::string16* error);
27 bool PopulateItem(const base::Value& from, double* out);
28 bool PopulateItem(const base::Value& from, double* out, base::string16* error);
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);
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);
40 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out);
41 bool PopulateItem(const base::Value& from,
42 linked_ptr<base::DictionaryValue>* out);
43 bool PopulateItem(const base::Value& from,
44 linked_ptr<base::DictionaryValue>* out,
45 base::string16* error);
47 // This template is used for types generated by tools/json_schema_compiler.
48 template <class T>
49 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) {
50 const base::DictionaryValue* dict = nullptr;
51 if (!from.GetAsDictionary(&dict))
52 return false;
53 scoped_ptr<T> obj(new T());
54 if (!T::Populate(*dict, obj.get()))
55 return false;
56 *out = linked_ptr<T>(obj.release());
57 return true;
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;
76 // Populates |out| with |list|. Returns false if there is no list at the
77 // specified key or if the list has anything other than |T|.
78 template <class T>
79 bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
80 out->clear();
81 T item;
82 for (const base::Value* value : list) {
83 if (!PopulateItem(*value, &item))
84 return false;
85 out->push_back(item);
88 return true;
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);
105 return true;
108 // Creates a new vector containing |list| at |out|. Returns
109 // true on success or if there is nothing at the specified key. Returns false
110 // if anything other than a list of |T| is at the specified key.
111 template <class T>
112 bool PopulateOptionalArrayFromList(const base::ListValue& list,
113 scoped_ptr<std::vector<T>>* out) {
114 out->reset(new std::vector<T>());
115 if (!PopulateArrayFromList(list, out->get())) {
116 out->reset();
117 return false;
119 return true;
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;
131 return true;
134 // Appends a Value newly created from |from| to |out|. These used by template
135 // specializations of |Set(Optional)ArrayToList|.
136 void AddItemToList(const int from, base::ListValue* out);
137 void AddItemToList(const bool from, base::ListValue* out);
138 void AddItemToList(const double from, base::ListValue* out);
139 void AddItemToList(const std::string& from, base::ListValue* out);
140 void AddItemToList(const std::vector<char>& from, base::ListValue* out);
141 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out);
142 void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
143 base::ListValue* out);
145 // This template is used for types generated by tools/json_schema_compiler.
146 template <class T>
147 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) {
148 out->Append(from->ToValue().release());
151 // Set |out| to the the contents of |from|. Requires PopulateItem to be
152 // implemented for |T|.
153 template <class T>
154 void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
155 out->Clear();
156 for (const auto& item : from)
157 AddItemToList(item, out);
160 // Set |out| to the the contents of |from| if |from| is not null. Requires
161 // PopulateItem to be implemented for |T|.
162 template <class T>
163 void PopulateListFromOptionalArray(const scoped_ptr<std::vector<T>>& from,
164 base::ListValue* out) {
165 if (from.get())
166 PopulateListFromArray(*from, out);
169 template <class T>
170 scoped_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
171 base::ListValue* list = new base::ListValue();
172 PopulateListFromArray(from, list);
173 return scoped_ptr<base::Value>(list);
176 template <class T>
177 scoped_ptr<base::Value> CreateValueFromOptionalArray(
178 const scoped_ptr<std::vector<T>>& from) {
179 if (from.get())
180 return CreateValueFromArray(*from);
181 return scoped_ptr<base::Value>();
184 std::string ValueTypeToString(base::Value::Type type);
186 } // namespace util
187 } // namespace json_schema_compiler
189 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__