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
, int* out
);
22 bool PopulateItem(const base::Value
& from
, bool* out
);
23 bool PopulateItem(const base::Value
& from
, double* out
);
24 bool PopulateItem(const base::Value
& from
, std::string
* out
);
25 bool PopulateItem(const base::Value
& from
, std::vector
<char>* out
);
26 bool PopulateItem(const base::Value
& from
, linked_ptr
<base::Value
>* out
);
27 bool PopulateItem(const base::Value
& from
,
28 linked_ptr
<base::DictionaryValue
>* out
);
30 // This template is used for types generated by tools/json_schema_compiler.
32 bool PopulateItem(const base::Value
& from
, linked_ptr
<T
>* out
) {
33 const base::DictionaryValue
* dict
= nullptr;
34 if (!from
.GetAsDictionary(&dict
))
36 scoped_ptr
<T
> obj(new T());
37 if (!T::Populate(*dict
, obj
.get()))
39 *out
= linked_ptr
<T
>(obj
.release());
43 // Populates |out| with |list|. Returns false if there is no list at the
44 // specified key or if the list has anything other than |T|.
46 bool PopulateArrayFromList(const base::ListValue
& list
, std::vector
<T
>* out
) {
49 for (const base::Value
* value
: list
) {
50 if (!PopulateItem(*value
, &item
))
58 // Creates a new vector containing |list| at |out|. Returns
59 // true on success or if there is nothing at the specified key. Returns false
60 // if anything other than a list of |T| is at the specified key.
62 bool PopulateOptionalArrayFromList(const base::ListValue
& list
,
63 scoped_ptr
<std::vector
<T
>>* out
) {
64 out
->reset(new std::vector
<T
>());
65 if (!PopulateArrayFromList(list
, out
->get())) {
72 // Appends a Value newly created from |from| to |out|. These used by template
73 // specializations of |Set(Optional)ArrayToList|.
74 void AddItemToList(const int from
, base::ListValue
* out
);
75 void AddItemToList(const bool from
, base::ListValue
* out
);
76 void AddItemToList(const double from
, base::ListValue
* out
);
77 void AddItemToList(const std::string
& from
, base::ListValue
* out
);
78 void AddItemToList(const std::vector
<char>& from
, base::ListValue
* out
);
79 void AddItemToList(const linked_ptr
<base::Value
>& from
, base::ListValue
* out
);
80 void AddItemToList(const linked_ptr
<base::DictionaryValue
>& from
,
81 base::ListValue
* out
);
83 // This template is used for types generated by tools/json_schema_compiler.
85 void AddItemToList(const linked_ptr
<T
>& from
, base::ListValue
* out
) {
86 out
->Append(from
->ToValue().release());
89 // Set |out| to the the contents of |from|. Requires PopulateItem to be
90 // implemented for |T|.
92 void PopulateListFromArray(const std::vector
<T
>& from
, base::ListValue
* out
) {
94 for (const auto& item
: from
)
95 AddItemToList(item
, out
);
98 // Set |out| to the the contents of |from| if |from| is not null. Requires
99 // PopulateItem to be implemented for |T|.
101 void PopulateListFromOptionalArray(const scoped_ptr
<std::vector
<T
>>& from
,
102 base::ListValue
* out
) {
104 PopulateListFromArray(*from
, out
);
108 scoped_ptr
<base::Value
> CreateValueFromArray(const std::vector
<T
>& from
) {
109 base::ListValue
* list
= new base::ListValue();
110 PopulateListFromArray(from
, list
);
111 return scoped_ptr
<base::Value
>(list
);
115 scoped_ptr
<base::Value
> CreateValueFromOptionalArray(
116 const scoped_ptr
<std::vector
<T
>>& from
) {
118 return CreateValueFromArray(*from
);
119 return scoped_ptr
<base::Value
>();
122 std::string
ValueTypeToString(base::Value::Type type
);
125 } // namespace json_schema_compiler
127 #endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H__