Skip to content

Commit

Permalink
Merge pull request #66 from ontodev/fix-nulls
Browse files Browse the repository at this point in the history
Fix rendering of null values
  • Loading branch information
jamesaoverton authored Nov 30, 2023
2 parents 74756ab + e2fbdef commit af8d169
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
37 changes: 31 additions & 6 deletions src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,21 @@ async fn get_page(
Ok(message_counts) => message_counts,
Err(e) => return Err(GetError::new(e.to_string())),
};

// convert value_rows to cell_rows
let table_type = config
.valve
.as_ref()
.and_then(|v| v.config.get("table"))
.and_then(|v| v.as_object())
.and_then(|o| o.get(&unquoted_table))
.and_then(|v| v.as_object())
.and_then(|o| o.get("type"))
.and_then(|v| v.as_str())
.unwrap_or_default();
let cell_rows: Vec<Map<String, Value>> = value_rows
.iter()
.map(|r| decorate_row(&unquoted_table, &column_map, r))
.map(|r| decorate_row(&unquoted_table, &table_type, &column_map, r))
.collect();

let mut counts = Map::new();
Expand Down Expand Up @@ -685,9 +696,10 @@ async fn get_page(
Ok(result)
}

// Given a table name, a column map, a cell value, and message list,
// Given a table type, a column map, a cell value, and message list,
// return a JSON value representing this cell.
fn decorate_cell(
table_type: &str,
column_name: &str,
column: &Value,
value: &Value,
Expand All @@ -701,7 +713,6 @@ fn decorate_cell(

// Handle null and nulltype
if value.is_null() {
classes.push("bg-null".to_string());
if let Some(nulltype) = column.get("nulltype") {
if nulltype.is_string() {
cell.insert("nulltype".to_string(), nulltype.clone());
Expand All @@ -714,15 +725,20 @@ fn decorate_cell(
cell.insert("datatype".to_string(), datatype.clone());
}

if classes.len() > 0 {
cell.insert("classes".to_string(), json!(classes));
// Add links to other tables
if ["table", "column"].contains(&table_type) && column_name == "table" {
cell.insert("href".to_string(), json!(value));
}

// Handle messages associated with the row:
let mut output_messages = vec![];
let mut max_level = 0;
let mut message_level = "none";
for message in messages.iter().filter(|m| m.column == column_name) {
// Override null values
if value.is_null() {
cell.insert("value".to_string(), json!(message.value));
}
output_messages.push(json!({
"level": message.level,
"rule": message.rule,
Expand Down Expand Up @@ -751,11 +767,20 @@ fn decorate_cell(
cell.insert("history".to_string(), json!(changes));
}

if cell.get("value").unwrap().is_null() {
classes.push("null".to_string());
}

if classes.len() > 0 {
cell.insert("classes".to_string(), json!(classes));
}

cell
}

fn decorate_row(
table: &str,
table_type: &str,
column_map: &Map<String, Value>,
row: &Map<String, Value>,
) -> Map<String, Value> {
Expand Down Expand Up @@ -794,7 +819,7 @@ fn decorate_row(
"datatype": "integer",
});
let column = column_map.get(column_name).unwrap_or(&default_column);
let cell = decorate_cell(column_name, column, value, &messages, &history);
let cell = decorate_cell(table_type, column_name, column, value, &messages, &history);
cell_row.insert(column_name.to_string(), serde_json::Value::Object(cell));
}
cell_row
Expand Down
16 changes: 13 additions & 3 deletions src/resources/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
text-decoration: none !important;
}

.null {
color: #cfcfcf;
}

.null:before {
content: "null";
}

span.twitter-typeahead .tt-menu {
cursor: pointer;
}
Expand Down Expand Up @@ -135,11 +143,13 @@
<i class="bi bi-save"></i>
Save
</button>
<button class="dropdown-item {% if not page.undo %}disabled{% endif %}" type="submit" name="undo" value="Undo">
<button class="dropdown-item {% if not page.undo %}disabled{% endif %}" type="submit" name="undo"
value="Undo">
<i class="bi bi-arrow-counterclockwise"></i>
{% if page.undo %}{{ page.undo }}{% else %}Undo{% endif %}
</button>
<button class="dropdown-item {% if not page.redo %}disabled{% endif %}" type="submit" name="redo" value="redo">
<button class="dropdown-item {% if not page.redo %}disabled{% endif %}" type="submit" name="redo"
value="redo">
<i class="bi bi-arrow-counterclockwise"></i>
{% if page.redo %}{{ page.redo }}{% else %}Redo{% endif %}
</button>
Expand Down Expand Up @@ -246,4 +256,4 @@
{% block body_end %}{% endblock %}
</body>

</html>
</html>
8 changes: 4 additions & 4 deletions src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,10 +892,10 @@ async fn table(
"root": "",
"project_name": "Nanobot",
"tables": table_map,
"undo": get::get_undo_message(&state.config),
"redo": get::get_redo_message(&state.config),
"actions": get::get_action_map(&state.config).unwrap_or_default(),
"repo": get::get_repo_details().unwrap_or_default(),
"undo": get::get_undo_message(&state.config),
"redo": get::get_redo_message(&state.config),
"actions": get::get_action_map(&state.config).unwrap_or_default(),
"repo": get::get_repo_details().unwrap_or_default(),
},
"title": "table",
"table_name": table,
Expand Down

0 comments on commit af8d169

Please sign in to comment.