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.h

Issue 851673003: Cleanup: Some simplifications in json_schema_compiler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_dart_tests
Patch Set: Add unit test for GetAsBinary. Created 5 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 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 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/memory/linked_ptr.h" 11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 14
15 namespace json_schema_compiler { 15 namespace json_schema_compiler {
16 16
17 namespace util { 17 namespace util {
18 18
19 // Creates a new item at |out| from |from|[|index|]. These are used by template 19 // Populates the item |out| from the value |from|. These are used by template
20 // specializations of |Get(Optional)ArrayFromList|. 20 // specializations of |Get(Optional)ArrayFromList|.
21 bool GetItemFromList(const base::ListValue& from, int index, int* out); 21 bool PopulateItem(const base::Value& from, int* out);
22 bool GetItemFromList(const base::ListValue& from, int index, bool* out); 22 bool PopulateItem(const base::Value& from, bool* out);
23 bool GetItemFromList(const base::ListValue& from, int index, double* out); 23 bool PopulateItem(const base::Value& from, double* out);
24 bool GetItemFromList(const base::ListValue& from, int index, std::string* out); 24 bool PopulateItem(const base::Value& from, std::string* out);
25 bool GetItemFromList(const base::ListValue& from, 25 bool PopulateStringFromBinary(const base::Value& from, std::string* out);
26 int index, 26 bool PopulateItem(const base::Value& from, linked_ptr<base::Value>* out);
27 linked_ptr<base::Value>* out); 27 bool PopulateItem(const base::Value& from,
28 bool GetItemFromList(const base::ListValue& from, 28 linked_ptr<base::DictionaryValue>* out);
29 int index,
30 linked_ptr<base::DictionaryValue>* out);
31 29
32 // This template is used for types generated by tools/json_schema_compiler. 30 // This template is used for types generated by tools/json_schema_compiler.
33 template<class T> 31 template <class T>
34 bool GetItemFromList(const base::ListValue& from, 32 bool PopulateItem(const base::Value& from, linked_ptr<T>* out) {
35 int index, 33 const base::DictionaryValue* dict = nullptr;
36 linked_ptr<T>* out) { 34 if (!from.GetAsDictionary(&dict))
37 const base::DictionaryValue* dict;
38 if (!from.GetDictionary(index, &dict))
39 return false; 35 return false;
40 scoped_ptr<T> obj(new T()); 36 scoped_ptr<T> obj(new T());
41 if (!T::Populate(*dict, obj.get())) 37 if (!T::Populate(*dict, obj.get()))
42 return false; 38 return false;
43 *out = linked_ptr<T>(obj.release()); 39 *out = linked_ptr<T>(obj.release());
44 return true; 40 return true;
45 } 41 }
46 42
47 // Populates |out| with |list|. Returns false if there is no list at the 43 // Populates |out| with |list|. Returns false if there is no list at the
48 // specified key or if the list has anything other than |T|. 44 // specified key or if the list has anything other than |T|.
49 template <class T> 45 template <class T>
50 bool PopulateArrayFromList( 46 bool PopulateArrayFromList(const base::ListValue& list,
51 const base::ListValue& list, std::vector<T>* out) { 47 bool (*populateItem)(const base::Value&, T*),
48 std::vector<T>* out) {
52 out->clear(); 49 out->clear();
53 T value; 50 T item;
54 for (size_t i = 0; i < list.GetSize(); ++i) { 51 for (size_t i = 0; i < list.GetSize(); ++i) {
55 if (!GetItemFromList(list, i, &value)) 52 const base::Value* value = nullptr;
53 if (!list.Get(i, &value) || !populateItem(*value, &item))
56 return false; 54 return false;
57 out->push_back(value); 55 out->push_back(item);
58 } 56 }
59 57
60 return true; 58 return true;
61 } 59 }
62 60
63 // Creates a new vector containing |list| at |out|. Returns 61 // Creates a new vector containing |list| at |out|. Returns
64 // true on success or if there is nothing at the specified key. Returns false 62 // true on success or if there is nothing at the specified key. Returns false
65 // if anything other than a list of |T| is at the specified key. 63 // if anything other than a list of |T| is at the specified key.
66 template <class T> 64 template <class T>
67 bool PopulateOptionalArrayFromList( 65 bool PopulateOptionalArrayFromList(const base::ListValue& list,
68 const base::ListValue& list, 66 bool (*populateItem)(const base::Value&, T*),
69 scoped_ptr<std::vector<T> >* out) { 67 scoped_ptr<std::vector<T>>* out) {
70 out->reset(new std::vector<T>()); 68 out->reset(new std::vector<T>());
71 T value; 69 if (!PopulateArrayFromList(list, populateItem, out->get())) {
72 for (size_t i = 0; i < list.GetSize(); ++i) { 70 out->reset();
73 if (!GetItemFromList(list, i, &value)) { 71 return false;
74 out->reset();
75 return false;
76 }
77 (*out)->push_back(value);
78 } 72 }
79
80 return true; 73 return true;
81 } 74 }
82 75
83 // Appends a Value newly created from |from| to |out|. These used by template 76 // Appends a Value newly created from |from| to |out|. These used by template
84 // specializations of |Set(Optional)ArrayToList|. 77 // specializations of |Set(Optional)ArrayToList|.
85 void AddItemToList(const int from, base::ListValue* out); 78 void AddItemToList(const int from, base::ListValue* out);
86 void AddItemToList(const bool from, base::ListValue* out); 79 void AddItemToList(const bool from, base::ListValue* out);
87 void AddItemToList(const double from, base::ListValue* out); 80 void AddItemToList(const double from, base::ListValue* out);
88 void AddItemToList(const std::string& from, base::ListValue* out); 81 void AddItemToList(const std::string& from, base::ListValue* out);
89 void AddItemToList(const linked_ptr<base::Value>& from, 82 void AddItemToList(const linked_ptr<base::Value>& from,
90 base::ListValue* out); 83 base::ListValue* out);
91 void AddItemToList(const linked_ptr<base::DictionaryValue>& from, 84 void AddItemToList(const linked_ptr<base::DictionaryValue>& from,
92 base::ListValue* out); 85 base::ListValue* out);
93 86
94 // This template is used for types generated by tools/json_schema_compiler. 87 // This template is used for types generated by tools/json_schema_compiler.
95 template<class T> 88 template<class T>
96 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) { 89 void AddItemToList(const linked_ptr<T>& from, base::ListValue* out) {
97 out->Append(from->ToValue().release()); 90 out->Append(from->ToValue().release());
98 } 91 }
99 92
100 // Set |out| to the the contents of |from|. Requires GetItemFromList to be 93 // Set |out| to the the contents of |from|. Requires PopulateItem to be
101 // implemented for |T|. 94 // implemented for |T|.
102 template <class T> 95 template <class T>
103 void PopulateListFromArray( 96 void PopulateListFromArray(
104 const std::vector<T>& from, 97 const std::vector<T>& from,
105 base::ListValue* out) { 98 base::ListValue* out) {
106 out->Clear(); 99 out->Clear();
107 for (typename std::vector<T>::const_iterator it = from.begin(); 100 for (typename std::vector<T>::const_iterator it = from.begin();
108 it != from.end(); ++it) { 101 it != from.end(); ++it) {
109 AddItemToList(*it, out); 102 AddItemToList(*it, out);
110 } 103 }
111 } 104 }
112 105
113 // Set |out| to the the contents of |from| if |from| is non-NULL. Requires 106 // Set |out| to the the contents of |from| if |from| is not null. Requires
114 // GetItemFromList to be implemented for |T|. 107 // PopulateItem to be implemented for |T|.
115 template <class T> 108 template <class T>
116 void PopulateListFromOptionalArray( 109 void PopulateListFromOptionalArray(
117 const scoped_ptr<std::vector<T> >& from, 110 const scoped_ptr<std::vector<T> >& from,
118 base::ListValue* out) { 111 base::ListValue* out) {
119 if (from.get()) 112 if (from.get())
120 PopulateListFromArray(*from, out); 113 PopulateListFromArray(*from, out);
121 114
122 } 115 }
123 116
124 template <class T> 117 template <class T>
(...skipping 10 matching lines...) Expand all
135 return CreateValueFromArray(*from); 128 return CreateValueFromArray(*from);
136 return scoped_ptr<base::Value>(); 129 return scoped_ptr<base::Value>();
137 } 130 }
138 131
139 std::string ValueTypeToString(base::Value::Type type); 132 std::string ValueTypeToString(base::Value::Type type);
140 133
141 } // namespace util 134 } // namespace util
142 } // namespace json_schema_compiler 135 } // namespace json_schema_compiler
143 136
144 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__ 137 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698