Create Record type (#10103)

# Description
This PR creates a new `Record` type to reduce duplicate code and
possibly bugs as well. (This is an edited version of #9648.)
- `Record` implements `FromIterator` and `IntoIterator` and so can be
iterated over or collected into. For example, this helps with
conversions to and from (hash)maps. (Also, no more
`cols.iter().zip(vals)`!)
- `Record` has a `push(col, val)` function to help insure that the
number of columns is equal to the number of values. I caught a few
potential bugs thanks to this (e.g. in the `ls` command).
- Finally, this PR also adds a `record!` macro that helps simplify
record creation. It is used like so:
   ```rust
   record! {
       "key1" => some_value,
       "key2" => Value::string("text", span),
       "key3" => Value::int(optional_int.unwrap_or(0), span),
       "key4" => Value::bool(config.setting, span),
   }
   ```
Since macros hinder formatting, etc., the right hand side values should
be relatively short and sweet like the examples above.

Where possible, prefer `record!` or `.collect()` on an iterator instead
of multiple `Record::push`s, since the first two automatically set the
record capacity and do less work overall.

# User-Facing Changes
Besides the changes in `nu-protocol` the only other breaking changes are
to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
This commit is contained in:
Ian Manske
2023-08-24 19:50:29 +00:00
committed by GitHub
parent 030e749fe7
commit 8da27a1a09
195 changed files with 4211 additions and 6245 deletions

View File

@ -1,7 +1,7 @@
use nu_protocol::{
ast::Expr,
engine::{Command, EngineState, Stack, Visibility},
ModuleId, Signature, Span, SyntaxShape, Type, Value,
record, ModuleId, Record, Signature, Span, SyntaxShape, Type, Value,
};
use std::cmp::Ordering;
use std::collections::HashMap;
@ -65,17 +65,16 @@ impl<'e, 's> ScopeData<'e, 's> {
let var_id_val = Value::int(**var_id as i64, span);
vars.push(Value::Record {
cols: vec![
"name".to_string(),
"type".to_string(),
"value".to_string(),
"is_const".to_string(),
"var_id".to_string(),
],
vals: vec![var_name, var_type, var_value, is_const, var_id_val],
vars.push(Value::record(
record! {
"name" => var_name,
"type" => var_type,
"value" => var_value,
"is_const" => is_const,
"var_id" => var_id_val,
},
span,
})
));
}
sort_rows(&mut vars);
@ -89,121 +88,44 @@ impl<'e, 's> ScopeData<'e, 's> {
if self.visibility.is_decl_id_visible(decl_id)
&& !self.engine_state.get_decl(**decl_id).is_alias()
{
let mut cols = vec![];
let mut vals = vec![];
cols.push("name".into());
vals.push(Value::String {
val: String::from_utf8_lossy(command_name).to_string(),
span,
});
let decl = self.engine_state.get_decl(**decl_id);
let signature = decl.signature();
cols.push("category".to_string());
vals.push(Value::String {
val: signature.category.to_string(),
span,
});
cols.push("signatures".to_string());
vals.push(self.collect_signatures(&signature, span));
cols.push("usage".to_string());
vals.push(Value::String {
val: decl.usage().into(),
span,
});
cols.push("examples".to_string());
vals.push(Value::List {
vals: decl
.examples()
.into_iter()
.map(|x| Value::Record {
cols: vec!["description".into(), "example".into(), "result".into()],
vals: vec![
Value::String {
val: x.description.to_string(),
span,
},
Value::String {
val: x.example.to_string(),
span,
},
if let Some(result) = x.result {
result
} else {
Value::Nothing { span }
},
],
let examples = decl
.examples()
.into_iter()
.map(|x| {
Value::record(
record! {
"description" => Value::string(x.description, span),
"example" => Value::string(x.example, span),
"result" => x.result.unwrap_or(Value::nothing(span)),
},
span,
})
.collect(),
span,
});
)
})
.collect();
cols.push("is_builtin".to_string());
// we can only be a is_builtin or is_custom, not both
vals.push(Value::Bool {
val: !decl.is_custom_command(),
span,
});
let record = record! {
"name" => Value::string(String::from_utf8_lossy(command_name), span),
"category" => Value::string(signature.category.to_string(), span),
"signatures" => self.collect_signatures(&signature, span),
"usage" => Value::string(decl.usage(), span),
"examples" => Value::list(examples, span),
// we can only be a is_builtin or is_custom, not both
"is_builtin" => Value::bool(!decl.is_custom_command(), span),
"is_sub" => Value::bool(decl.is_sub(), span),
"is_plugin" => Value::bool(decl.is_plugin().is_some(), span),
"is_custom" => Value::bool(decl.is_custom_command(), span),
"is_keyword" => Value::bool(decl.is_parser_keyword(), span),
"is_extern" => Value::bool(decl.is_known_external(), span),
"creates_scope" => Value::bool(signature.creates_scope, span),
"extra_usage" => Value::string(decl.extra_usage(), span),
"search_terms" => Value::string(decl.search_terms().join(", "), span),
"decl_id" => Value::int(**decl_id as i64, span),
};
cols.push("is_sub".to_string());
vals.push(Value::Bool {
val: decl.is_sub(),
span,
});
cols.push("is_plugin".to_string());
vals.push(Value::Bool {
val: decl.is_plugin().is_some(),
span,
});
cols.push("is_custom".to_string());
vals.push(Value::Bool {
val: decl.is_custom_command(),
span,
});
cols.push("is_keyword".into());
vals.push(Value::Bool {
val: decl.is_parser_keyword(),
span,
});
cols.push("is_extern".to_string());
vals.push(Value::Bool {
val: decl.is_known_external(),
span,
});
cols.push("creates_scope".to_string());
vals.push(Value::Bool {
val: signature.creates_scope,
span,
});
cols.push("extra_usage".to_string());
vals.push(Value::String {
val: decl.extra_usage().into(),
span,
});
let search_terms = decl.search_terms();
cols.push("search_terms".to_string());
vals.push(Value::String {
val: search_terms.join(", "),
span,
});
cols.push("decl_id".into());
vals.push(Value::int(**decl_id as i64, span));
commands.push(Value::Record { cols, vals, span })
commands.push(Value::record(record, span))
}
}
@ -256,8 +178,7 @@ impl<'e, 's> ScopeData<'e, 's> {
// signature usually comes later in the input_output_types, so this will
// remove them from the record.
sigs.dedup_by(|(k1, _), (k2, _)| k1 == k2);
let (cols, vals) = sigs.into_iter().unzip();
Value::Record { cols, vals, span }
Value::record(sigs.into_iter().collect(), span)
}
fn collect_signature_entries(
@ -281,20 +202,22 @@ impl<'e, 's> ScopeData<'e, 's> {
];
// input
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: vec![
Value::nothing(span),
Value::string("input", span),
Value::string(input_type.to_shape().to_string(), span),
Value::bool(false, span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
],
sig_records.push(Value::record(
Record {
cols: sig_cols.clone(),
vals: vec![
Value::nothing(span),
Value::string("input", span),
Value::string(input_type.to_shape().to_string(), span),
Value::bool(false, span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
],
},
span,
});
));
// required_positional
for req in &signature.required_positional {
@ -312,11 +235,13 @@ impl<'e, 's> ScopeData<'e, 's> {
Value::nothing(span),
];
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: sig_vals,
sig_records.push(Value::record(
Record {
cols: sig_cols.clone(),
vals: sig_vals,
},
span,
});
));
}
// optional_positional
@ -339,11 +264,13 @@ impl<'e, 's> ScopeData<'e, 's> {
},
];
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: sig_vals,
sig_records.push(Value::record(
Record {
cols: sig_cols.clone(),
vals: sig_vals,
},
span,
});
));
}
// rest_positional
@ -362,11 +289,13 @@ impl<'e, 's> ScopeData<'e, 's> {
Value::nothing(span), // rest_positional does have default, but parser prohibits specifying it?!
];
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: sig_vals,
sig_records.push(Value::record(
Record {
cols: sig_cols.clone(),
vals: sig_vals,
},
span,
});
));
}
// named flags
@ -410,28 +339,32 @@ impl<'e, 's> ScopeData<'e, 's> {
},
];
sig_records.push(Value::Record {
cols: sig_cols.clone(),
vals: sig_vals,
sig_records.push(Value::record(
Record {
cols: sig_cols.clone(),
vals: sig_vals,
},
span,
});
));
}
// output
sig_records.push(Value::Record {
cols: sig_cols,
vals: vec![
Value::nothing(span),
Value::string("output", span),
Value::string(output_type.to_shape().to_string(), span),
Value::bool(false, span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
],
sig_records.push(Value::record(
Record {
cols: sig_cols,
vals: vec![
Value::nothing(span),
Value::string("output", span),
Value::string(output_type.to_shape().to_string(), span),
Value::bool(false, span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
Value::nothing(span),
],
},
span,
});
));
sig_records
}
@ -443,25 +376,13 @@ impl<'e, 's> ScopeData<'e, 's> {
let decl = self.engine_state.get_decl(**decl_id);
if decl.is_known_external() {
let mut cols = vec![];
let mut vals = vec![];
let record = record! {
"name" => Value::string(String::from_utf8_lossy(command_name), span),
"usage" => Value::string(decl.usage(), span),
"decl_id" => Value::int(**decl_id as i64, span),
};
cols.push("name".into());
vals.push(Value::String {
val: String::from_utf8_lossy(command_name).to_string(),
span,
});
cols.push("usage".to_string());
vals.push(Value::String {
val: decl.usage().into(),
span,
});
cols.push("decl_id".into());
vals.push(Value::int(**decl_id as i64, span));
externals.push(Value::Record { cols, vals, span })
externals.push(Value::record(record, span))
}
}
@ -483,38 +404,20 @@ impl<'e, 's> ScopeData<'e, 's> {
Value::nothing(span)
};
aliases.push(Value::Record {
cols: vec![
"name".into(),
"expansion".into(),
"usage".into(),
"decl_id".into(),
"aliased_decl_id".into(),
],
vals: vec![
Value::String {
val: String::from_utf8_lossy(&decl_name).to_string(),
span,
},
Value::String {
val: String::from_utf8_lossy(
self.engine_state.get_span_contents(alias.wrapped_call.span),
)
.to_string(),
span,
},
Value::String {
val: alias.usage().to_string(),
span,
},
Value::Int {
val: decl_id as i64,
span,
},
aliased_decl_id,
],
let expansion = String::from_utf8_lossy(
self.engine_state.get_span_contents(alias.wrapped_call.span),
);
aliases.push(Value::record(
record! {
"name" => Value::string(String::from_utf8_lossy(&decl_name), span),
"expansion" => Value::string(expansion, span),
"usage" => Value::string(alias.usage(), span),
"decl_id" => Value::int(decl_id as i64, span),
"aliased_decl_id" => aliased_decl_id,
},
span,
});
));
}
}
}
@ -536,11 +439,10 @@ impl<'e, 's> ScopeData<'e, 's> {
if !decl.is_alias() && !decl.is_known_external() {
Some(Value::record(
vec!["name".into(), "decl_id".into()],
vec![
Value::string(String::from_utf8_lossy(name_bytes), span),
Value::int(*decl_id as i64, span),
],
record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"decl_id" => Value::int(*decl_id as i64, span),
},
span,
))
} else {
@ -556,11 +458,10 @@ impl<'e, 's> ScopeData<'e, 's> {
if decl.is_alias() {
Some(Value::record(
vec!["name".into(), "decl_id".into()],
vec![
Value::string(String::from_utf8_lossy(name_bytes), span),
Value::int(*decl_id as i64, span),
],
record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"decl_id" => Value::int(*decl_id as i64, span),
},
span,
))
} else {
@ -576,11 +477,10 @@ impl<'e, 's> ScopeData<'e, 's> {
if decl.is_known_external() {
Some(Value::record(
vec!["name".into(), "decl_id".into()],
vec![
Value::string(String::from_utf8_lossy(name_bytes), span),
Value::int(*decl_id as i64, span),
],
record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"decl_id" => Value::int(*decl_id as i64, span),
},
span,
))
} else {
@ -600,12 +500,11 @@ impl<'e, 's> ScopeData<'e, 's> {
.iter()
.map(|(name_bytes, var_id)| {
Value::record(
vec!["name".into(), "type".into(), "var_id".into()],
vec![
Value::string(String::from_utf8_lossy(name_bytes), span),
Value::string(self.engine_state.get_var(*var_id).ty.to_string(), span),
Value::int(*var_id as i64, span),
],
record! {
"name" => Value::string(String::from_utf8_lossy(name_bytes), span),
"type" => Value::string(self.engine_state.get_var(*var_id).ty.to_string(), span),
"var_id" => Value::int(*var_id as i64, span),
},
span,
)
})
@ -631,46 +530,20 @@ impl<'e, 's> ScopeData<'e, 's> {
.map(|(usage, _)| usage)
.unwrap_or_default();
Value::Record {
cols: vec![
"name".into(),
"commands".into(),
"aliases".into(),
"externs".into(),
"submodules".into(),
"constants".into(),
"env_block".into(),
"usage".into(),
"module_id".into(),
],
vals: vec![
Value::string(String::from_utf8_lossy(module_name), span),
Value::List {
vals: export_commands,
span,
},
Value::List {
vals: export_aliases,
span,
},
Value::List {
vals: export_externs,
span,
},
Value::List {
vals: export_submodules,
span,
},
Value::List {
vals: export_consts,
span,
},
export_env_block,
Value::string(module_usage, span),
Value::int(*module_id as i64, span),
],
Value::record(
record! {
"name" => Value::string(String::from_utf8_lossy(module_name), span),
"commands" => Value::list(export_commands, span),
"aliases" => Value::list(export_aliases, span),
"externs" => Value::list(export_externs, span),
"submodules" => Value::list(export_submodules, span),
"constants" => Value::list(export_consts, span),
"env_block" => export_env_block,
"usage" => Value::string(module_usage, span),
"module_id" => Value::int(*module_id as i64, span),
},
span,
}
)
}
pub fn collect_modules(&self, span: Span) -> Vec<Value> {
@ -685,36 +558,24 @@ impl<'e, 's> ScopeData<'e, 's> {
}
pub fn collect_engine_state(&self, span: Span) -> Value {
let engine_state_cols = vec![
"source_bytes".to_string(),
"num_vars".to_string(),
"num_decls".to_string(),
"num_blocks".to_string(),
"num_modules".to_string(),
"num_env_vars".to_string(),
];
let num_env_vars = self
.engine_state
.env_vars
.values()
.map(|overlay| overlay.len() as i64)
.sum();
let engine_state_vals = vec![
Value::int(self.engine_state.next_span_start() as i64, span),
Value::int(self.engine_state.num_vars() as i64, span),
Value::int(self.engine_state.num_decls() as i64, span),
Value::int(self.engine_state.num_blocks() as i64, span),
Value::int(self.engine_state.num_modules() as i64, span),
Value::int(
self.engine_state
.env_vars
.values()
.map(|overlay| overlay.len() as i64)
.sum(),
span,
),
];
Value::Record {
cols: engine_state_cols,
vals: engine_state_vals,
Value::record(
record! {
"source_bytes" => Value::int(self.engine_state.next_span_start() as i64, span),
"num_vars" => Value::int(self.engine_state.num_vars() as i64, span),
"num_decls" => Value::int(self.engine_state.num_decls() as i64, span),
"num_blocks" => Value::int(self.engine_state.num_blocks() as i64, span),
"num_modules" => Value::int(self.engine_state.num_modules() as i64, span),
"num_env_vars" => Value::int(num_env_vars, span),
},
span,
}
)
}
}
@ -731,10 +592,10 @@ fn extract_custom_completion_from_arg(engine_state: &EngineState, shape: &Syntax
fn sort_rows(decls: &mut [Value]) {
decls.sort_by(|a, b| match (a, b) {
(Value::Record { vals: rec_a, .. }, Value::Record { vals: rec_b, .. }) => {
(Value::Record { val: rec_a, .. }, Value::Record { val: rec_b, .. }) => {
// Comparing the first value from the record
// It is expected that the first value is the name of the entry (command, module, alias, etc.)
match (rec_a.get(0), rec_b.get(0)) {
match (rec_a.vals.get(0), rec_b.vals.get(0)) {
(Some(val_a), Some(val_b)) => match (val_a, val_b) {
(Value::String { val: str_a, .. }, Value::String { val: str_b, .. }) => {
str_a.cmp(str_b)