Chromium Code Reviews| 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 #include "tools/json_schema_compiler/util.h" | 5 #include "tools/json_schema_compiler/util.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | |
| 7 #include "base/strings/utf_string_conversions.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 |
| 14 namespace { | |
| 15 | |
| 16 bool ReportError(const base::Value& from, | |
| 17 base::Value::Type expected, | |
| 18 base::string16* error) { | |
| 19 if (!error->empty()) | |
| 20 error->append(base::ASCIIToUTF16("; ")); | |
| 21 error->append(base::ASCIIToUTF16(base::StringPrintf( | |
| 22 "expected %s, got %s", base::Value::GetTypeName(expected), | |
| 23 base::Value::GetTypeName(from.GetType())))); | |
| 24 return false; // Always false on purpose. | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 13 bool PopulateItem(const base::Value& from, int* out) { | 29 bool PopulateItem(const base::Value& from, int* out) { |
| 14 return from.GetAsInteger(out); | 30 return from.GetAsInteger(out); |
| 15 } | 31 } |
| 16 | 32 |
| 17 bool PopulateItem(const base::Value& from, int* out, base::string16* error) { | 33 bool PopulateItem(const base::Value& from, int* out, base::string16* error) { |
| 18 if (!from.GetAsInteger(out)) { | 34 if (!from.GetAsInteger(out)) |
| 19 if (error->length()) { | 35 return ReportError(from, base::Value::TYPE_INTEGER, error); |
| 20 error->append(base::UTF8ToUTF16("; ")); | |
| 21 } | |
| 22 error->append(base::UTF8ToUTF16("expected integer, got " + | |
| 23 ValueTypeToString(from.GetType()))); | |
| 24 return false; | |
| 25 } | |
| 26 return true; | 36 return true; |
| 27 } | 37 } |
| 28 | 38 |
| 29 bool PopulateItem(const base::Value& from, bool* out) { | 39 bool PopulateItem(const base::Value& from, bool* out) { |
| 30 return from.GetAsBoolean(out); | 40 return from.GetAsBoolean(out); |
| 31 } | 41 } |
| 32 | 42 |
| 33 bool PopulateItem(const base::Value& from, bool* out, base::string16* error) { | 43 bool PopulateItem(const base::Value& from, bool* out, base::string16* error) { |
| 34 if (!from.GetAsBoolean(out)) { | 44 if (!from.GetAsBoolean(out)) |
| 35 if (error->length()) { | 45 return ReportError(from, base::Value::TYPE_BOOLEAN, error); |
| 36 error->append(base::UTF8ToUTF16("; ")); | |
| 37 } | |
| 38 error->append(base::UTF8ToUTF16("expected boolean, got " + | |
| 39 ValueTypeToString(from.GetType()))); | |
| 40 return false; | |
| 41 } | |
| 42 return true; | 46 return true; |
| 43 } | 47 } |
| 44 | 48 |
| 45 bool PopulateItem(const base::Value& from, double* out) { | 49 bool PopulateItem(const base::Value& from, double* out) { |
| 46 return from.GetAsDouble(out); | 50 return from.GetAsDouble(out); |
| 47 } | 51 } |
| 48 | 52 |
| 49 bool PopulateItem(const base::Value& from, double* out, base::string16* error) { | 53 bool PopulateItem(const base::Value& from, double* out, base::string16* error) { |
| 50 if (!from.GetAsDouble(out)) { | 54 if (!from.GetAsDouble(out)) |
| 51 if (error->length()) { | 55 return ReportError(from, base::Value::TYPE_DOUBLE, error); |
| 52 error->append(base::UTF8ToUTF16("; ")); | |
| 53 } | |
| 54 error->append(base::UTF8ToUTF16("expected double, got " + | |
| 55 ValueTypeToString(from.GetType()))); | |
| 56 return false; | |
| 57 } | |
| 58 return true; | 56 return true; |
| 59 } | 57 } |
| 60 | 58 |
| 61 bool PopulateItem(const base::Value& from, std::string* out) { | 59 bool PopulateItem(const base::Value& from, std::string* out) { |
| 62 return from.GetAsString(out); | 60 return from.GetAsString(out); |
| 63 } | 61 } |
| 64 | 62 |
| 65 bool PopulateItem(const base::Value& from, | 63 bool PopulateItem(const base::Value& from, |
| 66 std::string* out, | 64 std::string* out, |
| 67 base::string16* error) { | 65 base::string16* error) { |
| 68 if (!from.GetAsString(out)) { | 66 if (!from.GetAsString(out)) |
| 69 if (error->length()) { | 67 return ReportError(from, base::Value::TYPE_STRING, error); |
| 70 error->append(base::UTF8ToUTF16("; ")); | |
| 71 } | |
| 72 error->append(base::UTF8ToUTF16("expected string, got " + | |
| 73 ValueTypeToString(from.GetType()))); | |
| 74 return false; | |
| 75 } | |
| 76 return true; | 68 return true; |
| 77 } | 69 } |
| 78 | 70 |
| 79 bool PopulateItem(const base::Value& from, std::vector<char>* out) { | 71 bool PopulateItem(const base::Value& from, std::vector<char>* out) { |
| 80 const base::BinaryValue* binary = nullptr; | 72 const base::BinaryValue* binary = nullptr; |
| 81 if (!from.GetAsBinary(&binary)) | 73 if (!from.GetAsBinary(&binary)) |
| 82 return false; | 74 return false; |
| 83 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); | 75 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); |
| 84 return true; | 76 return true; |
| 85 } | 77 } |
| 86 | 78 |
| 87 bool PopulateItem(const base::Value& from, | 79 bool PopulateItem(const base::Value& from, |
| 88 std::vector<char>* out, | 80 std::vector<char>* out, |
| 89 base::string16* error) { | 81 base::string16* error) { |
| 90 const base::BinaryValue* binary = nullptr; | 82 const base::BinaryValue* binary = nullptr; |
| 91 if (!from.GetAsBinary(&binary)) { | 83 if (!from.GetAsBinary(&binary)) |
| 92 if (error->length()) { | 84 return ReportError(from, base::Value::TYPE_BINARY, error); |
| 93 error->append(base::UTF8ToUTF16("; ")); | |
| 94 } | |
| 95 error->append(base::UTF8ToUTF16("expected binary, got " + | |
| 96 ValueTypeToString(from.GetType()))); | |
| 97 return false; | |
| 98 } | |
| 99 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); | 85 out->assign(binary->GetBuffer(), binary->GetBuffer() + binary->GetSize()); |
| 100 return true; | 86 return true; |
| 101 } | 87 } |
| 102 | 88 |
| 103 bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out) { | 89 bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out) { |
| 104 *out = from.CreateDeepCopy(); | 90 *out = from.CreateDeepCopy(); |
| 105 return true; | 91 return true; |
| 106 } | 92 } |
| 107 | 93 |
| 108 bool PopulateItem(const base::Value& from, | 94 bool PopulateItem(const base::Value& from, |
| 109 std::unique_ptr<base::Value>* out, | 95 std::unique_ptr<base::Value>* out, |
| 110 base::string16* error) { | 96 base::string16* error) { |
| 111 *out = from.CreateDeepCopy(); | 97 *out = from.CreateDeepCopy(); |
| 112 return true; | 98 return true; |
| 113 } | 99 } |
| 114 | 100 |
| 115 bool PopulateItem(const base::Value& from, | 101 bool PopulateItem(const base::Value& from, |
| 116 std::unique_ptr<base::DictionaryValue>* out) { | 102 std::unique_ptr<base::DictionaryValue>* out) { |
| 117 const base::DictionaryValue* dict = nullptr; | 103 const base::DictionaryValue* dict = nullptr; |
| 118 if (!from.GetAsDictionary(&dict)) | 104 if (!from.GetAsDictionary(&dict)) |
| 119 return false; | 105 return false; |
| 120 *out = dict->CreateDeepCopy(); | 106 *out = dict->CreateDeepCopy(); |
| 121 return true; | 107 return true; |
| 122 } | 108 } |
| 123 | 109 |
| 124 bool PopulateItem(const base::Value& from, | 110 bool PopulateItem(const base::Value& from, |
| 125 std::unique_ptr<base::DictionaryValue>* out, | 111 std::unique_ptr<base::DictionaryValue>* out, |
| 126 base::string16* error) { | 112 base::string16* error) { |
| 127 const base::DictionaryValue* dict = nullptr; | 113 const base::DictionaryValue* dict = nullptr; |
| 128 if (!from.GetAsDictionary(&dict)) { | 114 if (!from.GetAsDictionary(&dict)) |
| 129 if (error->length()) { | 115 return ReportError(from, base::Value::TYPE_DICTIONARY, error); |
| 130 error->append(base::UTF8ToUTF16("; ")); | |
| 131 } | |
| 132 error->append(base::UTF8ToUTF16("expected dictionary, got " + | |
| 133 ValueTypeToString(from.GetType()))); | |
| 134 return false; | |
| 135 } | |
| 136 *out = dict->CreateDeepCopy(); | 116 *out = dict->CreateDeepCopy(); |
| 137 return true; | 117 return true; |
| 138 } | 118 } |
| 139 | 119 |
| 140 void AddItemToList(const int from, base::ListValue* out) { | 120 void AddItemToList(const int from, base::ListValue* out) { |
| 141 out->AppendInteger(from); | 121 out->AppendInteger(from); |
| 142 } | 122 } |
| 143 | 123 |
| 144 void AddItemToList(const bool from, base::ListValue* out) { | 124 void AddItemToList(const bool from, base::ListValue* out) { |
| 145 out->AppendBoolean(from); | 125 out->AppendBoolean(from); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 161 void AddItemToList(const std::unique_ptr<base::Value>& from, | 141 void AddItemToList(const std::unique_ptr<base::Value>& from, |
| 162 base::ListValue* out) { | 142 base::ListValue* out) { |
| 163 out->Append(from->CreateDeepCopy()); | 143 out->Append(from->CreateDeepCopy()); |
| 164 } | 144 } |
| 165 | 145 |
| 166 void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from, | 146 void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from, |
| 167 base::ListValue* out) { | 147 base::ListValue* out) { |
| 168 out->Append(from->CreateDeepCopy()); | 148 out->Append(from->CreateDeepCopy()); |
| 169 } | 149 } |
| 170 | 150 |
| 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"; | |
|
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
| |
| 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"; | |
| 189 } | |
| 190 NOTREACHED(); | |
| 191 return ""; | |
| 192 } | |
| 193 | |
| 194 } // namespace util | 151 } // namespace util |
| 195 } // namespace json_schema_compiler | 152 } // namespace json_schema_compiler |
| OLD | NEW |