Migrate dbus/values_util.cc to new base::Value APIs.
Bug: 1187001
Change-Id: I53858d3f8ac497beb2751d1a7e25f389cfd6a070
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4060621
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Commit-Queue: Matt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1076573}
NOKEYCHECK=True
GitOrigin-RevId: d5b6a14df2ea7541597277ea9a30f65e7f3e8fd2
diff --git a/values_util.cc b/values_util.cc
index 47e4a20..b34595e 100644
--- a/values_util.cc
+++ b/values_util.cc
@@ -24,21 +24,19 @@
}
// Pops values from |reader| and appends them to |list_value|.
-bool PopListElements(MessageReader* reader, base::Value* list_value) {
- DCHECK(list_value->is_list());
+bool PopListElements(MessageReader* reader, base::Value::List& list_value) {
while (reader->HasMoreData()) {
base::Value element_value = PopDataAsValue(reader);
if (element_value.is_none())
return false;
- list_value->Append(std::move(element_value));
+ list_value.Append(std::move(element_value));
}
return true;
}
// Pops dict-entries from |reader| and sets them to |dictionary_value|
bool PopDictionaryEntries(MessageReader* reader,
- base::Value* dictionary_value) {
- DCHECK(dictionary_value->is_dict());
+ base::Value::Dict& dictionary_value) {
while (reader->HasMoreData()) {
DCHECK_EQ(Message::DICT_ENTRY, reader->GetDataType());
MessageReader entry_reader(nullptr);
@@ -62,7 +60,7 @@
base::Value value = PopDataAsValue(&entry_reader);
if (value.is_none())
return false;
- dictionary_value->SetKey(key_string, std::move(value));
+ dictionary_value.Set(key_string, std::move(value));
}
return true;
}
@@ -185,13 +183,13 @@
// Value with type base::Value::Type::DICTIONARY, otherwise create a
// Value with type base::Value::Type::LIST.
if (sub_reader.GetDataType() == Message::DICT_ENTRY) {
- auto dictionary_value = base::Value(base::Value::Type::DICTIONARY);
- if (PopDictionaryEntries(&sub_reader, &dictionary_value))
- result = std::move(dictionary_value);
+ base::Value::Dict dictionary_value;
+ if (PopDictionaryEntries(&sub_reader, dictionary_value))
+ result = base::Value(std::move(dictionary_value));
} else {
- auto list_value = base::Value(base::Value::Type::LIST);
- if (PopListElements(&sub_reader, &list_value))
- result = std::move(list_value);
+ base::Value::List list_value;
+ if (PopListElements(&sub_reader, list_value))
+ result = base::Value(std::move(list_value));
}
}
break;
@@ -199,9 +197,9 @@
case Message::STRUCT: {
MessageReader sub_reader(nullptr);
if (reader->PopStruct(&sub_reader)) {
- auto list_value = base::Value(base::Value::Type::LIST);
- if (PopListElements(&sub_reader, &list_value))
- result = std::move(list_value);
+ base::Value::List list_value;
+ if (PopListElements(&sub_reader, list_value))
+ result = base::Value(std::move(list_value));
}
break;
}
diff --git a/values_util_unittest.cc b/values_util_unittest.cc
index 120f124..a94b7d2 100644
--- a/values_util_unittest.cc
+++ b/values_util_unittest.cc
@@ -175,7 +175,7 @@
writer.CloseContainer(&sub_writer);
// Create the expected value.
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
for (size_t i = 0; i != data.size(); ++i)
list_value.Append(data[i]);
@@ -198,7 +198,7 @@
writer.AppendArrayOfStrings(data);
// Create the expected value.
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
for (size_t i = 0; i != data.size(); ++i)
list_value.Append(data[i]);
@@ -226,7 +226,7 @@
writer.CloseContainer(&sub_writer);
// Create the expected value.
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
list_value.Append(kBoolValue);
list_value.Append(kInt32Value);
list_value.Append(kDoubleValue);
@@ -273,11 +273,11 @@
writer.CloseContainer(&sub_writer);
// Create the expected value.
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
- dictionary_value.SetBoolKey(kKey1, kBoolValue);
- dictionary_value.SetIntKey(kKey2, kInt32Value);
- dictionary_value.SetDoubleKey(kKey3, kDoubleValue);
- dictionary_value.SetStringKey(kKey4, kStringValue);
+ base::Value::Dict dictionary_value;
+ dictionary_value.Set(kKey1, kBoolValue);
+ dictionary_value.Set(kKey2, kInt32Value);
+ dictionary_value.Set(kKey3, kDoubleValue);
+ dictionary_value.Set(kKey4, kStringValue);
// Pop a dictinoary.
MessageReader reader(response.get());
@@ -314,10 +314,10 @@
writer.CloseContainer(&sub_writer);
// Create the expected value.
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
- dictionary_value.SetKey(kKey1, base::Value(kBoolValue));
- dictionary_value.SetKey(kKey2, base::Value(kInt32Value));
- dictionary_value.SetKey(kKey3, base::Value(kDoubleValue));
+ base::Value::Dict dictionary_value;
+ dictionary_value.Set(kKey1, kBoolValue);
+ dictionary_value.Set(kKey2, kInt32Value);
+ dictionary_value.Set(kKey3, kDoubleValue);
// Pop a dictinoary.
MessageReader reader(response.get());
@@ -349,11 +349,11 @@
writer.CloseContainer(&sub_writer);
// Create the expected value.
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
+ base::Value::Dict dictionary_value;
for (size_t i = 0; i != values.size(); ++i) {
std::string key_string;
base::JSONWriter::Write(base::Value(keys[i]), &key_string);
- dictionary_value.SetKey(key_string, base::Value(values[i]));
+ dictionary_value.Set(key_string, values[i]);
}
// Pop a dictionary.
@@ -493,21 +493,21 @@
const double kDoubleValue = 4.9;
const std::string kStringValue = "fifty";
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
list_value.Append(kBoolValue);
list_value.Append(kInt32Value);
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
- dictionary_value.SetBoolKey(kKey1, kBoolValue);
- dictionary_value.SetIntKey(kKey2, kDoubleValue);
+ base::Value::Dict dictionary_value;
+ dictionary_value.Set(kKey1, kBoolValue);
+ dictionary_value.Set(kKey2, kDoubleValue);
- base::Value test_dictionary(base::Value::Type::DICTIONARY);
- test_dictionary.SetBoolKey(kKey1, kBoolValue);
- test_dictionary.SetIntKey(kKey2, kInt32Value);
- test_dictionary.SetDoubleKey(kKey3, kDoubleValue);
- test_dictionary.SetStringKey(kKey4, kStringValue);
- test_dictionary.SetKey(kKey5, std::move(list_value));
- test_dictionary.SetKey(kKey6, std::move(dictionary_value));
+ base::Value::Dict test_dictionary;
+ test_dictionary.Set(kKey1, kBoolValue);
+ test_dictionary.Set(kKey2, kInt32Value);
+ test_dictionary.Set(kKey3, kDoubleValue);
+ test_dictionary.Set(kKey4, kStringValue);
+ test_dictionary.Set(kKey5, std::move(list_value));
+ test_dictionary.Set(kKey6, std::move(dictionary_value));
std::unique_ptr<Response> response(Response::CreateEmpty());
MessageWriter writer(response.get());
@@ -540,21 +540,21 @@
const double kDoubleValue = 4.9;
const std::string kStringValue = "fifty";
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
list_value.Append(kBoolValue);
list_value.Append(kInt32Value);
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
- dictionary_value.SetBoolKey(kKey1, kBoolValue);
- dictionary_value.SetIntKey(kKey2, kDoubleValue);
+ base::Value::Dict dictionary_value;
+ dictionary_value.Set(kKey1, kBoolValue);
+ dictionary_value.Set(kKey2, kDoubleValue);
- base::Value test_dictionary(base::Value::Type::DICTIONARY);
- test_dictionary.SetBoolKey(kKey1, kBoolValue);
- test_dictionary.SetIntKey(kKey2, kInt32Value);
- test_dictionary.SetDoubleKey(kKey3, kDoubleValue);
- test_dictionary.SetStringKey(kKey4, kStringValue);
- test_dictionary.SetKey(kKey5, std::move(list_value));
- test_dictionary.SetKey(kKey6, std::move(dictionary_value));
+ base::Value::Dict test_dictionary;
+ test_dictionary.Set(kKey1, kBoolValue);
+ test_dictionary.Set(kKey2, kInt32Value);
+ test_dictionary.Set(kKey3, kDoubleValue);
+ test_dictionary.Set(kKey4, kStringValue);
+ test_dictionary.Set(kKey5, std::move(list_value));
+ test_dictionary.Set(kKey6, std::move(dictionary_value));
std::unique_ptr<Response> response(Response::CreateEmpty());
MessageWriter writer(response.get());
@@ -583,15 +583,15 @@
const double kDoubleValue = 4.9;
const std::string kStringValue = "fifty";
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
list_value.Append(kBoolValue);
list_value.Append(kInt32Value);
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
- dictionary_value.SetBoolPath(kKey1, kBoolValue);
- dictionary_value.SetIntPath(kKey2, kDoubleValue);
+ base::Value::Dict dictionary_value;
+ dictionary_value.Set(kKey1, kBoolValue);
+ dictionary_value.Set(kKey2, kDoubleValue);
- base::Value test_list(base::Value::Type::LIST);
+ base::Value::List test_list;
test_list.Append(kBoolValue);
test_list.Append(kInt32Value);
test_list.Append(kDoubleValue);
@@ -626,15 +626,15 @@
const double kDoubleValue = 4.9;
const std::string kStringValue = "fifty";
- base::Value list_value(base::Value::Type::LIST);
+ base::Value::List list_value;
list_value.Append(kBoolValue);
list_value.Append(kInt32Value);
- base::Value dictionary_value(base::Value::Type::DICTIONARY);
- dictionary_value.SetBoolPath(kKey1, kBoolValue);
- dictionary_value.SetIntPath(kKey2, kDoubleValue);
+ base::Value::Dict dictionary_value;
+ dictionary_value.Set(kKey1, kBoolValue);
+ dictionary_value.Set(kKey2, kDoubleValue);
- base::Value test_list(base::Value::Type::LIST);
+ base::Value::List test_list;
test_list.Append(kBoolValue);
test_list.Append(kInt32Value);
test_list.Append(kDoubleValue);