Chromium Code Reviews| Index: tools/json_schema_compiler/util.cc |
| diff --git a/tools/json_schema_compiler/util.cc b/tools/json_schema_compiler/util.cc |
| index a5e1668c1ff2aef628e005e8ab1e9d19962bd430..a15c9f514f242c697c1f921d01d4a6018d8a7f55 100644 |
| --- a/tools/json_schema_compiler/util.cc |
| +++ b/tools/json_schema_compiler/util.cc |
| @@ -4,25 +4,35 @@ |
| #include "tools/json_schema_compiler/util.h" |
| +#include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| namespace json_schema_compiler { |
| namespace util { |
| +namespace { |
| + |
| +bool ReportError(const base::Value& from, |
| + base::Value::Type expected, |
| + base::string16* error) { |
| + if (!error->empty()) |
| + error->append(base::ASCIIToUTF16("; ")); |
| + error->append(base::ASCIIToUTF16(base::StringPrintf( |
| + "expected %s, got %s", base::Value::GetTypeName(expected), |
| + base::Value::GetTypeName(from.GetType())))); |
| + return false; // Always false on purpose. |
| +} |
| + |
| +} // namespace |
| + |
| bool PopulateItem(const base::Value& from, int* out) { |
| return from.GetAsInteger(out); |
| } |
| bool PopulateItem(const base::Value& from, int* out, base::string16* error) { |
| - if (!from.GetAsInteger(out)) { |
| - if (error->length()) { |
| - error->append(base::UTF8ToUTF16("; ")); |
| - } |
| - error->append(base::UTF8ToUTF16("expected integer, got " + |
| - ValueTypeToString(from.GetType()))); |
| - return false; |
| - } |
| + if (!from.GetAsInteger(out)) |
| + return ReportError(from, base::Value::TYPE_INTEGER, error); |
| return true; |
| } |
| @@ -31,14 +41,8 @@ bool PopulateItem(const base::Value& from, bool* out) { |
| } |
| bool PopulateItem(const base::Value& from, bool* out, base::string16* error) { |
| - if (!from.GetAsBoolean(out)) { |
| - if (error->length()) { |
| - error->append(base::UTF8ToUTF16("; ")); |
| - } |
| - error->append(base::UTF8ToUTF16("expected boolean, got " + |
| - ValueTypeToString(from.GetType()))); |
| - return false; |
| - } |
| + if (!from.GetAsBoolean(out)) |
| + return ReportError(from, base::Value::TYPE_BOOLEAN, error); |
| return true; |
| } |
| @@ -47,14 +51,8 @@ bool PopulateItem(const base::Value& from, double* out) { |
| } |
| bool PopulateItem(const base::Value& from, double* out, base::string16* error) { |
| - if (!from.GetAsDouble(out)) { |
| - if (error->length()) { |
| - error->append(base::UTF8ToUTF16("; ")); |
| - } |
| - error->append(base::UTF8ToUTF16("expected double, got " + |
| - ValueTypeToString(from.GetType()))); |
| - return false; |
| - } |
| + if (!from.GetAsDouble(out)) |
| + return ReportError(from, base::Value::TYPE_DOUBLE, error); |
| return true; |
| } |
| @@ -65,14 +63,8 @@ bool PopulateItem(const base::Value& from, std::string* out) { |
| bool PopulateItem(const base::Value& from, |
| std::string* out, |
| base::string16* error) { |
| - if (!from.GetAsString(out)) { |
| - if (error->length()) { |
| - error->append(base::UTF8ToUTF16("; ")); |
| - } |
| - error->append(base::UTF8ToUTF16("expected string, got " + |
| - ValueTypeToString(from.GetType()))); |
| - return false; |
| - } |
| + if (!from.GetAsString(out)) |
| + return ReportError(from, base::Value::TYPE_STRING, error); |
| return true; |
| } |
| @@ -88,14 +80,8 @@ bool PopulateItem(const base::Value& from, |
| std::vector<char>* out, |
| base::string16* error) { |
| const base::BinaryValue* binary = nullptr; |
| - if (!from.GetAsBinary(&binary)) { |
| - if (error->length()) { |
| - error->append(base::UTF8ToUTF16("; ")); |
| - } |
| - error->append(base::UTF8ToUTF16("expected binary, got " + |
| - ValueTypeToString(from.GetType()))); |
| - return false; |
| - } |
| + if (!from.GetAsBinary(&binary)) |
| + return ReportError(from, base::Value::TYPE_BINARY, error); |
| out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); |
| return true; |
| } |
| @@ -125,14 +111,8 @@ bool PopulateItem(const base::Value& from, |
| std::unique_ptr<base::DictionaryValue>* out, |
| base::string16* error) { |
| const base::DictionaryValue* dict = nullptr; |
| - if (!from.GetAsDictionary(&dict)) { |
| - if (error->length()) { |
| - error->append(base::UTF8ToUTF16("; ")); |
| - } |
| - error->append(base::UTF8ToUTF16("expected dictionary, got " + |
| - ValueTypeToString(from.GetType()))); |
| - return false; |
| - } |
| + if (!from.GetAsDictionary(&dict)) |
| + return ReportError(from, base::Value::TYPE_DICTIONARY, error); |
| *out = dict->CreateDeepCopy(); |
| return true; |
| } |
| @@ -168,28 +148,5 @@ void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from, |
| out->Append(from->CreateDeepCopy()); |
| } |
| -std::string ValueTypeToString(base::Value::Type type) { |
| - switch (type) { |
| - case base::Value::TYPE_NULL: |
| - return "null"; |
| - case base::Value::TYPE_BOOLEAN: |
| - return "boolean"; |
| - case base::Value::TYPE_INTEGER: |
| - return "integer"; |
| - case base::Value::TYPE_DOUBLE: |
| - return "number"; |
|
Lei Zhang
2016/07/16 02:02:23
https://codereview.chromium.org/16462004/diff/3000
Devlin
2016/07/18 13:29:46
I think the motivation was since these values larg
|
| - case base::Value::TYPE_STRING: |
| - return "string"; |
| - case base::Value::TYPE_BINARY: |
| - return "binary"; |
| - case base::Value::TYPE_DICTIONARY: |
| - return "dictionary"; |
| - case base::Value::TYPE_LIST: |
| - return "list"; |
| - } |
| - NOTREACHED(); |
| - return ""; |
| -} |
| - |
| } // namespace util |
| } // namespace json_schema_compiler |