AK+Spreadsheet+LibWeb: Remove JsonObject::get_or()

This removes JsonObject::get_or(), which is inefficient because it has
to copy the returned value. It was only used in a few cases, some of
which resulted in copying JsonObjects, which can become quite large.
This commit is contained in:
Max Wipfli 2021-06-28 12:40:04 +02:00 committed by Andreas Kling
parent e82611bb87
commit 9c8a2a5f69
4 changed files with 91 additions and 97 deletions

View file

@ -59,12 +59,6 @@ public:
return *value;
}
JsonValue get_or(String const& key, JsonValue const& alternative) const
{
auto* value = get_ptr(key);
return value ? *value : alternative;
}
JsonValue const* get_ptr(String const& key) const
{
auto it = m_members.find(key);

View file

@ -91,18 +91,18 @@ HelpWindow::HelpWindow(GUI::Window* parent)
auto& doc = doc_option.as_object();
const auto& name = url.fragment();
auto example_data_value = doc.get_or("example_data", JsonObject {});
if (!example_data_value.is_object()) {
auto* example_data_ptr = doc.get_ptr("example_data");
if (!example_data_ptr || !example_data_ptr->is_object()) {
GUI::MessageBox::show_error(this, String::formatted("No example data found for '{}'", url.path()));
return;
}
auto& example_data = example_data_ptr->as_object();
auto& example_data = example_data_value.as_object();
auto value = example_data.get(name);
if (!value.is_object()) {
if (!example_data.has_object(name)) {
GUI::MessageBox::show_error(this, String::formatted("Example '{}' not found for '{}'", name, url.path()));
return;
}
auto& value = example_data.get(name);
auto window = GUI::Window::construct(this);
window->resize(size());
@ -138,21 +138,15 @@ HelpWindow::HelpWindow(GUI::Window* parent)
String HelpWindow::render(const StringView& key)
{
auto doc_option = m_docs.get(key);
VERIFY(doc_option.is_object());
auto& doc = doc_option.as_object();
VERIFY(m_docs.has_object(key));
auto& doc = m_docs.get(key).as_object();
auto name = doc.get("name").to_string();
auto argc = doc.get("argc").to_u32(0);
auto argnames_value = doc.get("argnames");
VERIFY(argnames_value.is_array());
auto& argnames = argnames_value.as_array();
VERIFY(doc.has_array("argnames"));
auto& argnames = doc.get("argnames").as_array();
auto docstring = doc.get("doc").to_string();
auto examples_value = doc.get_or("examples", JsonObject {});
VERIFY(examples_value.is_object());
auto& examples = examples_value.as_object();
StringBuilder markdown_builder;
@ -184,9 +178,11 @@ String HelpWindow::render(const StringView& key)
markdown_builder.append(docstring);
markdown_builder.append("\n\n");
if (!examples.is_empty()) {
if (doc.has("examples")) {
auto& examples = doc.get("examples");
VERIFY(examples.is_object());
markdown_builder.append("# EXAMPLES\n");
examples.for_each_member([&](auto& text, auto& description_value) {
examples.as_object().for_each_member([&](auto& text, auto& description_value) {
dbgln("- {}\n\n```js\n{}\n```\n", description_value.to_string(), text);
markdown_builder.appendff("- {}\n\n```js\n{}\n```\n", description_value.to_string(), text);
});

View file

@ -343,10 +343,8 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
auto rows = object.get("rows").to_u32(default_row_count);
auto columns = object.get("columns");
auto name = object.get("name").as_string_or("Sheet");
auto cells_value = object.get_or("cells", JsonObject {});
if (!cells_value.is_object())
return nullptr;
auto& cells = cells_value.as_object();
if (object.has("cells") && !object.has_object("cells"))
return {};
sheet->set_name(name);
@ -376,77 +374,79 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
format.background_color = Color::from_string(value.as_string());
};
cells.for_each_member([&](auto& name, JsonValue const& value) {
auto position_option = sheet->parse_cell_name(name);
if (!position_option.has_value())
return IterationDecision::Continue;
auto position = position_option.value();
auto& obj = value.as_object();
auto kind = obj.get("kind").as_string_or("LiteralString") == "LiteralString" ? Cell::LiteralString : Cell::Formula;
OwnPtr<Cell> cell;
switch (kind) {
case Cell::LiteralString:
cell = make<Cell>(obj.get("value").to_string(), position, *sheet);
break;
case Cell::Formula: {
auto& interpreter = sheet->interpreter();
auto value = interpreter.vm().call(parse_function, json, JS::js_string(interpreter.heap(), obj.get("value").as_string()));
cell = make<Cell>(obj.get("source").to_string(), move(value), position, *sheet);
break;
}
}
auto type_name = obj.get_or("type", "Numeric").to_string();
cell->set_type(type_name);
auto type_meta = obj.get("type_metadata");
if (type_meta.is_object()) {
auto& meta_obj = type_meta.as_object();
auto meta = cell->type_metadata();
if (auto value = meta_obj.get("length"); value.is_number())
meta.length = value.to_i32();
if (auto value = meta_obj.get("format"); value.is_string())
meta.format = value.as_string();
read_format(meta.static_format, meta_obj);
cell->set_type_metadata(move(meta));
}
auto conditional_formats = obj.get("conditional_formats");
auto cformats = cell->conditional_formats();
if (conditional_formats.is_array()) {
conditional_formats.as_array().for_each([&](const auto& fmt_val) {
if (!fmt_val.is_object())
return IterationDecision::Continue;
auto& fmt_obj = fmt_val.as_object();
auto fmt_cond = fmt_obj.get("condition").to_string();
if (fmt_cond.is_empty())
return IterationDecision::Continue;
ConditionalFormat fmt;
fmt.condition = move(fmt_cond);
read_format(fmt, fmt_obj);
cformats.append(move(fmt));
if (object.has_object("cells")) {
object.get("cells").as_object().for_each_member([&](auto& name, JsonValue const& value) {
auto position_option = sheet->parse_cell_name(name);
if (!position_option.has_value())
return IterationDecision::Continue;
});
cell->set_conditional_formats(move(cformats));
}
auto evaluated_format = obj.get("evaluated_formats");
if (evaluated_format.is_object()) {
auto& evaluated_format_obj = evaluated_format.as_object();
auto& evaluated_fmts = cell->evaluated_formats();
auto position = position_option.value();
auto& obj = value.as_object();
auto kind = obj.get("kind").as_string_or("LiteralString") == "LiteralString" ? Cell::LiteralString : Cell::Formula;
read_format(evaluated_fmts, evaluated_format_obj);
}
OwnPtr<Cell> cell;
switch (kind) {
case Cell::LiteralString:
cell = make<Cell>(obj.get("value").to_string(), position, *sheet);
break;
case Cell::Formula: {
auto& interpreter = sheet->interpreter();
auto value = interpreter.vm().call(parse_function, json, JS::js_string(interpreter.heap(), obj.get("value").as_string()));
cell = make<Cell>(obj.get("source").to_string(), move(value), position, *sheet);
break;
}
}
sheet->m_cells.set(position, cell.release_nonnull());
return IterationDecision::Continue;
});
auto type_name = obj.has("type") ? obj.get("type").to_string() : "Numeric";
cell->set_type(type_name);
auto type_meta = obj.get("type_metadata");
if (type_meta.is_object()) {
auto& meta_obj = type_meta.as_object();
auto meta = cell->type_metadata();
if (auto value = meta_obj.get("length"); value.is_number())
meta.length = value.to_i32();
if (auto value = meta_obj.get("format"); value.is_string())
meta.format = value.as_string();
read_format(meta.static_format, meta_obj);
cell->set_type_metadata(move(meta));
}
auto conditional_formats = obj.get("conditional_formats");
auto cformats = cell->conditional_formats();
if (conditional_formats.is_array()) {
conditional_formats.as_array().for_each([&](const auto& fmt_val) {
if (!fmt_val.is_object())
return IterationDecision::Continue;
auto& fmt_obj = fmt_val.as_object();
auto fmt_cond = fmt_obj.get("condition").to_string();
if (fmt_cond.is_empty())
return IterationDecision::Continue;
ConditionalFormat fmt;
fmt.condition = move(fmt_cond);
read_format(fmt, fmt_obj);
cformats.append(move(fmt));
return IterationDecision::Continue;
});
cell->set_conditional_formats(move(cformats));
}
auto evaluated_format = obj.get("evaluated_formats");
if (evaluated_format.is_object()) {
auto& evaluated_format_obj = evaluated_format.as_object();
auto& evaluated_fmts = cell->evaluated_formats();
read_format(evaluated_fmts, evaluated_format_obj);
}
sheet->m_cells.set(position, cell.release_nonnull());
return IterationDecision::Continue;
});
}
return sheet;
}

View file

@ -100,10 +100,14 @@ bool is_pseudo_property(PropertyID property_id)
json.value().as_object().for_each_member([&](auto& name, auto& value) {
VERIFY(value.is_object());
auto pseudo = value.as_object().get_or("pseudo", false);
VERIFY(pseudo.is_bool());
bool pseudo = false;
if (value.as_object().has("pseudo")) {
auto& pseudo_value = value.as_object().get("pseudo");
VERIFY(pseudo_value.is_bool());
pseudo = pseudo_value.as_bool();
}
if (pseudo.as_bool()) {
if (pseudo) {
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(