Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: tools/json_schema_compiler/util.cc

Issue 1128523003: Add support for passing errors through PopulateArrayFromList. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated JSON schema tests. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/json_schema_compiler/util.h ('k') | tools/json_schema_compiler/util_cc_helper.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "tools/json_schema_compiler/util.h" 5 #include "tools/json_schema_compiler/util.h"
6 6
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "base/strings/utf_string_conversions.h"
8 #include "base/values.h" 9 #include "base/values.h"
9 10
10 namespace json_schema_compiler { 11 namespace json_schema_compiler {
11 namespace util { 12 namespace util {
12 13
13 bool PopulateItem(const base::Value& from, int* out) { 14 bool PopulateItem(const base::Value& from, int* out) {
14 return from.GetAsInteger(out); 15 return from.GetAsInteger(out);
15 } 16 }
16 17
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("; "));
22 }
23 error->append(base::UTF8ToUTF16("expected integer, got " +
24 ValueTypeToString(from.GetType())));
25 return false;
26 }
27 return true;
28 }
29
17 bool PopulateItem(const base::Value& from, bool* out) { 30 bool PopulateItem(const base::Value& from, bool* out) {
18 return from.GetAsBoolean(out); 31 return from.GetAsBoolean(out);
19 } 32 }
20 33
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("; "));
38 }
39 error->append(base::UTF8ToUTF16("expected boolean, got " +
40 ValueTypeToString(from.GetType())));
41 return false;
42 }
43 return true;
44 }
45
21 bool PopulateItem(const base::Value& from, double* out) { 46 bool PopulateItem(const base::Value& from, double* out) {
22 return from.GetAsDouble(out); 47 return from.GetAsDouble(out);
23 } 48 }
24 49
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("; "));
54 }
55 error->append(base::UTF8ToUTF16("expected double, got " +
56 ValueTypeToString(from.GetType())));
57 return false;
58 }
59 return true;
60 }
61
25 bool PopulateItem(const base::Value& from, std::string* out) { 62 bool PopulateItem(const base::Value& from, std::string* out) {
26 return from.GetAsString(out); 63 return from.GetAsString(out);
27 } 64 }
28 65
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("; "));
72 }
73 error->append(base::UTF8ToUTF16("expected string, got " +
74 ValueTypeToString(from.GetType())));
75 return false;
76 }
77 return true;
78 }
79
29 bool PopulateItem(const base::Value& from, std::vector<char>* out) { 80 bool PopulateItem(const base::Value& from, std::vector<char>* out) {
30 const base::BinaryValue* binary = nullptr; 81 const base::BinaryValue* binary = nullptr;
31 if (!from.GetAsBinary(&binary)) 82 if (!from.GetAsBinary(&binary))
32 return false; 83 return false;
33 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); 84 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize());
34 return true; 85 return true;
35 } 86 }
36 87
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("; "));
95 }
96 error->append(base::UTF8ToUTF16("expected binary, got " +
97 ValueTypeToString(from.GetType())));
98 return false;
99 }
100 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize());
101 return true;
102 }
103
37 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out) { 104 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out) {
38 *out = make_linked_ptr(from.DeepCopy()); 105 *out = make_linked_ptr(from.DeepCopy());
39 return true; 106 return true;
40 } 107 }
41 108
42 bool PopulateItem(const base::Value& from, 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;
114 }
115
116 bool PopulateItem(const base::Value& from,
43 linked_ptr<base::DictionaryValue>* out) { 117 linked_ptr<base::DictionaryValue>* out) {
44 const base::DictionaryValue* dict = nullptr; 118 const base::DictionaryValue* dict = nullptr;
45 if (!from.GetAsDictionary(&dict)) 119 if (!from.GetAsDictionary(&dict))
46 return false; 120 return false;
47 *out = make_linked_ptr(dict->DeepCopy()); 121 *out = make_linked_ptr(dict->DeepCopy());
48 return true; 122 return true;
49 } 123 }
50 124
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("; "));
132 }
133 error->append(base::UTF8ToUTF16("expected dictionary, got " +
134 ValueTypeToString(from.GetType())));
135 return false;
136 }
137 *out = make_linked_ptr(dict->DeepCopy());
138 return true;
139 }
140
51 void AddItemToList(const int from, base::ListValue* out) { 141 void AddItemToList(const int from, base::ListValue* out) {
52 out->Append(new base::FundamentalValue(from)); 142 out->Append(new base::FundamentalValue(from));
53 } 143 }
54 144
55 void AddItemToList(const bool from, base::ListValue* out) { 145 void AddItemToList(const bool from, base::ListValue* out) {
56 out->Append(new base::FundamentalValue(from)); 146 out->Append(new base::FundamentalValue(from));
57 } 147 }
58 148
59 void AddItemToList(const double from, base::ListValue* out) { 149 void AddItemToList(const double from, base::ListValue* out) {
60 out->Append(new base::FundamentalValue(from)); 150 out->Append(new base::FundamentalValue(from));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 return "dictionary"; 186 return "dictionary";
97 case base::Value::TYPE_LIST: 187 case base::Value::TYPE_LIST:
98 return "list"; 188 return "list";
99 } 189 }
100 NOTREACHED(); 190 NOTREACHED();
101 return ""; 191 return "";
102 } 192 }
103 193
104 } // namespace util 194 } // namespace util
105 } // namespace json_schema_compiler 195 } // namespace json_schema_compiler
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/util.h ('k') | tools/json_schema_compiler/util_cc_helper.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698