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

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: fixed optional properties Created 8 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 e94b518cd1232f4a1e8808febbc71198385f1584..a8d3ae3a3502d1a48b8937cbe3b4ef61c923eed2 100644
--- a/tools/json_schema_compiler/util.h
+++ b/tools/json_schema_compiler/util.h
@@ -10,42 +10,164 @@
#include <vector>
#include "base/memory/scoped_ptr.h"
-
-namespace base {
-class DictionaryValue;
-}
+#include "base/memory/linked_ptr.h"
not at google - send to devlin 2012/02/05 23:42:12 :sort
calamity 2012/02/06 11:51:18 Done.
+#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|].
not at google - send to devlin 2012/02/05 23:42:12 Explain that these are template specializations fo
calamity 2012/02/06 11:51:18 Done. Couldn't move these under the templates that
+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))
not at google - send to devlin 2012/02/05 23:42:12 space after if
calamity 2012/02/06 11:51:18 Done.
+ return false;
+ *out = linked_ptr<T>(obj);
+ return true;
+}
+
+// Appends a Value newly created from |from| to |out|.
+void AddItemToList(const int from, base::ListValue* out);
not at google - send to devlin 2012/02/05 23:42:12 Ditto explanation/ordering.
+void AddItemToList(const bool from, base::ListValue* out);
+void AddItemToList(const double from, base::ListValue* out);
+// TODO(calamity) does std::string need &?
not at google - send to devlin 2012/02/05 23:42:12 yes
calamity 2012/02/06 11:51:18 Done.
+void AddItemToList(const std::string from, base::ListValue* out);
+void AddItemToList(const linked_ptr<base::DictionaryValue>& from, base::ListValue* out);
+
+// 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());
+}
+
+template <class T>
+void SetArrayToList(
+ const std::vector<T>& from,
+ base::ListValue* out) {
not at google - send to devlin 2012/02/05 23:42:12 Put on same line as above
calamity 2012/02/06 11:51:18 Done.
+ for (typename std::vector<T>::const_iterator it = from.begin();
+ it != from.end(); ++it) {
+ AddItemToList(*it, out);
+ }
+}
+
+template <class T>
+void SetOptionalArrayToList(
+ const scoped_ptr<std::vector<T> >& from,
+ base::ListValue* out) {
not at google - send to devlin 2012/02/05 23:42:12 ditto
calamity 2012/02/06 11:51:18 Done.
+ if (!from.get())
+ return;
+ SetArrayToList(from, out);
not at google - send to devlin 2012/02/05 23:42:12 heh, a little more concise as if (from.get()) S
calamity 2012/02/06 11:51:18 Done.
+}
+
+// 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::ListValue* list = new base::ListValue();
+ out->SetWithoutPathExpansion(name, list);
+ SetArrayToList(from, list);
+}
+
+// If |from| is non-NULL, sets |out|.|name| to a newly created ListValue containing |from|. Requires
+// GetItemFromList to be implemented for |T|.
not at google - send to devlin 2012/02/05 23:42:12 run gq over this
calamity 2012/02/06 11:51:18 Done.
+template <class T>
+void SetOptionalArrayToDictionary(
+ const scoped_ptr<std::vector<T> >& from,
+ const std::string& name,
+ base::DictionaryValue* out) {
+ if (!from.get())
+ return;
+
+ SetArrayToDictionary(*from, name, out);
+}
+
+// Creates a new vector containing the fundamentals |list| at |out|. Returns
not at google - send to devlin 2012/02/05 23:42:12 what does "fundamentals" mean?
calamity 2012/02/06 11:51:18 Sorry, forgot to change this doc.
// 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 the fundamentals |from|.|name| at |out|. Returns
+// false if there is no list at the specified key or if the list has anything
+// other than |T|.
not at google - send to devlin 2012/02/05 23:42:12 gq here too
calamity 2012/02/06 11:51:18 Done.
+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 the strings |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
// 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);
+ }
-// Puts the each string in |from| into a new ListValue at |out|.|name|.
-void SetStrings(
- const std::vector<std::string>& from,
- const std::string& name,
- base::DictionaryValue* out);
-
-// 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,
- const std::string& name,
- base::DictionaryValue* out);
+ return GetOptionalArrayFromList(*list, out);
+}
} // namespace api_util
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698