libweave: TypedValueToJson returns derived class not base::Value
No reason to return base class and lose type information.
BUG=brillo:1245
TEST='FEATURES=test emerge-gizmo buffet'
Change-Id: Ia554e902db9ca2ebaaaaf224b3c402f4a915c55c
Reviewed-on: https://chromium-review.googlesource.com/287593
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/libweave/src/commands/schema_utils.cc b/libweave/src/commands/schema_utils.cc
index 9030415..ecfd4da 100644
--- a/libweave/src/commands/schema_utils.cc
+++ b/libweave/src/commands/schema_utils.cc
@@ -54,48 +54,55 @@
} // namespace
// Specializations of TypedValueToJson<T>() for supported C++ types.
-std::unique_ptr<base::Value> TypedValueToJson(bool value,
- chromeos::ErrorPtr* error) {
- return std::unique_ptr<base::Value>(new base::FundamentalValue(value));
+std::unique_ptr<base::FundamentalValue> TypedValueToJson(
+ bool value,
+ chromeos::ErrorPtr* error) {
+ return std::unique_ptr<base::FundamentalValue>(
+ new base::FundamentalValue(value));
}
-std::unique_ptr<base::Value> TypedValueToJson(int value,
- chromeos::ErrorPtr* error) {
- return std::unique_ptr<base::Value>(new base::FundamentalValue(value));
+std::unique_ptr<base::FundamentalValue> TypedValueToJson(
+ int value,
+ chromeos::ErrorPtr* error) {
+ return std::unique_ptr<base::FundamentalValue>(
+ new base::FundamentalValue(value));
}
-std::unique_ptr<base::Value> TypedValueToJson(double value,
- chromeos::ErrorPtr* error) {
- return std::unique_ptr<base::Value>(new base::FundamentalValue(value));
+std::unique_ptr<base::FundamentalValue> TypedValueToJson(
+ double value,
+ chromeos::ErrorPtr* error) {
+ return std::unique_ptr<base::FundamentalValue>(
+ new base::FundamentalValue(value));
}
-std::unique_ptr<base::Value> TypedValueToJson(const std::string& value,
- chromeos::ErrorPtr* error) {
- return std::unique_ptr<base::Value>(new base::StringValue(value));
+std::unique_ptr<base::StringValue> TypedValueToJson(const std::string& value,
+ chromeos::ErrorPtr* error) {
+ return std::unique_ptr<base::StringValue>(new base::StringValue(value));
}
-std::unique_ptr<base::Value> TypedValueToJson(const ValueMap& value,
- chromeos::ErrorPtr* error) {
+std::unique_ptr<base::DictionaryValue> TypedValueToJson(
+ const ValueMap& value,
+ chromeos::ErrorPtr* error) {
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
for (const auto& pair : value) {
auto prop_value = pair.second->ToJson(error);
if (!prop_value)
- return prop_value;
+ return nullptr;
dict->SetWithoutPathExpansion(pair.first, prop_value.release());
}
- return std::move(dict);
+ return dict;
}
-std::unique_ptr<base::Value> TypedValueToJson(const ValueVector& value,
- chromeos::ErrorPtr* error) {
+std::unique_ptr<base::ListValue> TypedValueToJson(const ValueVector& value,
+ chromeos::ErrorPtr* error) {
std::unique_ptr<base::ListValue> list(new base::ListValue);
for (const auto& item : value) {
auto json = item->ToJson(error);
if (!json)
- return std::unique_ptr<base::Value>();
+ return nullptr;
list->Append(json.release());
}
- return std::move(list);
+ return list;
}
bool TypedValueFromJson(const base::Value* value_in,
diff --git a/libweave/src/commands/schema_utils.h b/libweave/src/commands/schema_utils.h
index 2822a3d..e2efbe3 100644
--- a/libweave/src/commands/schema_utils.h
+++ b/libweave/src/commands/schema_utils.h
@@ -56,29 +56,33 @@
// A bunch of helper function to create base::Value for specific C++ classes,
// including vectors of types. These are used in template classes below
// to simplify specialization logic.
-std::unique_ptr<base::Value> TypedValueToJson(bool value,
- chromeos::ErrorPtr* error);
-std::unique_ptr<base::Value> TypedValueToJson(int value,
- chromeos::ErrorPtr* error);
-std::unique_ptr<base::Value> TypedValueToJson(double value,
- chromeos::ErrorPtr* error);
-std::unique_ptr<base::Value> TypedValueToJson(const std::string& value,
- chromeos::ErrorPtr* error);
-std::unique_ptr<base::Value> TypedValueToJson(const ValueMap& value,
- chromeos::ErrorPtr* error);
-std::unique_ptr<base::Value> TypedValueToJson(const ValueVector& value,
- chromeos::ErrorPtr* error);
+std::unique_ptr<base::FundamentalValue> TypedValueToJson(
+ bool value,
+ chromeos::ErrorPtr* error);
+std::unique_ptr<base::FundamentalValue> TypedValueToJson(
+ int value,
+ chromeos::ErrorPtr* error);
+std::unique_ptr<base::FundamentalValue> TypedValueToJson(
+ double value,
+ chromeos::ErrorPtr* error);
+std::unique_ptr<base::StringValue> TypedValueToJson(const std::string& value,
+ chromeos::ErrorPtr* error);
+std::unique_ptr<base::DictionaryValue> TypedValueToJson(
+ const ValueMap& value,
+ chromeos::ErrorPtr* error);
+std::unique_ptr<base::ListValue> TypedValueToJson(const ValueVector& value,
+ chromeos::ErrorPtr* error);
template <typename T>
-std::unique_ptr<base::Value> TypedValueToJson(const std::vector<T>& values,
- chromeos::ErrorPtr* error) {
+std::unique_ptr<base::ListValue> TypedValueToJson(const std::vector<T>& values,
+ chromeos::ErrorPtr* error) {
std::unique_ptr<base::ListValue> list(new base::ListValue);
for (const auto& v : values) {
auto json = TypedValueToJson(v, error);
if (!json)
- return std::unique_ptr<base::Value>();
+ return nullptr;
list->Append(json.release());
}
- return std::move(list);
+ return list;
}
// Similarly to TypedValueToJson() function above, the following overloaded