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

Unified Diff: tools/json_schema_compiler/util.h

Issue 9309044: Supporting more APIs with json_schema_compiler (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: support for choices Created 8 years, 10 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 e94b518cd1232f4a1e8808febbc71198385f1584..0865404f64063079be92736d7d50e26dc5f2a23d 100644
--- a/tools/json_schema_compiler/util.h
+++ b/tools/json_schema_compiler/util.h
@@ -9,43 +9,168 @@
#include <string>
#include <vector>
+#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
-
-namespace base {
-class DictionaryValue;
-}
+#include "base/values.h"
namespace json_schema_compiler {
namespace util {
-// Creates a new vector containing the strings |from|.|name| at |out|. Returns
+// Creates a new item at |out| from |from|[|index|]. These are used by template
+// specializations of |Get(Optional)ArrayFromList|.
+bool GetItemFromList(const ListValue& from, int index, int* out);
+bool GetItemFromList(const ListValue& from, int index, bool* out);
+bool GetItemFromList(const ListValue& from, int index, double* out);
+bool GetItemFromList(const ListValue& from, int index, std::string* out);
+bool GetItemFromList(const ListValue& from, int index,
+ linked_ptr<base::DictionaryValue>* out);
+
+// This template is used for types generated by tools/json_schema_compiler.
+template<class T>
+bool GetItemFromList(const ListValue& from, int index, linked_ptr<T>* out) {
+ DictionaryValue* dict;
+ if (!from.GetDictionary(index, &dict))
+ return false;
+ T* obj(new T());
+ if (!T::Populate(*dict, obj))
+ return false;
+ *out = linked_ptr<T>(obj);
+ return true;
+}
+// Creates a new vector containing Llist| at |out|. Returns
// false if there is no list at the specified key or if the list has anything
-// other than strings.
-bool GetStrings(
+// other than |T|.
+template <class T>
+bool GetArrayFromList(
+ const base::ListValue& list, std::vector<T>* out) {
+ T value;
+ for (size_t i = 0; i < list.GetSize(); ++i) {
+ if (!GetItemFromList(list, i, &value))
+ return false;
+ out->push_back(value);
+ }
+
+ return true;
+}
+
+// Creates a new vector containing |from|.|name| at |out|. Returns
+// false if there is no list at the specified key or if the list has anything
+// other than |T|.
+template <class T>
+bool GetArrayFromDictionary(
const base::DictionaryValue& from,
const std::string& name,
- std::vector<std::string>* out);
+ std::vector<T>* out) {
+ base::ListValue* list = NULL;
+ if (!from.GetListWithoutPathExpansion(name, &list))
+ return false;
+
+ return GetArrayFromList(*list, out);
+}
+
+// Creates a new vector containing |list| at |out|. Returns
+// 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 GetOptionalArrayFromList(
+ const base::ListValue& list,
+ 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);
+ }
+
+ return true;
+}
-// Creates a new vector containing the strings |from|.|name| at |out|. Returns
+// Creates a new vector containing |from|.|name| at |out|. Returns
// true on success or if there is nothing at the specified key. Returns false
-// if anything other than a list of strings is at the specified key.
-bool GetOptionalStrings(
+// if anything other than a list of |T| is at the specified key.
+template <class T>
+bool GetOptionalArrayFromDictionary(
const base::DictionaryValue& from,
const std::string& name,
- scoped_ptr<std::vector<std::string> >* out);
+ scoped_ptr<std::vector<T> >* out) {
+ base::ListValue* list = NULL;
+ {
+ base::Value* maybe_list = NULL;
+ // Since |name| is optional, its absence is acceptable. However, anything
+ // other than a ListValue is not.
+ if (!from.GetWithoutPathExpansion(name, &maybe_list))
+ return true;
+ if (!maybe_list->IsType(base::Value::TYPE_LIST))
+ return false;
+ list = static_cast<base::ListValue*>(maybe_list);
+ }
+
+ return GetOptionalArrayFromList(*list, out);
+}
+
+// Appends a Value newly created from |from| to |out|. These used by template
+// specializations of |Set(Optional)ArrayToList|.
+void AddItemToList(const int from, base::ListValue* out);
+void AddItemToList(const bool from, base::ListValue* out);
+void AddItemToList(const double from, base::ListValue* out);
+void AddItemToList(const std::string& from, base::ListValue* out);
+void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
+ base::ListValue* out);
-// Puts the each string in |from| into a new ListValue at |out|.|name|.
-void SetStrings(
- const std::vector<std::string>& from,
+// This template is used for types generated by tools/json_schema_compiler.
+template<class T>
+void AddItemToList(const linked_ptr<T>& from, ListValue* out) {
+ out->Append(from->ToValue());
+}
+
+// Set |out| to the the contents of |from|. Requires GetItemFromList to be
+// implemented for |T|.
+template <class T>
+void SetArrayToList(
+ const std::vector<T>& from,
+ base::ListValue* out) {
+ for (typename std::vector<T>::const_iterator it = from.begin();
+ it != from.end(); ++it) {
+ AddItemToList(*it, out);
+ }
+}
+
+// Set |out| to the the contents of |from| if |from| is non-NULL. Requires
+// GetItemFromList to be implemented for |T|.
+template <class T>
+void SetOptionalArrayToList(
+ const scoped_ptr<std::vector<T> >& from,
+ base::ListValue* out) {
+ if (from.get())
+ SetArrayToList(*from, out);
+}
+
+// Sets |out|.|name| to a newly created ListValue containing |from|. Requires
+// GetItemFromList to be implemented for |T|.
+template <class T>
+void SetArrayToDictionary(
+ const std::vector<T>& from,
const std::string& name,
- base::DictionaryValue* out);
+ base::DictionaryValue* out) {
+ base::ListValue* list = new base::ListValue();
+ out->SetWithoutPathExpansion(name, list);
+ SetArrayToList(from, list);
+}
-// If from is non-NULL, puts each string in |from| into a new ListValue at
-// |out|.|name|.
-void SetOptionalStrings(
- const scoped_ptr<std::vector<std::string> >& from,
+// If |from| is non-NULL, sets |out|.|name| to a newly created ListValue
+// containing |from|. Requires GetItemFromList to be implemented for |T|.
+template <class T>
+void SetOptionalArrayToDictionary(
+ const scoped_ptr<std::vector<T> >& from,
const std::string& name,
- base::DictionaryValue* out);
+ base::DictionaryValue* out) {
+ if (from.get())
+ SetArrayToDictionary(*from, name, out);
+}
+
} // namespace api_util
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698