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

Unified Diff: tools/json_schema_compiler/util.h

Issue 851673003: Cleanup: Some simplifications in json_schema_compiler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_dart_tests
Patch Set: Add unit test for GetAsBinary. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/util.h
diff --git a/tools/json_schema_compiler/util.h b/tools/json_schema_compiler/util.h
index b844dd2882c03137cfa7410504fea566e515aeed..a1432445866445ae7ba8f7a798d2c8962e5d3499 100644
--- a/tools/json_schema_compiler/util.h
+++ b/tools/json_schema_compiler/util.h
@@ -16,26 +16,22 @@ namespace json_schema_compiler {
namespace util {
-// Creates a new item at |out| from |from|[|index|]. These are used by template
+// Populates the item |out| from the value |from|. These are used by template
// specializations of |Get(Optional)ArrayFromList|.
-bool GetItemFromList(const base::ListValue& from, int index, int* out);
-bool GetItemFromList(const base::ListValue& from, int index, bool* out);
-bool GetItemFromList(const base::ListValue& from, int index, double* out);
-bool GetItemFromList(const base::ListValue& from, int index, std::string* out);
-bool GetItemFromList(const base::ListValue& from,
- int index,
- linked_ptr<base::Value>* out);
-bool GetItemFromList(const base::ListValue& from,
- int index,
- linked_ptr<base::DictionaryValue>* out);
+bool PopulateItem(const base::Value& from, int* out);
+bool PopulateItem(const base::Value& from, bool* out);
+bool PopulateItem(const base::Value& from, double* out);
+bool PopulateItem(const base::Value& from, std::string* out);
+bool PopulateStringFromBinary(const base::Value& from, std::string* out);
+bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out);
+bool PopulateItem(const base::Value& from,
+ linked_ptr<base::DictionaryValue>* out);
// This template is used for types generated by tools/json_schema_compiler.
-template<class T>
-bool GetItemFromList(const base::ListValue& from,
- int index,
- linked_ptr<T>* out) {
- const base::DictionaryValue* dict;
- if (!from.GetDictionary(index, &dict))
+template <class T>
+bool PopulateItem(const base::Value& from, linked_ptr<T>* out) {
+ const base::DictionaryValue* dict = nullptr;
+ if (!from.GetAsDictionary(&dict))
return false;
scoped_ptr<T> obj(new T());
if (!T::Populate(*dict, obj.get()))
@@ -47,14 +43,16 @@ bool GetItemFromList(const base::ListValue& from,
// Populates |out| with |list|. Returns false if there is no list at the
// specified key or if the list has anything other than |T|.
template <class T>
-bool PopulateArrayFromList(
- const base::ListValue& list, std::vector<T>* out) {
+bool PopulateArrayFromList(const base::ListValue& list,
+ bool (*populateItem)(const base::Value&, T*),
+ std::vector<T>* out) {
out->clear();
- T value;
+ T item;
for (size_t i = 0; i < list.GetSize(); ++i) {
- if (!GetItemFromList(list, i, &value))
+ const base::Value* value = nullptr;
+ if (!list.Get(i, &value) || !populateItem(*value, &item))
return false;
- out->push_back(value);
+ out->push_back(item);
}
return true;
@@ -64,19 +62,14 @@ bool PopulateArrayFromList(
// true on success or if there is nothing at the specified key. Returns false
// if anything other than a list of |T| is at the specified key.
template <class T>
-bool PopulateOptionalArrayFromList(
- const base::ListValue& list,
- scoped_ptr<std::vector<T> >* out) {
+bool PopulateOptionalArrayFromList(const base::ListValue& list,
+ bool (*populateItem)(const base::Value&, T*),
+ scoped_ptr<std::vector<T>>* out) {
out->reset(new std::vector<T>());
- T value;
- for (size_t i = 0; i < list.GetSize(); ++i) {
- if (!GetItemFromList(list, i, &value)) {
- out->reset();
- return false;
- }
- (*out)->push_back(value);
+ if (!PopulateArrayFromList(list, populateItem, out->get())) {
+ out->reset();
+ return false;
}
-
return true;
}
@@ -97,7 +90,7 @@ void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) {
out->Append(from->ToValue().release());
}
-// Set |out| to the the contents of |from|. Requires GetItemFromList to be
+// Set |out| to the the contents of |from|. Requires PopulateItem to be
// implemented for |T|.
template <class T>
void PopulateListFromArray(
@@ -110,8 +103,8 @@ void PopulateListFromArray(
}
}
-// Set |out| to the the contents of |from| if |from| is non-NULL. Requires
-// GetItemFromList to be implemented for |T|.
+// Set |out| to the the contents of |from| if |from| is not null. Requires
+// PopulateItem to be implemented for |T|.
template <class T>
void PopulateListFromOptionalArray(
const scoped_ptr<std::vector<T> >& from,

Powered by Google App Engine
This is Rietveld 408576698