blob: 28a8cfd67a0087d0bb0b5fa9e1bf6b9de4a99ef4 [file] [log] [blame]
[email protected]15f08dd2012-01-27 07:29:481// 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.
4
dchenga500b692016-04-08 19:55:425#ifndef TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
6#define TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_
[email protected]15f08dd2012-01-27 07:29:487
dchenga500b692016-04-08 19:55:428#include <memory>
[email protected]15f08dd2012-01-27 07:29:489#include <string>
dchenga500b692016-04-08 19:55:4210#include <utility>
[email protected]15f08dd2012-01-27 07:29:4811#include <vector>
12
[email protected]cfe484902012-02-15 14:52:3213#include "base/values.h"
[email protected]15f08dd2012-01-27 07:29:4814
15namespace json_schema_compiler {
[email protected]feba21e2012-03-02 15:05:2716
[email protected]15f08dd2012-01-27 07:29:4817namespace util {
18
pneubeckde496d52015-01-19 08:49:5319// Populates the item |out| from the value |from|. These are used by template
[email protected]cfe484902012-02-15 14:52:3220// specializations of |Get(Optional)ArrayFromList|.
dchenga500b692016-04-08 19:55:4221bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
reillyg89a17f72015-05-06 02:34:2722
pneubeckde496d52015-01-19 08:49:5323bool PopulateItem(const base::Value& from, int* out);
reillyg89a17f72015-05-06 02:34:2724bool PopulateItem(const base::Value& from, int* out, base::string16* error);
pneubeckde496d52015-01-19 08:49:5325bool PopulateItem(const base::Value& from, bool* out);
reillyg89a17f72015-05-06 02:34:2726bool PopulateItem(const base::Value& from, bool* out, base::string16* error);
pneubeckde496d52015-01-19 08:49:5327bool PopulateItem(const base::Value& from, double* out);
reillyg89a17f72015-05-06 02:34:2728bool PopulateItem(const base::Value& from, double* out, base::string16* error);
pneubeckde496d52015-01-19 08:49:5329bool PopulateItem(const base::Value& from, std::string* out);
reillyg89a17f72015-05-06 02:34:2730bool PopulateItem(const base::Value& from,
31 std::string* out,
32 base::string16* error);
jdoerrie9970f20e2018-07-20 21:41:1833bool PopulateItem(const base::Value& from, std::vector<uint8_t>* out);
reillyg89a17f72015-05-06 02:34:2734bool PopulateItem(const base::Value& from,
jdoerrie9970f20e2018-07-20 21:41:1835 std::vector<uint8_t>* out,
reillyg89a17f72015-05-06 02:34:2736 base::string16* error);
37bool PopulateItem(const base::Value& from,
dchenga500b692016-04-08 19:55:4238 std::unique_ptr<base::Value>* out,
reillyg89a17f72015-05-06 02:34:2739 base::string16* error);
dchenga500b692016-04-08 19:55:4240bool PopulateItem(const base::Value& from, std::unique_ptr<base::Value>* out);
[email protected]cfe484902012-02-15 14:52:3241
42// This template is used for types generated by tools/json_schema_compiler.
pneubeckde496d52015-01-19 08:49:5343template <class T>
rdevlin.cronin63ef43c02016-03-22 22:11:0044bool PopulateItem(const base::Value& from, T* out) {
45 const base::DictionaryValue* dict = nullptr;
46 if (!from.GetAsDictionary(&dict))
47 return false;
48 T obj;
49 if (!T::Populate(*dict, &obj))
50 return false;
51 *out = std::move(obj);
52 return true;
53}
54
reillyg89a17f72015-05-06 02:34:2755// This template is used for types generated by tools/json_schema_compiler with
56// error generation enabled.
57template <class T>
rdevlin.cronin8b7c7102016-03-29 20:27:5458bool PopulateItem(const base::Value& from, T* out, base::string16* error) {
rdevlin.cronin8b7c7102016-03-29 20:27:5459 T obj;
Utkarsh Patankar96f049d2020-07-25 03:12:0760 if (!T::Populate(from, &obj, error))
rdevlin.cronin8b7c7102016-03-29 20:27:5461 return false;
62 *out = std::move(obj);
63 return true;
64}
65
[email protected]cfe484902012-02-15 14:52:3266// Populates |out| with |list|. Returns false if there is no list at the
67// specified key or if the list has anything other than |T|.
68template <class T>
pneubeckde496d52015-01-19 08:49:5369bool PopulateArrayFromList(const base::ListValue& list, std::vector<T>* out) {
[email protected]cfe484902012-02-15 14:52:3270 out->clear();
pneubeckde496d52015-01-19 08:49:5371 T item;
dchengcb60e702016-05-25 18:30:4772 for (const auto& value : list) {
jdoerriea5676c62017-04-11 18:09:1473 if (!PopulateItem(value, &item))
[email protected]cfe484902012-02-15 14:52:3274 return false;
rdevlin.cronin63ef43c02016-03-22 22:11:0075 // T might not be movable, but in that case it should be copyable, and this
76 // will still work.
77 out->push_back(std::move(item));
[email protected]cfe484902012-02-15 14:52:3278 }
79
80 return true;
81}
82
reillyg89a17f72015-05-06 02:34:2783// Populates |out| with |list|. Returns false and sets |error| if there is no
84// list at the specified key or if the list has anything other than |T|.
85template <class T>
86bool PopulateArrayFromList(const base::ListValue& list,
87 std::vector<T>* out,
88 base::string16* error) {
89 out->clear();
90 T item;
dchengcb60e702016-05-25 18:30:4791 for (const auto& value : list) {
jdoerriea5676c62017-04-11 18:09:1492 if (!PopulateItem(value, &item, error))
reillyg89a17f72015-05-06 02:34:2793 return false;
rdevlin.cronin8b7c7102016-03-29 20:27:5494 out->push_back(std::move(item));
reillyg89a17f72015-05-06 02:34:2795 }
96
97 return true;
98}
99
[email protected]cfe484902012-02-15 14:52:32100// Creates a new vector containing |list| at |out|. Returns
[email protected]15f08dd2012-01-27 07:29:48101// true on success or if there is nothing at the specified key. Returns false
[email protected]cfe484902012-02-15 14:52:32102// if anything other than a list of |T| is at the specified key.
103template <class T>
pneubeckde496d52015-01-19 08:49:53104bool PopulateOptionalArrayFromList(const base::ListValue& list,
dchenga500b692016-04-08 19:55:42105 std::unique_ptr<std::vector<T>>* out) {
[email protected]cfe484902012-02-15 14:52:32106 out->reset(new std::vector<T>());
pneubeckde496d52015-01-19 08:49:53107 if (!PopulateArrayFromList(list, out->get())) {
108 out->reset();
109 return false;
[email protected]cfe484902012-02-15 14:52:32110 }
[email protected]cfe484902012-02-15 14:52:32111 return true;
112}
113
reillyg89a17f72015-05-06 02:34:27114template <class T>
115bool PopulateOptionalArrayFromList(const base::ListValue& list,
dchenga500b692016-04-08 19:55:42116 std::unique_ptr<std::vector<T>>* out,
reillyg89a17f72015-05-06 02:34:27117 base::string16* error) {
118 out->reset(new std::vector<T>());
119 if (!PopulateArrayFromList(list, out->get(), error)) {
120 out->reset();
121 return false;
122 }
123 return true;
124}
125
[email protected]cfe484902012-02-15 14:52:32126// Appends a Value newly created from |from| to |out|. These used by template
127// specializations of |Set(Optional)ArrayToList|.
128void AddItemToList(const int from, base::ListValue* out);
129void AddItemToList(const bool from, base::ListValue* out);
130void AddItemToList(const double from, base::ListValue* out);
131void AddItemToList(const std::string& from, base::ListValue* out);
jdoerrie9970f20e2018-07-20 21:41:18132void AddItemToList(const std::vector<uint8_t>& from, base::ListValue* out);
dchenga500b692016-04-08 19:55:42133void AddItemToList(const std::unique_ptr<base::Value>& from,
134 base::ListValue* out);
135void AddItemToList(const std::unique_ptr<base::DictionaryValue>& from,
[email protected]242d5e7a2013-01-17 06:50:31136 base::ListValue* out);
[email protected]cfe484902012-02-15 14:52:32137
138// This template is used for types generated by tools/json_schema_compiler.
pneubeckde496d52015-01-19 08:49:53139template <class T>
dchenga500b692016-04-08 19:55:42140void AddItemToList(const std::unique_ptr<T>& from, base::ListValue* out) {
rdevlin.cronin3d8ce9c2016-04-04 21:14:16141 out->Append(from->ToValue());
[email protected]cfe484902012-02-15 14:52:32142}
143
rdevlin.cronin63ef43c02016-03-22 22:11:00144// This template is used for types generated by tools/json_schema_compiler.
145template <class T>
146void AddItemToList(const T& from, base::ListValue* out) {
147 out->Append(from.ToValue());
148}
149
pneubeckde496d52015-01-19 08:49:53150// Set |out| to the the contents of |from|. Requires PopulateItem to be
[email protected]cfe484902012-02-15 14:52:32151// implemented for |T|.
152template <class T>
pneubeckde496d52015-01-19 08:49:53153void PopulateListFromArray(const std::vector<T>& from, base::ListValue* out) {
[email protected]cfe484902012-02-15 14:52:32154 out->Clear();
rdevlin.cronin63ef43c02016-03-22 22:11:00155 for (const T& item : from)
pneubeckde496d52015-01-19 08:49:53156 AddItemToList(item, out);
[email protected]cfe484902012-02-15 14:52:32157}
158
pneubeckde496d52015-01-19 08:49:53159// Set |out| to the the contents of |from| if |from| is not null. Requires
160// PopulateItem to be implemented for |T|.
[email protected]cfe484902012-02-15 14:52:32161template <class T>
dchenga500b692016-04-08 19:55:42162void PopulateListFromOptionalArray(const std::unique_ptr<std::vector<T>>& from,
pneubeckde496d52015-01-19 08:49:53163 base::ListValue* out) {
dchenga500b692016-04-08 19:55:42164 if (from)
[email protected]cfe484902012-02-15 14:52:32165 PopulateListFromArray(*from, out);
[email protected]cfe484902012-02-15 14:52:32166}
167
[email protected]cfe484902012-02-15 14:52:32168template <class T>
dchenga500b692016-04-08 19:55:42169std::unique_ptr<base::Value> CreateValueFromArray(const std::vector<T>& from) {
170 std::unique_ptr<base::ListValue> list(new base::ListValue());
171 PopulateListFromArray(from, list.get());
172 return std::move(list);
[email protected]cfe484902012-02-15 14:52:32173}
174
[email protected]cfe484902012-02-15 14:52:32175template <class T>
dchenga500b692016-04-08 19:55:42176std::unique_ptr<base::Value> CreateValueFromOptionalArray(
177 const std::unique_ptr<std::vector<T>>& from) {
thestige7615d6c2016-07-19 19:43:46178 return from ? CreateValueFromArray(*from) : nullptr;
[email protected]cfe484902012-02-15 14:52:32179}
180
[email protected]feba21e2012-03-02 15:05:27181} // namespace util
182} // namespace json_schema_compiler
[email protected]15f08dd2012-01-27 07:29:48183
dchenga500b692016-04-08 19:55:42184#endif // TOOLS_JSON_SCHEMA_COMPILER_UTIL_H_