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

@ -5,7 +5,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
engine::Command, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
};
@ -150,7 +150,7 @@ the output of 'path parse' and 'path split' subcommands."#
fn handle_value(v: Value, args: &Arguments, head: Span) -> Value {
match v {
Value::String { ref val, .. } => join_single(Path::new(val), head, args),
Value::Record { cols, vals, span } => join_record(&cols, &vals, head, span, args),
Value::Record { val, span } => join_record(&val, head, span, args),
Value::List { vals, span } => join_list(&vals, head, span, args),
_ => super::handle_invalid_values(v, head),
@ -177,7 +177,7 @@ fn join_list(parts: &[Value], head: Span, span: Span, args: &Arguments) -> Value
Ok(vals) => {
let vals = vals
.iter()
.map(|(k, v)| join_record(k, v, head, span, args))
.map(|r| join_record(r, head, span, args))
.collect();
Value::List { vals, span }
@ -194,8 +194,8 @@ fn join_list(parts: &[Value], head: Span, span: Span, args: &Arguments) -> Value
}
}
fn join_record(cols: &[String], vals: &[Value], head: Span, span: Span, args: &Arguments) -> Value {
match merge_record(cols, vals, head, span) {
fn join_record(record: &Record, head: Span, span: Span, args: &Arguments) -> Value {
match merge_record(record, head, span) {
Ok(p) => join_single(p.as_path(), head, args),
Err(error) => Value::Error {
error: Box::new(error),
@ -203,13 +203,8 @@ fn join_record(cols: &[String], vals: &[Value], head: Span, span: Span, args: &A
}
}
fn merge_record(
cols: &[String],
vals: &[Value],
head: Span,
span: Span,
) -> Result<PathBuf, ShellError> {
for key in cols {
fn merge_record(record: &Record, head: Span, span: Span) -> Result<PathBuf, ShellError> {
for key in &record.cols {
if !super::ALLOWED_COLUMNS.contains(&key.as_str()) {
let allowed_cols = super::ALLOWED_COLUMNS.join(", ");
return Err(ShellError::UnsupportedInput(
@ -223,7 +218,13 @@ fn merge_record(
}
}
let entries: HashMap<&str, &Value> = cols.iter().map(String::as_str).zip(vals).collect();
let entries: HashMap<&str, &Value> = record
.cols
.iter()
.map(String::as_str)
.zip(&record.vals)
.collect();
let mut result = PathBuf::new();
#[cfg(windows)]

View File

@ -1,11 +1,10 @@
use std::path::Path;
use indexmap::IndexMap;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{
engine::Command, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
engine::Command, Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned,
SyntaxShape, Type, Value,
};
@ -77,7 +76,7 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Parse a single path",
example: r"'C:\Users\viking\spam.txt' | path parse",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec![
"prefix".into(),
"parent".into(),
@ -90,8 +89,7 @@ On Windows, an extra 'prefix' column is added."#
Value::test_string("spam"),
Value::test_string("txt"),
],
span: Span::test_data(),
}),
})),
},
Example {
description: "Replace a complex extension",
@ -101,7 +99,7 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Ignore the extension",
example: r"'C:\Users\viking.d' | path parse -e ''",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec![
"prefix".into(),
"parent".into(),
@ -114,14 +112,13 @@ On Windows, an extra 'prefix' column is added."#
Value::test_string("viking.d"),
Value::test_string(""),
],
span: Span::test_data(),
}),
})),
},
Example {
description: "Parse all paths in a list",
example: r"[ C:\Users\viking.d C:\Users\spam.txt ] | path parse",
result: Some(Value::test_list(vec![
Value::Record {
Value::test_record(Record {
cols: vec![
"prefix".into(),
"parent".into(),
@ -134,9 +131,8 @@ On Windows, an extra 'prefix' column is added."#
Value::test_string("viking"),
Value::test_string("d"),
],
span: Span::test_data(),
},
Value::Record {
}),
Value::test_record(Record {
cols: vec![
"prefix".into(),
"parent".into(),
@ -149,8 +145,7 @@ On Windows, an extra 'prefix' column is added."#
Value::test_string("spam"),
Value::test_string("txt"),
],
span: Span::test_data(),
},
}),
])),
},
]
@ -162,15 +157,14 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Parse a path",
example: r"'/home/viking/spam.txt' | path parse",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["parent".into(), "stem".into(), "extension".into()],
vals: vec![
Value::test_string("/home/viking"),
Value::test_string("spam"),
Value::test_string("txt"),
],
span: Span::test_data(),
}),
})),
},
Example {
description: "Replace a complex extension",
@ -180,38 +174,35 @@ On Windows, an extra 'prefix' column is added."#
Example {
description: "Ignore the extension",
example: r"'/etc/conf.d' | path parse -e ''",
result: Some(Value::Record {
result: Some(Value::test_record(Record {
cols: vec!["parent".into(), "stem".into(), "extension".into()],
vals: vec![
Value::test_string("/etc"),
Value::test_string("conf.d"),
Value::test_string(""),
],
span: Span::test_data(),
}),
})),
},
Example {
description: "Parse all paths in a list",
example: r"[ /home/viking.d /home/spam.txt ] | path parse",
result: Some(Value::test_list(vec![
Value::Record {
Value::test_record(Record {
cols: vec!["parent".into(), "stem".into(), "extension".into()],
vals: vec![
Value::test_string("/home"),
Value::test_string("viking"),
Value::test_string("d"),
],
span: Span::test_data(),
},
Value::Record {
}),
Value::test_record(Record {
cols: vec!["parent".into(), "stem".into(), "extension".into()],
vals: vec![
Value::test_string("/home"),
Value::test_string("spam"),
Value::test_string("txt"),
],
span: Span::test_data(),
},
}),
])),
},
]
@ -219,7 +210,7 @@ On Windows, an extra 'prefix' column is added."#
}
fn parse(path: &Path, span: Span, args: &Arguments) -> Value {
let mut map: IndexMap<String, Value> = IndexMap::new();
let mut record = Record::new();
#[cfg(windows)]
{
@ -231,7 +222,7 @@ fn parse(path: &Path, span: Span, args: &Arguments) -> Value {
}
_ => "".into(),
};
map.insert("prefix".into(), Value::string(prefix, span));
record.push("prefix", Value::string(prefix, span));
}
let parent = path
@ -239,7 +230,7 @@ fn parse(path: &Path, span: Span, args: &Arguments) -> Value {
.unwrap_or_else(|| "".as_ref())
.to_string_lossy();
map.insert("parent".into(), Value::string(parent, span));
record.push("parent", Value::string(parent, span));
let basename = path
.file_name()
@ -254,14 +245,11 @@ fn parse(path: &Path, span: Span, args: &Arguments) -> Value {
let ext_with_dot = [".", extension].concat();
if basename.ends_with(&ext_with_dot) && !extension.is_empty() {
let stem = basename.trim_end_matches(&ext_with_dot);
map.insert("stem".into(), Value::string(stem, span));
map.insert(
"extension".into(),
Value::string(extension, *extension_span),
);
record.push("stem", Value::string(stem, span));
record.push("extension", Value::string(extension, *extension_span));
} else {
map.insert("stem".into(), Value::string(basename, span));
map.insert("extension".into(), Value::string("", span));
record.push("stem", Value::string(basename, span));
record.push("extension", Value::string("", span));
}
}
None => {
@ -274,12 +262,12 @@ fn parse(path: &Path, span: Span, args: &Arguments) -> Value {
.unwrap_or_else(|| "".as_ref())
.to_string_lossy();
map.insert("stem".into(), Value::string(stem, span));
map.insert("extension".into(), Value::string(extension, span));
record.push("stem", Value::string(stem, span));
record.push("extension", Value::string(extension, span));
}
}
Value::from(Spanned { item: map, span })
Value::record(record, span)
}
#[cfg(test)]