Switch some page set from user_agent fields to coresponding shared_state classes
[chromium-blink-merge.git] / tools / json_schema_compiler / util.cc
blobb7c4eccae1441b2bf585c9ad9f567440bf776988
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 #include "tools/json_schema_compiler/util.h"
7 #include "base/stl_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
11 namespace json_schema_compiler {
12 namespace util {
14 bool PopulateItem(const base::Value& from, int* out) {
15 return from.GetAsInteger(out);
18 bool PopulateItem(const base::Value& from, int* out, base::string16* error) {
19 if (!from.GetAsInteger(out)) {
20 if (error->length()) {
21 error->append(base::UTF8ToUTF16("; "));
23 error->append(base::UTF8ToUTF16("expected integer, got " +
24 ValueTypeToString(from.GetType())));
25 return false;
27 return true;
30 bool PopulateItem(const base::Value& from, bool* out) {
31 return from.GetAsBoolean(out);
34 bool PopulateItem(const base::Value& from, bool* out, base::string16* error) {
35 if (!from.GetAsBoolean(out)) {
36 if (error->length()) {
37 error->append(base::UTF8ToUTF16("; "));
39 error->append(base::UTF8ToUTF16("expected boolean, got " +
40 ValueTypeToString(from.GetType())));
41 return false;
43 return true;
46 bool PopulateItem(const base::Value& from, double* out) {
47 return from.GetAsDouble(out);
50 bool PopulateItem(const base::Value& from, double* out, base::string16* error) {
51 if (!from.GetAsDouble(out)) {
52 if (error->length()) {
53 error->append(base::UTF8ToUTF16("; "));
55 error->append(base::UTF8ToUTF16("expected double, got " +
56 ValueTypeToString(from.GetType())));
57 return false;
59 return true;
62 bool PopulateItem(const base::Value& from, std::string* out) {
63 return from.GetAsString(out);
66 bool PopulateItem(const base::Value& from,
67 std::string* out,
68 base::string16* error) {
69 if (!from.GetAsString(out)) {
70 if (error->length()) {
71 error->append(base::UTF8ToUTF16("; "));
73 error->append(base::UTF8ToUTF16("expected string, got " +
74 ValueTypeToString(from.GetType())));
75 return false;
77 return true;
80 bool PopulateItem(const base::Value& from, std::vector<char>* out) {
81 const base::BinaryValue* binary = nullptr;
82 if (!from.GetAsBinary(&binary))
83 return false;
84 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize());
85 return true;
88 bool PopulateItem(const base::Value& from,
89 std::vector<char>* out,
90 base::string16* error) {
91 const base::BinaryValue* binary = nullptr;
92 if (!from.GetAsBinary(&binary)) {
93 if (error->length()) {
94 error->append(base::UTF8ToUTF16("; "));
96 error->append(base::UTF8ToUTF16("expected binary, got " +
97 ValueTypeToString(from.GetType())));
98 return false;
100 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize());
101 return true;
104 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out) {
105 *out = make_linked_ptr(from.DeepCopy());
106 return true;
109 bool PopulateItem(const base::Value& from,
110 linked_ptr<base::Value>* out,
111 base::string16* error) {
112 *out = make_linked_ptr(from.DeepCopy());
113 return true;
116 bool PopulateItem(const base::Value& from,
117 linked_ptr<base::DictionaryValue>* out) {
118 const base::DictionaryValue* dict = nullptr;
119 if (!from.GetAsDictionary(&dict))
120 return false;
121 *out = make_linked_ptr(dict->DeepCopy());
122 return true;
125 bool PopulateItem(const base::Value& from,
126 linked_ptr<base::DictionaryValue>* out,
127 base::string16* error) {
128 const base::DictionaryValue* dict = nullptr;
129 if (!from.GetAsDictionary(&dict)) {
130 if (error->length()) {
131 error->append(base::UTF8ToUTF16("; "));
133 error->append(base::UTF8ToUTF16("expected dictionary, got " +
134 ValueTypeToString(from.GetType())));
135 return false;
137 *out = make_linked_ptr(dict->DeepCopy());
138 return true;
141 void AddItemToList(const int from, base::ListValue* out) {
142 out->Append(new base::FundamentalValue(from));
145 void AddItemToList(const bool from, base::ListValue* out) {
146 out->Append(new base::FundamentalValue(from));
149 void AddItemToList(const double from, base::ListValue* out) {
150 out->Append(new base::FundamentalValue(from));
153 void AddItemToList(const std::string& from, base::ListValue* out) {
154 out->Append(new base::StringValue(from));
157 void AddItemToList(const std::vector<char>& from, base::ListValue* out) {
158 out->Append(base::BinaryValue::CreateWithCopiedBuffer(vector_as_array(&from),
159 from.size()));
162 void AddItemToList(const linked_ptr<base::Value>& from, base::ListValue* out) {
163 out->Append(from->DeepCopy());
166 void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
167 base::ListValue* out) {
168 out->Append(static_cast<base::Value*>(from->DeepCopy()));
171 std::string ValueTypeToString(base::Value::Type type) {
172 switch (type) {
173 case base::Value::TYPE_NULL:
174 return "null";
175 case base::Value::TYPE_BOOLEAN:
176 return "boolean";
177 case base::Value::TYPE_INTEGER:
178 return "integer";
179 case base::Value::TYPE_DOUBLE:
180 return "number";
181 case base::Value::TYPE_STRING:
182 return "string";
183 case base::Value::TYPE_BINARY:
184 return "binary";
185 case base::Value::TYPE_DICTIONARY:
186 return "dictionary";
187 case base::Value::TYPE_LIST:
188 return "list";
190 NOTREACHED();
191 return "";
194 } // namespace util
195 } // namespace json_schema_compiler