Merge pull request #126 from nushell/missing_column_error

Give error on missing column during cell path
This commit is contained in:
JT 2021-10-12 09:02:46 +13:00 committed by GitHub
commit ce81cd6e2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 19 deletions

View File

@ -27,9 +27,9 @@
- [x] Source - [x] Source
- [x] Error shortcircuit (stopping on first error). Revised: errors emit first, but can be seen by commands. - [x] Error shortcircuit (stopping on first error). Revised: errors emit first, but can be seen by commands.
- [x] Value serialization - [x] Value serialization
- [x] Handling rows with missing columns during a cell path
- [ ] Input/output types - [ ] Input/output types
- [ ] Support for `$in` - [ ] Support for `$in`
- [ ] Handling rows with missing columns during a cell path
- [ ] ctrl-c support - [ ] ctrl-c support
- [ ] operator overflow - [ ] operator overflow
- [ ] finish operator type-checking - [ ] finish operator type-checking

View File

@ -52,7 +52,7 @@ impl Command for Select {
fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, ShellError> { fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, ShellError> {
if columns.is_empty() { if columns.is_empty() {
return Err(ShellError::CantFindColumn(span)); return Err(ShellError::CantFindColumn(span, input.span()?));
} }
match input { match input {

View File

@ -99,7 +99,10 @@ pub enum ShellError {
#[error("Cannot find column")] #[error("Cannot find column")]
#[diagnostic(code(nu::shell::column_not_found), url(docsrs))] #[diagnostic(code(nu::shell::column_not_found), url(docsrs))]
CantFindColumn(#[label = "cannot find column"] Span), CantFindColumn(
#[label = "cannot find column"] Span,
#[label = "value originates here"] Span,
),
#[error("External command")] #[error("External command")]
#[diagnostic(code(nu::shell::external_command), url(docsrs))] #[diagnostic(code(nu::shell::external_command), url(docsrs))]

View File

@ -292,7 +292,8 @@ impl Value {
val: column_name, val: column_name,
span: origin_span, span: origin_span,
} => match &mut current { } => match &mut current {
Value::Record { cols, vals, .. } => { Value::Record { cols, vals, span } => {
let span = *span;
let mut found = false; let mut found = false;
for col in cols.iter().zip(vals.iter()) { for col in cols.iter().zip(vals.iter()) {
if col.0 == column_name { if col.0 == column_name {
@ -303,19 +304,23 @@ impl Value {
} }
if !found { if !found {
return Err(ShellError::CantFindColumn(*origin_span)); return Err(ShellError::CantFindColumn(*origin_span, span));
} }
} }
Value::List { vals, span } => { Value::List { vals, span } => {
let mut output = vec![]; let mut output = vec![];
for val in vals { for val in vals {
if let Value::Record { cols, vals, .. } = val { output.push(val.clone().follow_cell_path(&[PathMember::String {
for col in cols.iter().enumerate() { val: column_name.clone(),
if col.1 == column_name { span: *origin_span,
output.push(vals[col.0].clone()); }])?);
} // if let Value::Record { cols, vals, .. } = val {
} // for col in cols.iter().enumerate() {
} // if col.1 == column_name {
// output.push(vals[col.0].clone());
// }
// }
// }
} }
current = Value::List { current = Value::List {
@ -326,13 +331,17 @@ impl Value {
Value::Stream { stream, span } => { Value::Stream { stream, span } => {
let mut output = vec![]; let mut output = vec![];
for val in stream { for val in stream {
if let Value::Record { cols, vals, .. } = val { output.push(val.clone().follow_cell_path(&[PathMember::String {
for col in cols.iter().enumerate() { val: column_name.clone(),
if col.1 == column_name { span: *origin_span,
output.push(vals[col.0].clone()); }])?);
} // if let Value::Record { cols, vals, .. } = val {
} // for col in cols.iter().enumerate() {
} // if col.1 == column_name {
// output.push(vals[col.0].clone());
// }
// }
// }
} }
current = Value::List { current = Value::List {

View File

@ -666,3 +666,11 @@ fn earlier_errors() -> TestResult {
"int", "int",
) )
} }
#[test]
fn missing_column_error() -> TestResult {
fail_test(
r#"([([[name, size]; [ABC, 10], [DEF, 20]]).1, ([[name]; [HIJ]]).0]).size | table"#,
"cannot find column",
)
}