1*1a96fba6SXin Li // Copyright 2015 The Android Open Source Project
2*1a96fba6SXin Li //
3*1a96fba6SXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*1a96fba6SXin Li // you may not use this file except in compliance with the License.
5*1a96fba6SXin Li // You may obtain a copy of the License at
6*1a96fba6SXin Li //
7*1a96fba6SXin Li // http://www.apache.org/licenses/LICENSE-2.0
8*1a96fba6SXin Li //
9*1a96fba6SXin Li // Unless required by applicable law or agreed to in writing, software
10*1a96fba6SXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*1a96fba6SXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*1a96fba6SXin Li // See the License for the specific language governing permissions and
13*1a96fba6SXin Li // limitations under the License.
14*1a96fba6SXin Li
15*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_VALUE_CONVERSION_H_
16*1a96fba6SXin Li #define LIBBRILLO_BRILLO_VALUE_CONVERSION_H_
17*1a96fba6SXin Li
18*1a96fba6SXin Li // This file provides a set of helper functions to convert between base::Value
19*1a96fba6SXin Li // and native types. Apart from handling standard types such as 'int' and
20*1a96fba6SXin Li // 'std::string' it also provides conversion to/from std::vector<T> (which
21*1a96fba6SXin Li // converts to Base::listValue) and std::map<std::string, T> (convertible to
22*1a96fba6SXin Li // base::DictionaryValue).
23*1a96fba6SXin Li
24*1a96fba6SXin Li #include <map>
25*1a96fba6SXin Li #include <memory>
26*1a96fba6SXin Li #include <string>
27*1a96fba6SXin Li #include <utility>
28*1a96fba6SXin Li #include <vector>
29*1a96fba6SXin Li
30*1a96fba6SXin Li #include <base/values.h>
31*1a96fba6SXin Li #include <brillo/brillo_export.h>
32*1a96fba6SXin Li
33*1a96fba6SXin Li namespace brillo {
34*1a96fba6SXin Li
FromValue(const base::Value & in_value,bool * out_value)35*1a96fba6SXin Li inline bool FromValue(const base::Value& in_value, bool* out_value) {
36*1a96fba6SXin Li return in_value.GetAsBoolean(out_value);
37*1a96fba6SXin Li }
38*1a96fba6SXin Li
FromValue(const base::Value & in_value,int * out_value)39*1a96fba6SXin Li inline bool FromValue(const base::Value& in_value, int* out_value) {
40*1a96fba6SXin Li return in_value.GetAsInteger(out_value);
41*1a96fba6SXin Li }
42*1a96fba6SXin Li
FromValue(const base::Value & in_value,double * out_value)43*1a96fba6SXin Li inline bool FromValue(const base::Value& in_value, double* out_value) {
44*1a96fba6SXin Li return in_value.GetAsDouble(out_value);
45*1a96fba6SXin Li }
46*1a96fba6SXin Li
FromValue(const base::Value & in_value,std::string * out_value)47*1a96fba6SXin Li inline bool FromValue(const base::Value& in_value, std::string* out_value) {
48*1a96fba6SXin Li return in_value.GetAsString(out_value);
49*1a96fba6SXin Li }
50*1a96fba6SXin Li
FromValue(const base::Value & in_value,const base::ListValue ** out_value)51*1a96fba6SXin Li inline bool FromValue(const base::Value& in_value,
52*1a96fba6SXin Li const base::ListValue** out_value) {
53*1a96fba6SXin Li return in_value.GetAsList(out_value);
54*1a96fba6SXin Li }
55*1a96fba6SXin Li
FromValue(const base::Value & in_value,const base::DictionaryValue ** out_value)56*1a96fba6SXin Li inline bool FromValue(const base::Value& in_value,
57*1a96fba6SXin Li const base::DictionaryValue** out_value) {
58*1a96fba6SXin Li return in_value.GetAsDictionary(out_value);
59*1a96fba6SXin Li }
60*1a96fba6SXin Li
61*1a96fba6SXin Li BRILLO_EXPORT bool FromValue(const base::Value& in_value,
62*1a96fba6SXin Li std::unique_ptr<base::ListValue>* out_value);
63*1a96fba6SXin Li BRILLO_EXPORT bool FromValue(const base::Value& in_value,
64*1a96fba6SXin Li std::unique_ptr<base::DictionaryValue>* out_value);
65*1a96fba6SXin Li
66*1a96fba6SXin Li template <typename T, typename Pred, typename Alloc>
67*1a96fba6SXin Li bool FromValue(const base::Value& in_value,
68*1a96fba6SXin Li std::map<std::string, T, Pred, Alloc>* out_value);
69*1a96fba6SXin Li
70*1a96fba6SXin Li template <typename T, typename Alloc>
FromValue(const base::Value & in_value,std::vector<T,Alloc> * out_value)71*1a96fba6SXin Li bool FromValue(const base::Value& in_value, std::vector<T, Alloc>* out_value) {
72*1a96fba6SXin Li const base::ListValue* list = nullptr;
73*1a96fba6SXin Li if (!in_value.GetAsList(&list))
74*1a96fba6SXin Li return false;
75*1a96fba6SXin Li out_value->clear();
76*1a96fba6SXin Li out_value->reserve(list->GetSize());
77*1a96fba6SXin Li for (const base::Value& item : *list) {
78*1a96fba6SXin Li T value{};
79*1a96fba6SXin Li if (!FromValue(item, &value))
80*1a96fba6SXin Li return false;
81*1a96fba6SXin Li out_value->push_back(std::move(value));
82*1a96fba6SXin Li }
83*1a96fba6SXin Li return true;
84*1a96fba6SXin Li }
85*1a96fba6SXin Li
86*1a96fba6SXin Li template <typename T, typename Pred, typename Alloc>
FromValue(const base::Value & in_value,std::map<std::string,T,Pred,Alloc> * out_value)87*1a96fba6SXin Li bool FromValue(const base::Value& in_value,
88*1a96fba6SXin Li std::map<std::string, T, Pred, Alloc>* out_value) {
89*1a96fba6SXin Li const base::DictionaryValue* dict = nullptr;
90*1a96fba6SXin Li if (!in_value.GetAsDictionary(&dict))
91*1a96fba6SXin Li return false;
92*1a96fba6SXin Li out_value->clear();
93*1a96fba6SXin Li for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) {
94*1a96fba6SXin Li if (!FromValue(it.value(), &(*out_value)[it.key()]))
95*1a96fba6SXin Li return false;
96*1a96fba6SXin Li }
97*1a96fba6SXin Li return true;
98*1a96fba6SXin Li }
99*1a96fba6SXin Li
100*1a96fba6SXin Li template <typename T>
FromValue(const base::Value & value)101*1a96fba6SXin Li T FromValue(const base::Value& value) {
102*1a96fba6SXin Li T out_value{};
103*1a96fba6SXin Li CHECK(FromValue(value, &out_value));
104*1a96fba6SXin Li return out_value;
105*1a96fba6SXin Li }
106*1a96fba6SXin Li
107*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(int value);
108*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(bool value);
109*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(double value);
110*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(const std::string& value);
111*1a96fba6SXin Li // Implicit conversion of char* to 'bool' has precedence over the user-defined
112*1a96fba6SXin Li // std::string conversion. Override this behavior explicitly.
113*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<base::Value> ToValue(const char* value);
114*1a96fba6SXin Li
115*1a96fba6SXin Li template <typename T, typename Pred, typename Alloc>
116*1a96fba6SXin Li std::unique_ptr<base::Value> ToValue(
117*1a96fba6SXin Li const std::map<std::string, T, Pred, Alloc>& dictionary);
118*1a96fba6SXin Li
119*1a96fba6SXin Li template <typename T, typename Alloc>
ToValue(const std::vector<T,Alloc> & list)120*1a96fba6SXin Li std::unique_ptr<base::Value> ToValue(const std::vector<T, Alloc>& list) {
121*1a96fba6SXin Li std::unique_ptr<base::ListValue> result{new base::ListValue};
122*1a96fba6SXin Li for (const auto& value : list)
123*1a96fba6SXin Li result->Append(ToValue(value));
124*1a96fba6SXin Li return std::move(result);
125*1a96fba6SXin Li }
126*1a96fba6SXin Li
127*1a96fba6SXin Li template <typename T, typename Pred, typename Alloc>
ToValue(const std::map<std::string,T,Pred,Alloc> & dictionary)128*1a96fba6SXin Li std::unique_ptr<base::Value> ToValue(
129*1a96fba6SXin Li const std::map<std::string, T, Pred, Alloc>& dictionary) {
130*1a96fba6SXin Li std::unique_ptr<base::DictionaryValue> result{new base::DictionaryValue};
131*1a96fba6SXin Li for (const auto& pair : dictionary)
132*1a96fba6SXin Li result->Set(pair.first, ToValue(pair.second));
133*1a96fba6SXin Li return std::move(result);
134*1a96fba6SXin Li }
135*1a96fba6SXin Li
136*1a96fba6SXin Li } // namespace brillo
137*1a96fba6SXin Li
138*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_VALUE_CONVERSION_H_
139