| 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..908ac012a142bd4f16a8a1c8d80fb005881439cf 100644
|
| --- a/tools/json_schema_compiler/util.h
|
| +++ b/tools/json_schema_compiler/util.h
|
| @@ -10,42 +10,141 @@
|
| #include <vector>
|
|
|
| #include "base/memory/scoped_ptr.h"
|
| -
|
| -namespace base {
|
| -class DictionaryValue;
|
| -}
|
| +#include "base/memory/linked_ptr.h"
|
| +#include "base/values.h"
|
|
|
| namespace json_schema_compiler {
|
| namespace util {
|
|
|
| -// Creates a new vector containing the strings |from|.|name| at |out|. Returns
|
| +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);
|
| +
|
| +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;
|
| +}
|
| +
|
| +void SetItemInList(const int from, base::ListValue* out);
|
| +void SetItemInList(const bool from, base::ListValue* out);
|
| +void SetItemInList(const double from, base::ListValue* out);
|
| +// TODO std::string needs &?
|
| +void SetItemInList(const std::string from, base::ListValue* out);
|
| +void SetItemInList(const linked_ptr<base::DictionaryValue>& from, base::ListValue* out);
|
| +
|
| +template<class T>
|
| +void SetItemInList(const linked_ptr<T>& from, ListValue* out) {
|
| + out->Append(from->ToValue());
|
| +}
|
| +
|
| +template <class T>
|
| +void SetArrayFromDictionary(
|
| + const std::vector<T>& from,
|
| + const std::string& name,
|
| + base::DictionaryValue* out) {
|
| + base::ListValue* list = new base::ListValue();
|
| + out->SetWithoutPathExpansion(name, list);
|
| + for (typename std::vector<T>::const_iterator it = from.begin();
|
| + it != from.end(); ++it) {
|
| + SetItemInList(*it, list);
|
| + }
|
| +}
|
| +
|
| +template <class T>
|
| +void SetOptionalArrayFromDictionary(
|
| + const scoped_ptr<std::vector<T> >& from,
|
| + const std::string& name,
|
| + base::DictionaryValue* out) {
|
| + if (!from.get())
|
| + return;
|
| +
|
| + SetArrayFromDictionary(*from, name, out);
|
| +}
|
| +
|
| +// Creates a new vector containing the fundamentals |list| 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 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|.
|
| +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);
|
| + return GetOptionalArrayFromList(*list, 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);
|
|
|
| } // namespace api_util
|
| } // namespace extensions
|
|
|