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

Side by Side Diff: tools/json_schema_compiler/util.cc

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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/json_schema_compiler/util.h" 5 #include "tools/json_schema_compiler/util.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 8
9 namespace json_schema_compiler { 9 namespace json_schema_compiler {
10 namespace util { 10 namespace util {
11 11
12 bool GetStrings( 12 bool GetItemFromList(const ListValue& from, int index, int* out) {
13 const base::DictionaryValue& from, 13 return from.GetInteger(index, out);
14 const std::string& name, 14 }
15 std::vector<std::string>* out) { 15
16 base::ListValue* list = NULL; 16 bool GetItemFromList(const ListValue& from, int index, bool* out) {
17 if (!from.GetListWithoutPathExpansion(name, &list)) 17 return from.GetBoolean(index, out);
18 }
19
20 bool GetItemFromList(const ListValue& from, int index, double* out) {
21 return from.GetDouble(index, out);
22 }
23
24 bool GetItemFromList(const ListValue& from, int index, std::string* out) {
25 return from.GetString(index, out);
26 }
27
28 bool GetItemFromList(const ListValue& from, int index,
29 linked_ptr<base::DictionaryValue>* out) {
30 DictionaryValue* dict = NULL;
31 if (!from.GetDictionary(index, &dict)) {
18 return false; 32 return false;
19
20 std::string string;
21 for (size_t i = 0; i < list->GetSize(); ++i) {
22 if (!list->GetString(i, &string))
23 return false;
24 out->push_back(string);
25 } 33 }
26 34 *out = linked_ptr<DictionaryValue>(dict->DeepCopy());
27 return true; 35 return true;
28 } 36 }
29 37
30 bool GetOptionalStrings( 38 void AddItemToList(const int from, base::ListValue* out) {
31 const base::DictionaryValue& from, 39 out->Append(base::Value::CreateIntegerValue(from));
32 const std::string& name,
33 scoped_ptr<std::vector<std::string> >* out) {
34 base::ListValue* list = NULL;
35 {
36 base::Value* maybe_list = NULL;
37 // Since |name| is optional, its absence is acceptable. However, anything
38 // other than a ListValue is not.
39 if (!from.GetWithoutPathExpansion(name, &maybe_list))
40 return true;
41 if (!maybe_list->IsType(base::Value::TYPE_LIST))
42 return false;
43 list = static_cast<base::ListValue*>(maybe_list);
44 }
45
46 out->reset(new std::vector<std::string>());
47 std::string string;
48 for (size_t i = 0; i < list->GetSize(); ++i) {
49 if (!list->GetString(i, &string)) {
50 out->reset();
51 return false;
52 }
53 (*out)->push_back(string);
54 }
55
56 return true;
57 } 40 }
58 41 void AddItemToList(const bool from, base::ListValue* out) {
59 void SetStrings( 42 out->Append(base::Value::CreateBooleanValue(from));
60 const std::vector<std::string>& from,
61 const std::string& name,
62 base::DictionaryValue* out) {
63 base::ListValue* list = new base::ListValue();
64 out->SetWithoutPathExpansion(name, list);
65 for (std::vector<std::string>::const_iterator it = from.begin();
66 it != from.end(); ++it) {
67 list->Append(base::Value::CreateStringValue(*it));
68 }
69 } 43 }
70 44 void AddItemToList(const double from, base::ListValue* out) {
71 void SetOptionalStrings( 45 out->Append(base::Value::CreateDoubleValue(from));
72 const scoped_ptr<std::vector<std::string> >& from, 46 }
73 const std::string& name, 47 void AddItemToList(const std::string& from, base::ListValue* out) {
74 base::DictionaryValue* out) { 48 out->Append(base::Value::CreateStringValue(from));
75 if (!from.get()) 49 }
76 return; 50 void AddItemToList(const linked_ptr<base::DictionaryValue> from,
77 51 base::ListValue* out) {
78 SetStrings(*from, name, out); 52 out->Append(static_cast<Value*>(from->DeepCopy()));
79 } 53 }
80 54
81 } // namespace api_util 55 } // namespace api_util
82 } // namespace extensions 56 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698