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:
parent
3b00636288
commit
8134dccdc7
Notes:
sideshowbarker
2024-07-17 22:01:16 +09:00
Author: https://github.com/cammo1123 Commit: https://github.com/SerenityOS/serenity/commit/8134dccdc7 Pull-request: https://github.com/SerenityOS/serenity/pull/18409 Reviewed-by: https://github.com/gmta Reviewed-by: https://github.com/krkk
14 changed files with 28 additions and 25 deletions
|
@ -61,9 +61,12 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] JsonValue take(size_t index) { return m_values.take(index); }
|
[[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 clear() { m_values.clear(); }
|
||||||
void append(JsonValue value) { m_values.append(move(value)); }
|
ErrorOr<void> append(JsonValue value) { return m_values.try_append(move(value)); }
|
||||||
void set(size_t index, JsonValue value) { m_values[index] = move(value); }
|
ErrorOr<void> set(size_t index, JsonValue value) { return m_values.try_insert(index, move(value)); }
|
||||||
|
|
||||||
template<typename Builder>
|
template<typename Builder>
|
||||||
typename Builder::OutputType serialized() const;
|
typename Builder::OutputType serialized() const;
|
||||||
|
|
|
@ -164,7 +164,7 @@ ErrorOr<JsonValue> JsonParser::parse_array()
|
||||||
if (peek() == ']')
|
if (peek() == ']')
|
||||||
break;
|
break;
|
||||||
auto element = TRY(parse_helper());
|
auto element = TRY(parse_helper());
|
||||||
array.append(move(element));
|
array.must_append(move(element));
|
||||||
ignore_while(is_space);
|
ignore_while(is_space);
|
||||||
if (peek() == ']')
|
if (peek() == ']')
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -177,7 +177,7 @@ ErrorOr<void> KeyboardMapperWidget::save_to_file(StringView filename)
|
||||||
sb.append_code_point(values[i]);
|
sb.append_code_point(values[i]);
|
||||||
|
|
||||||
JsonValue val(sb.to_deprecated_string());
|
JsonValue val(sb.to_deprecated_string());
|
||||||
items.append(move(val));
|
items.must_append(move(val));
|
||||||
}
|
}
|
||||||
map_json.set(name, move(items));
|
map_json.set(name, move(items));
|
||||||
};
|
};
|
||||||
|
|
|
@ -202,7 +202,7 @@ ErrorOr<void> ExportDialog::make_and_run_for(StringView mime, Core::File& file,
|
||||||
auto export_worksheet = [&]() -> ErrorOr<void> {
|
auto export_worksheet = [&]() -> ErrorOr<void> {
|
||||||
JsonArray array;
|
JsonArray array;
|
||||||
for (auto& sheet : workbook.sheets())
|
for (auto& sheet : workbook.sheets())
|
||||||
array.append(sheet->to_json());
|
array.must_append(sheet->to_json());
|
||||||
|
|
||||||
auto file_content = array.to_deprecated_string();
|
auto file_content = array.to_deprecated_string();
|
||||||
return file.write_until_depleted(file_content.bytes());
|
return file.write_until_depleted(file_content.bytes());
|
||||||
|
|
|
@ -542,7 +542,7 @@ JsonObject Sheet::to_json() const
|
||||||
if (!columns_are_standard()) {
|
if (!columns_are_standard()) {
|
||||||
auto columns = JsonArray();
|
auto columns = JsonArray();
|
||||||
for (auto& column : m_columns)
|
for (auto& column : m_columns)
|
||||||
columns.append(column);
|
columns.must_append(column);
|
||||||
object.set("columns", move(columns));
|
object.set("columns", move(columns));
|
||||||
}
|
}
|
||||||
object.set("rows", bottom_right.row + 1);
|
object.set("rows", bottom_right.row + 1);
|
||||||
|
@ -587,7 +587,7 @@ JsonObject Sheet::to_json() const
|
||||||
fmt_object.set("condition", fmt.condition);
|
fmt_object.set("condition", fmt.condition);
|
||||||
save_format(fmt, fmt_object);
|
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));
|
data.set("conditional_formats", move(conditional_formats));
|
||||||
|
|
|
@ -260,7 +260,7 @@ public:
|
||||||
for (auto& object : Object::all_objects()) {
|
for (auto& object : Object::all_objects()) {
|
||||||
JsonObject json_object;
|
JsonObject json_object;
|
||||||
object.save_to(json_object);
|
object.save_to(json_object);
|
||||||
objects.append(move(json_object));
|
objects.must_append(move(json_object));
|
||||||
}
|
}
|
||||||
response.set("objects", move(objects));
|
response.set("objects", move(objects));
|
||||||
send_response(response);
|
send_response(response);
|
||||||
|
|
|
@ -335,8 +335,8 @@ requires IsBaseOf<Object, T>
|
||||||
[this] { \
|
[this] { \
|
||||||
auto size = this->getter(); \
|
auto size = this->getter(); \
|
||||||
JsonArray size_array; \
|
JsonArray size_array; \
|
||||||
size_array.append(size.width()); \
|
size_array.must_append(size.width()); \
|
||||||
size_array.append(size.height()); \
|
size_array.must_append(size.height()); \
|
||||||
return size_array; \
|
return size_array; \
|
||||||
}, \
|
}, \
|
||||||
{});
|
{});
|
||||||
|
@ -379,8 +379,8 @@ requires IsBaseOf<Object, T>
|
||||||
[this] { \
|
[this] { \
|
||||||
auto size = this->getter(); \
|
auto size = this->getter(); \
|
||||||
JsonArray size_array; \
|
JsonArray size_array; \
|
||||||
size_array.append(size.width()); \
|
size_array.must_append(size.width()); \
|
||||||
size_array.append(size.height()); \
|
size_array.must_append(size.height()); \
|
||||||
return size_array; \
|
return size_array; \
|
||||||
}, \
|
}, \
|
||||||
[this](auto& value) { \
|
[this](auto& value) { \
|
||||||
|
|
|
@ -67,7 +67,7 @@ bool JsonArrayModel::add(Vector<JsonValue> const&& values)
|
||||||
auto& field_spec = m_fields[i];
|
auto& field_spec = m_fields[i];
|
||||||
obj.set(field_spec.json_field_name, values.at(i));
|
obj.set(field_spec.json_field_name, values.at(i));
|
||||||
}
|
}
|
||||||
m_array.append(move(obj));
|
m_array.must_append(move(obj));
|
||||||
did_update();
|
did_update();
|
||||||
return true;
|
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)));
|
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();
|
did_update();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -99,7 +99,7 @@ bool JsonArrayModel::remove(int row)
|
||||||
JsonArray new_array;
|
JsonArray new_array;
|
||||||
for (size_t i = 0; i < m_array.size(); ++i)
|
for (size_t i = 0; i < m_array.size(); ++i)
|
||||||
if (i != (size_t)row)
|
if (i != (size_t)row)
|
||||||
new_array.append(m_array.at(i));
|
new_array.must_append(m_array.at(i));
|
||||||
|
|
||||||
m_array = new_array;
|
m_array = new_array;
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ Layout::Layout(Margins initial_margins, int spacing)
|
||||||
} else {
|
} else {
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
entries_array.append(move(entry_object));
|
entries_array.must_append(move(entry_object));
|
||||||
}
|
}
|
||||||
return entries_array;
|
return entries_array;
|
||||||
});
|
});
|
||||||
|
|
|
@ -347,7 +347,7 @@ Response process_capabilities(JsonValue const& parameters)
|
||||||
all_first_match_capabilities = capabilities->as_array();
|
all_first_match_capabilities = capabilities->as_array();
|
||||||
} else {
|
} 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.
|
// 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.
|
// 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));
|
auto validated_capabilities = TRY(validate_capabilities(first_match_capabilities));
|
||||||
|
|
||||||
// b. Append validated capabilities to validated 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 {};
|
return {};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -374,7 +374,7 @@ Response process_capabilities(JsonValue const& parameters)
|
||||||
auto merged = TRY(merge_capabilities(required_capabilities, first_match_capabilities.as_object()));
|
auto merged = TRY(merge_capabilities(required_capabilities, first_match_capabilities.as_object()));
|
||||||
|
|
||||||
// b. Append merged to merged capabilities.
|
// b. Append merged to merged capabilities.
|
||||||
merged_capabilities.append(move(merged));
|
merged_capabilities.must_append(move(merged));
|
||||||
return {};
|
return {};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,7 @@ static ErrorOr<JsonValue, ExecuteScriptResultType> clone_an_object(JS::Realm& re
|
||||||
return ExecuteScriptResultType::JavaScriptError;
|
return ExecuteScriptResultType::JavaScriptError;
|
||||||
auto array = JsonArray {};
|
auto array = JsonArray {};
|
||||||
for (size_t i = 0; i < length; ++i)
|
for (size_t i = 0; i < length; ++i)
|
||||||
array.append(JsonValue {});
|
array.must_append(JsonValue {});
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
// -> Otherwise
|
// -> Otherwise
|
||||||
|
@ -203,7 +203,7 @@ static ErrorOr<JsonValue, ExecuteScriptResultType> clone_an_object(JS::Realm& re
|
||||||
[&](JsonArray& array) {
|
[&](JsonArray& array) {
|
||||||
// NOTE: If this was a JS array, only indexed properties would be serialized anyway.
|
// NOTE: If this was a JS array, only indexed properties would be serialized anyway.
|
||||||
if (name.is_number())
|
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) {
|
[&](JsonObject& object) {
|
||||||
object.set(name.to_string(), cloned_property_result.value());
|
object.set(name.to_string(), cloned_property_result.value());
|
||||||
|
|
|
@ -1521,7 +1521,7 @@ Messages::WebDriverClient::GetAllCookiesResponse WebDriverConnection::get_all_co
|
||||||
auto serialized_cookie = serialize_cookie(cookie);
|
auto serialized_cookie = serialize_cookie(cookie);
|
||||||
|
|
||||||
// 2. Append serialized cookie to cookies
|
// 2. Append serialized cookie to cookies
|
||||||
cookies.append(move(serialized_cookie));
|
cookies.must_append(move(serialized_cookie));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Return success with data cookies.
|
// 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.
|
// 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)
|
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.
|
// 9. Return success with data result.
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -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.
|
// 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()) {
|
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.
|
// 3. Return success with data handles.
|
||||||
|
|
|
@ -2367,7 +2367,7 @@ void Shell::save_to(JsonObject& object)
|
||||||
job_object.set("running_time", job_entry.value->timer().elapsed());
|
job_object.set("running_time", job_entry.value->timer().elapsed());
|
||||||
job_object.set("command", job_entry.value->cmd());
|
job_object.set("command", job_entry.value->cmd());
|
||||||
job_object.set("is_running_in_background", job_entry.value->is_running_in_background());
|
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));
|
object.set("jobs", move(job_objects));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue