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
3 // found in the LICENSE file.
5 #ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
6 #define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/values.h"
15 namespace json_schema_compiler
{
19 // Populates the item |out| from the value |from|. These are used by template
20 // specializations of |Get(Optional)ArrayFromList|.
21 bool PopulateItem(const base::Value
& from
, linked_ptr
<base::Value
>* out
);
23 bool PopulateItem(const base::Value
& from
, int* out
);
24 bool PopulateItem(const base::Value
& from
, int* out
, base::string16
* error
);
25 bool PopulateItem(const base::Value
& from
, bool* out
);
26 bool PopulateItem(const base::Value
& from
, bool* out
, base::string16
* error
);
27 bool PopulateItem(const base::Value
& from
, double* out
);
28 bool PopulateItem(const base::Value
& from
, double* out
, base::string16
* error
);
29 bool PopulateItem(const base::Value
& from
, std::string
* out
);
30 bool PopulateItem(const base::Value
& from
,
32 base::string16
* error
);
33 bool PopulateItem(const base::Value
& from
, std::vector
<char>* out
);
34 bool PopulateItem(const base::Value
& from
,
35 std::vector
<char>* out
,
36 base::string16
* error
);
37 bool PopulateItem(const base::Value
& from
,
38 linked_ptr
<base::Value
>* out
,
39 base::string16
* error
);
40 bool PopulateItem(const base::Value
& from
, linked_ptr
<base::Value
>* out
);
41 bool PopulateItem(const base::Value
& from
,
42 linked_ptr
<base::DictionaryValue
>* out
);
43 bool PopulateItem(const base::Value
& from
,
44 linked_ptr
<base::DictionaryValue
>* out
,
45 base::string16
* error
);
47 // This template is used for types generated by tools/json_schema_compiler.
49 bool PopulateItem(const base::Value
& from
, linked_ptr
<T
>* out
) {
50 const base::DictionaryValue
* dict
= nullptr;
51 if (!from
.GetAsDictionary(&dict
))
53 scoped_ptr
<T
> obj(new T());
54 if (!T::Populate(*dict
, obj
.get()))
56 *out
= linked_ptr
<T
>(obj
.release());
60 // This template is used for types generated by tools/json_schema_compiler with
61 // error generation enabled.
63 bool PopulateItem(const base::Value
& from
,
65 base::string16
* error
) {
66 const base::DictionaryValue
* dict
= nullptr;
67 if (!from
.GetAsDictionary(&dict
))
69 scoped_ptr
<T
> obj(new T());
70 if (!T::Populate(*dict
, obj
.get(), error
))
72 *out
= linked_ptr
<T
>(obj
.release());
76 // Populates |out| with |list|. Returns false if there is no list at the
77 // specified key or if the list has anything other than |T|.
79 bool PopulateArrayFromList(const base::ListValue
& list
, std::vector
<T
>* out
) {
82 for (const base::Value
* value
: list
) {
83 if (!PopulateItem(*value
, &item
))
91 // Populates |out| with |list|. Returns false and sets |error| if there is no
92 // list at the specified key or if the list has anything other than |T|.
94 bool PopulateArrayFromList(const base::ListValue
& list
,
96 base::string16
* error
) {
99 for (const base::Value
* value
: list
) {
100 if (!PopulateItem(*value
, &item
, error
))
102 out
->push_back(item
);
108 // Creates a new vector containing |list| at |out|. Returns
109 // true on success or if there is nothing at the specified key. Returns false
110 // if anything other than a list of |T| is at the specified key.
112 bool PopulateOptionalArrayFromList(const base::ListValue
& list
,
113 scoped_ptr
<std::vector
<T
>>* out
) {
114 out
->reset(new std::vector
<T
>());
115 if (!PopulateArrayFromList(list
, out
->get())) {
123 bool PopulateOptionalArrayFromList(const base::ListValue
& list
,
124 scoped_ptr
<std::vector
<T
>>* out
,
125 base::string16
* error
) {
126 out
->reset(new std::vector
<T
>());
127 if (!PopulateArrayFromList(list
, out
->get(), error
)) {
134 // Appends a Value newly created from |from| to |out|. These used by template
135 // specializations of |Set(Optional)ArrayToList|.
136 void AddItemToList(const int from
, base::ListValue
* out
);
137 void AddItemToList(const bool from
, base::ListValue
* out
);
138 void AddItemToList(const double from
, base::ListValue
* out
);
139 void AddItemToList(const std::string
& from
, base::ListValue
* out
);
140 void AddItemToList(const std::vector
<char>& from
, base::ListValue
* out
);
141 void AddItemToList(const linked_ptr
<base::Value
>& from
, base::ListValue
* out
);
142 void AddItemToList(const linked_ptr
<base::DictionaryValue
>& from
,
143 base::ListValue
* out
);
145 // This template is used for types generated by tools/json_schema_compiler.
147 void AddItemToList(const linked_ptr
<T
>& from
, base::ListValue
* out
) {
148 out
->Append(from
->ToValue().release());
151 // Set |out| to the the contents of |from|. Requires PopulateItem to be
152 // implemented for |T|.
154 void PopulateListFromArray(const std::vector
<T
>& from
, base::ListValue
* out
) {
156 for (const auto& item
: from
)
157 AddItemToList(item
, out
);
160 // Set |out| to the the contents of |from| if |from| is not null. Requires
161 // PopulateItem to be implemented for |T|.
163 void PopulateListFromOptionalArray(const scoped_ptr
<std::vector
<T
>>& from
,
164 base::ListValue
* out
) {
166 PopulateListFromArray(*from
, out
);
170 scoped_ptr
<base::Value
> CreateValueFromArray(const std::vector
<T
>& from
) {
171 base::ListValue
* list
= new base::ListValue();
172 PopulateListFromArray(from
, list
);
173 return scoped_ptr
<base::Value
>(list
);
177 scoped_ptr
<base::Value
> CreateValueFromOptionalArray(
178 const scoped_ptr
<std::vector
<T
>>& from
) {
180 return CreateValueFromArray(*from
);
181 return scoped_ptr
<base::Value
>();
184 std::string
ValueTypeToString(base::Value::Type type
);
187 } // namespace json_schema_compiler
189 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__