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) {
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())),
)
}
},
}
}),
);

View File

@ -523,24 +523,30 @@ impl Value {
path: &Vec<Tagged<Value>>,
callback: Box<dyn FnOnce((Value, 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;
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::<usize>() {
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 {

View File

@ -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?"#))
})
}