AK: Add new failable JsonArray::{append/set} functions

Move all old usages to the more explicit `JsonArray:must_{append/set}`
This commit is contained in:
Cameron Youell 2023-04-17 15:38:27 +10:00 committed by Andreas Kling
parent 3b00636288
commit 8134dccdc7
14 changed files with 28 additions and 25 deletions

View file

@ -61,9 +61,12 @@ public:
[[nodiscard]] JsonValue take(size_t index) { return m_values.take(index); }
void must_append(JsonValue value) { m_values.append(move(value)); }
void must_set(size_t index, JsonValue value) { m_values.insert(index, move(value)); }
void clear() { m_values.clear(); }
void append(JsonValue value) { m_values.append(move(value)); }
void set(size_t index, JsonValue value) { m_values[index] = move(value); }
ErrorOr<void> append(JsonValue value) { return m_values.try_append(move(value)); }
ErrorOr<void> set(size_t index, JsonValue value) { return m_values.try_insert(index, move(value)); }
template<typename Builder>
typename Builder::OutputType serialized() const;

View file

@ -164,7 +164,7 @@ ErrorOr<JsonValue> JsonParser::parse_array()
if (peek() == ']')
break;
auto element = TRY(parse_helper());
array.append(move(element));
array.must_append(move(element));
ignore_while(is_space);
if (peek() == ']')
break;

View file

@ -177,7 +177,7 @@ ErrorOr<void> KeyboardMapperWidget::save_to_file(StringView filename)
sb.append_code_point(values[i]);
JsonValue val(sb.to_deprecated_string());
items.append(move(val));
items.must_append(move(val));
}
map_json.set(name, move(items));
};

View file

@ -202,7 +202,7 @@ ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, Core::File& file,
auto export_worksheet = [&]() -> ErrorOr<void> {
JsonArray array;
for (auto& sheet : workbook.sheets())
array.append(sheet->to_json());
array.must_append(sheet->to_json());
auto file_content = array.to_deprecated_string();
return file.write_until_depleted(file_content.bytes());

View file

@ -542,7 +542,7 @@ JsonObject Sheet::to_json() const
if (!columns_are_standard()) {
auto columns = JsonArray();
for (auto& column : m_columns)
columns.append(column);
columns.must_append(column);
object.set("columns", move(columns));
}
object.set("rows", bottom_right.row + 1);
@ -587,7 +587,7 @@ JsonObject Sheet::to_json() const
fmt_object.set("condition", fmt.condition);
save_format(fmt, fmt_object);
conditional_formats.append(move(fmt_object));
conditional_formats.must_append(move(fmt_object));
}
data.set("conditional_formats", move(conditional_formats));

View file

@ -260,7 +260,7 @@ public:
for (auto& object : Object::all_objects()) {
JsonObject json_object;
object.save_to(json_object);
objects.append(move(json_object));
objects.must_append(move(json_object));
}
response.set("objects", move(objects));
send_response(response);

View file

@ -335,8 +335,8 @@ requires IsBaseOf<Object, T>
[this] { \
auto size = this->getter(); \
JsonArray size_array; \
size_array.append(size.width()); \
size_array.append(size.height()); \
size_array.must_append(size.width()); \
size_array.must_append(size.height()); \
return size_array; \
}, \
{});
@ -379,8 +379,8 @@ requires IsBaseOf<Object, T>
[this] { \
auto size = this->getter(); \
JsonArray size_array; \
size_array.append(size.width()); \
size_array.append(size.height()); \
size_array.must_append(size.width()); \
size_array.must_append(size.height()); \
return size_array; \
}, \
[this](auto& value) { \

View file

@ -67,7 +67,7 @@ bool JsonArrayModel::add(Vector<JsonValue> const&& values)
auto& field_spec = m_fields[i];
obj.set(field_spec.json_field_name, values.at(i));
}
m_array.append(move(obj));
m_array.must_append(move(obj));
did_update();
return true;
}
@ -85,7 +85,7 @@ bool JsonArrayModel::set(int row, Vector<JsonValue>&& values)
obj.set(field_spec.json_field_name, move(values.at(i)));
}
m_array.set(row, move(obj));
m_array.must_set(row, move(obj));
did_update();
return true;
@ -99,7 +99,7 @@ bool JsonArrayModel::remove(int row)
JsonArray new_array;
for (size_t i = 0; i < m_array.size(); ++i)
if (i != (size_t)row)
new_array.append(m_array.at(i));
new_array.must_append(m_array.at(i));
m_array = new_array;

View file

@ -34,7 +34,7 @@ Layout::Layout(Margins initial_margins, int spacing)
} else {
VERIFY_NOT_REACHED();
}
entries_array.append(move(entry_object));
entries_array.must_append(move(entry_object));
}
return entries_array;
});

View file

@ -347,7 +347,7 @@ Response process_capabilities(JsonValue const& parameters)
all_first_match_capabilities = capabilities->as_array();
} else {
// a. If all first match capabilities is undefined, set the value to a JSON List with a single entry of an empty JSON Object.
all_first_match_capabilities.append(JsonObject {});
all_first_match_capabilities.must_append(JsonObject {});
}
// 4. Let validated first match capabilities be an empty JSON List.
@ -360,7 +360,7 @@ Response process_capabilities(JsonValue const& parameters)
auto validated_capabilities = TRY(validate_capabilities(first_match_capabilities));
// b. Append validated capabilities to validated first match capabilities.
validated_first_match_capabilities.append(move(validated_capabilities));
validated_first_match_capabilities.must_append(move(validated_capabilities));
return {};
}));
@ -374,7 +374,7 @@ Response process_capabilities(JsonValue const& parameters)
auto merged = TRY(merge_capabilities(required_capabilities, first_match_capabilities.as_object()));
// b. Append merged to merged capabilities.
merged_capabilities.append(move(merged));
merged_capabilities.must_append(move(merged));
return {};
}));

View file

@ -170,7 +170,7 @@ static ErrorOr<JsonValue, ExecuteScriptResultType> clone_an_object(JS::Realm& re
return ExecuteScriptResultType::JavaScriptError;
auto array = JsonArray {};
for (size_t i = 0; i < length; ++i)
array.append(JsonValue {});
array.must_append(JsonValue {});
return array;
}
// -> Otherwise
@ -203,7 +203,7 @@ static ErrorOr<JsonValue, ExecuteScriptResultType> clone_an_object(JS::Realm& re
[&](JsonArray& array) {
// NOTE: If this was a JS array, only indexed properties would be serialized anyway.
if (name.is_number())
array.set(name.as_number(), cloned_property_result.value());
array.must_set(name.as_number(), cloned_property_result.value());
},
[&](JsonObject& object) {
object.set(name.to_string(), cloned_property_result.value());

View file

@ -1521,7 +1521,7 @@ Messages::WebDriverClient::GetAllCookiesResponse WebDriverConnection::get_all_co
auto serialized_cookie = serialize_cookie(cookie);
// 2. Append serialized cookie to cookies
cookies.append(move(serialized_cookie));
cookies.must_append(move(serialized_cookie));
}
// 5. Return success with data cookies.
@ -1997,7 +1997,7 @@ ErrorOr<JsonArray, Web::WebDriver::Error> WebDriverConnection::find(StartNodeGet
// 8. For each element in elements returned, append the web element reference object for element, to result.
for (size_t i = 0; i < elements->length(); ++i)
result.append(web_element_reference_object(*elements->item(i)));
result.must_append(web_element_reference_object(*elements->item(i)));
// 9. Return success with data result.
return result;

View file

@ -162,7 +162,7 @@ Web::WebDriver::Response Session::get_window_handles() const
// 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
for (auto const& window_handle : m_windows.keys()) {
handles.append(JsonValue(window_handle));
handles.must_append(JsonValue(window_handle));
}
// 3. Return success with data handles.

View file

@ -2367,7 +2367,7 @@ void Shell::save_to(JsonObject& object)
job_object.set("running_time", job_entry.value->timer().elapsed());
job_object.set("command", job_entry.value->cmd());
job_object.set("is_running_in_background", job_entry.value->is_running_in_background());
job_objects.append(move(job_object));
job_objects.must_append(move(job_object));
}
object.set("jobs", move(job_objects));
}