Chromium Code Reviews| Index: tools/json_schema_compiler/util.cc |
| diff --git a/tools/json_schema_compiler/util.cc b/tools/json_schema_compiler/util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..38e6b30674e75bb39bdae1d90314390fb1d32d94 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/util.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "tools/json_schema_compiler/util.h" |
| + |
| +#include "base/values.h" |
| + |
| +namespace json_schema_compiler { |
| +namespace util { |
| + |
| +bool GetStrings( |
| + const base::DictionaryValue& from, |
| + const std::string& name, |
| + std::vector<std::string>* out) { |
| + base::ListValue* list = NULL; |
| + if (!from.GetListWithoutPathExpansion(name, &list)) |
| + return false; |
| + |
| + std::string string; |
| + for (size_t i = 0; i < list->GetSize(); ++i) { |
| + if (!list->GetString(i, &string)) |
| + return false; |
| + out->push_back(string); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool GetOptionalStrings( |
| + const base::DictionaryValue& from, |
| + const std::string& name, |
| + scoped_ptr<std::vector<std::string> >* 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); |
| + } |
| + |
| + out->reset(new std::vector<std::string>()); |
|
Yoyo Zhou
2012/01/19 23:19:11
This clears out, whereas the comment says that it
calamity
2012/01/23 05:14:45
Done.
|
| + std::string string; |
| + for (size_t i = 0; i < list->GetSize(); ++i) { |
| + if (!list->GetString(i, &string)) { |
| + out->reset(); |
| + return false; |
| + } |
| + (*out)->push_back(string); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void SetStrings( |
| + const std::vector<std::string>& from, |
| + const std::string& name, |
| + base::DictionaryValue* out) { |
| + base::ListValue* list = new base::ListValue(); |
| + out->SetWithoutPathExpansion(name, list); |
| + for (std::vector<std::string>::const_iterator it = from.begin(); |
| + it != from.end(); ++it) { |
| + list->Append(base::Value::CreateStringValue(*it)); |
| + } |
| +} |
| + |
| +void SetOptionalStrings( |
| + const scoped_ptr<std::vector<std::string> >& from, |
| + const std::string& name, |
| + base::DictionaryValue* out) { |
| + if (!from.get()) |
| + return; |
| + |
| + SetStrings(*from, name, out); |
| +} |
| + |
| +} // namespace api_util |
| +} // namespace extensions |