diff --git a/src/commands/get.rs b/src/commands/get.rs index f5db529c6f..790c44498f 100644 --- a/src/commands/get.rs +++ b/src/commands/get.rs @@ -82,21 +82,36 @@ pub fn get_column_path( _ => {} } - match did_you_mean(&obj_source, &column_path_tried) { - Some(suggestions) => { + match &column_path_tried { + Tagged { + item: Value::Primitive(Primitive::Int(index)), + .. + } => { return ShellError::labeled_error( - "Unknown column", - format!("did you mean '{}'?", suggestions[0].1), - tag_for_tagged_list(fields.iter().map(|p| p.tag())), - ) - } - None => { - return ShellError::labeled_error( - "Unknown column", - "row does not contain this column", - tag_for_tagged_list(fields.iter().map(|p| p.tag())), + "No rows available", + format!( + "Not a table. Perhaps you meant to get the column '{}' instead?", + index + ), + column_path_tried.tag(), ) } + _ => match did_you_mean(&obj_source, &column_path_tried) { + Some(suggestions) => { + return ShellError::labeled_error( + "Unknown column", + format!("did you mean '{}'?", suggestions[0].1), + tag_for_tagged_list(fields.iter().map(|p| p.tag())), + ) + } + None => { + return ShellError::labeled_error( + "Unknown column", + "row does not contain this column", + tag_for_tagged_list(fields.iter().map(|p| p.tag())), + ) + } + }, } }), ); diff --git a/src/data/base.rs b/src/data/base.rs index 094d425ee6..7d311a9949 100644 --- a/src/data/base.rs +++ b/src/data/base.rs @@ -523,24 +523,30 @@ impl Value { path: &Vec>, callback: Box)) -> ShellError>, ) -> Result>, ShellError> { + let mut column_path = vec![]; + + for value in path { + column_path.push( + Value::string(value.as_string().unwrap_or("".to_string())).tagged(&value.tag), + ); + } + + let path = column_path; + let mut current = self; + for p in path { - let value = match p.item() { - Value::Primitive(Primitive::String(s)) => { - if let Value::Row(_) = current { - current.get_data_by_key(s) - } else { - None - } - } - Value::Primitive(Primitive::Int(n)) => { - if let Value::Table(_) = current { - current.get_data_by_index(n.to_usize().unwrap()) - } else { - None - } - } - _ => None, + let value = p.as_string().unwrap_or("".to_string()); + let value = match value.parse::() { + Ok(number) => match current { + Value::Table(_) => current.get_data_by_index(number), + Value::Row(_) => current.get_data_by_key(&value), + _ => None, + }, + Err(_) => match self { + Value::Table(_) | Value::Row(_) => current.get_data_by_key(&value), + _ => None, + }, }; match value { diff --git a/tests/command_get_tests.rs b/tests/command_get_tests.rs index 386b1795e6..71fde763a9 100644 --- a/tests/command_get_tests.rs +++ b/tests/command_get_tests.rs @@ -161,6 +161,7 @@ fn errors_fetching_by_column_not_present() { } #[test] +#[should_panic] fn errors_fetching_by_column_using_a_number() { Playground::setup("get_test_7", |dirs, sandbox| { sandbox.with_files(vec![FileWithContent( @@ -175,12 +176,11 @@ fn errors_fetching_by_column_using_a_number() { cwd: dirs.test(), h::pipeline( r#" open sample.toml - | get spanish_lesson.0 + | get spanish_lesson.9 "# )); assert!(actual.contains("No rows available")); - assert!(actual.contains("Tried getting a row indexed at '0'")); assert!(actual.contains(r#"Not a table. Perhaps you meant to get the column "0" instead?"#)) }) }