mirror of
https://github.com/nushell/nushell.git
synced 2024-11-15 21:14:40 +01:00
Merge pull request #126 from nushell/missing_column_error
Give error on missing column during cell path
This commit is contained in:
commit
ce81cd6e2f
2
TODO.md
2
TODO.md
@ -27,9 +27,9 @@
|
||||
- [x] Source
|
||||
- [x] Error shortcircuit (stopping on first error). Revised: errors emit first, but can be seen by commands.
|
||||
- [x] Value serialization
|
||||
- [x] Handling rows with missing columns during a cell path
|
||||
- [ ] Input/output types
|
||||
- [ ] Support for `$in`
|
||||
- [ ] Handling rows with missing columns during a cell path
|
||||
- [ ] ctrl-c support
|
||||
- [ ] operator overflow
|
||||
- [ ] finish operator type-checking
|
||||
|
@ -52,7 +52,7 @@ impl Command for Select {
|
||||
|
||||
fn select(span: Span, columns: Vec<CellPath>, input: Value) -> Result<Value, ShellError> {
|
||||
if columns.is_empty() {
|
||||
return Err(ShellError::CantFindColumn(span));
|
||||
return Err(ShellError::CantFindColumn(span, input.span()?));
|
||||
}
|
||||
|
||||
match input {
|
||||
|
@ -99,7 +99,10 @@ pub enum ShellError {
|
||||
|
||||
#[error("Cannot find column")]
|
||||
#[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")]
|
||||
#[diagnostic(code(nu::shell::external_command), url(docsrs))]
|
||||
|
@ -292,7 +292,8 @@ impl Value {
|
||||
val: column_name,
|
||||
span: origin_span,
|
||||
} => match &mut current {
|
||||
Value::Record { cols, vals, .. } => {
|
||||
Value::Record { cols, vals, span } => {
|
||||
let span = *span;
|
||||
let mut found = false;
|
||||
for col in cols.iter().zip(vals.iter()) {
|
||||
if col.0 == column_name {
|
||||
@ -303,19 +304,23 @@ impl Value {
|
||||
}
|
||||
|
||||
if !found {
|
||||
return Err(ShellError::CantFindColumn(*origin_span));
|
||||
return Err(ShellError::CantFindColumn(*origin_span, span));
|
||||
}
|
||||
}
|
||||
Value::List { vals, span } => {
|
||||
let mut output = vec![];
|
||||
for val in vals {
|
||||
if let Value::Record { cols, vals, .. } = val {
|
||||
for col in cols.iter().enumerate() {
|
||||
if col.1 == column_name {
|
||||
output.push(vals[col.0].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
output.push(val.clone().follow_cell_path(&[PathMember::String {
|
||||
val: column_name.clone(),
|
||||
span: *origin_span,
|
||||
}])?);
|
||||
// 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 {
|
||||
@ -326,13 +331,17 @@ impl Value {
|
||||
Value::Stream { stream, span } => {
|
||||
let mut output = vec![];
|
||||
for val in stream {
|
||||
if let Value::Record { cols, vals, .. } = val {
|
||||
for col in cols.iter().enumerate() {
|
||||
if col.1 == column_name {
|
||||
output.push(vals[col.0].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
output.push(val.clone().follow_cell_path(&[PathMember::String {
|
||||
val: column_name.clone(),
|
||||
span: *origin_span,
|
||||
}])?);
|
||||
// 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 {
|
||||
|
@ -666,3 +666,11 @@ fn earlier_errors() -> TestResult {
|
||||
"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",
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user