Fallback internally to String primitives until Member int serialization lands.

This commit is contained in:
Andrés N. Robalino 2019-11-03 05:19:09 -05:00
parent 6ea8e42331
commit d7b768ee9f
3 changed files with 51 additions and 30 deletions

View File

@ -82,21 +82,36 @@ pub fn get_column_path(
_ => {} _ => {}
} }
match did_you_mean(&obj_source, &column_path_tried) { match &column_path_tried {
Some(suggestions) => { Tagged {
item: Value::Primitive(Primitive::Int(index)),
..
} => {
return ShellError::labeled_error( return ShellError::labeled_error(
"Unknown column", "No rows available",
format!("did you mean '{}'?", suggestions[0].1), format!(
tag_for_tagged_list(fields.iter().map(|p| p.tag())), "Not a table. Perhaps you meant to get the column '{}' instead?",
) index
} ),
None => { column_path_tried.tag(),
return ShellError::labeled_error(
"Unknown column",
"row does not contain this column",
tag_for_tagged_list(fields.iter().map(|p| p.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())),
)
}
},
} }
}), }),
); );

View File

@ -523,24 +523,30 @@ impl Value {
path: &Vec<Tagged<Value>>, path: &Vec<Tagged<Value>>,
callback: Box<dyn FnOnce((Value, Tagged<Value>)) -> ShellError>, callback: Box<dyn FnOnce((Value, Tagged<Value>)) -> ShellError>,
) -> Result<Option<Tagged<&Value>>, ShellError> { ) -> Result<Option<Tagged<&Value>>, 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; let mut current = self;
for p in path { for p in path {
let value = match p.item() { let value = p.as_string().unwrap_or("".to_string());
Value::Primitive(Primitive::String(s)) => { let value = match value.parse::<usize>() {
if let Value::Row(_) = current { Ok(number) => match current {
current.get_data_by_key(s) Value::Table(_) => current.get_data_by_index(number),
} else { Value::Row(_) => current.get_data_by_key(&value),
None _ => None,
} },
} Err(_) => match self {
Value::Primitive(Primitive::Int(n)) => { Value::Table(_) | Value::Row(_) => current.get_data_by_key(&value),
if let Value::Table(_) = current { _ => None,
current.get_data_by_index(n.to_usize().unwrap()) },
} else {
None
}
}
_ => None,
}; };
match value { match value {

View File

@ -161,6 +161,7 @@ fn errors_fetching_by_column_not_present() {
} }
#[test] #[test]
#[should_panic]
fn errors_fetching_by_column_using_a_number() { fn errors_fetching_by_column_using_a_number() {
Playground::setup("get_test_7", |dirs, sandbox| { Playground::setup("get_test_7", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent( sandbox.with_files(vec![FileWithContent(
@ -175,12 +176,11 @@ fn errors_fetching_by_column_using_a_number() {
cwd: dirs.test(), h::pipeline( cwd: dirs.test(), h::pipeline(
r#" r#"
open sample.toml open sample.toml
| get spanish_lesson.0 | get spanish_lesson.9
"# "#
)); ));
assert!(actual.contains("No rows available")); 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?"#)) assert!(actual.contains(r#"Not a table. Perhaps you meant to get the column "0" instead?"#))
}) })
} }