Cleanup code in get and nu-value-ext (#2563)

* Cleanup code in get and nu-value-ext

* Remove unnecessary return statements from get
This commit is contained in:
Radek Vít 2020-09-18 08:40:20 +02:00 committed by GitHub
parent 365f76ad19
commit 15f3a545f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 234 additions and 219 deletions

View File

@ -4,8 +4,8 @@ use indexmap::set::IndexSet;
use log::trace; use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{
did_you_mean, ColumnPath, PathMember, Primitive, ReturnSuccess, Signature, SyntaxShape, did_you_mean, ColumnPath, Dictionary, PathMember, Primitive, ReturnSuccess, Signature,
UnspannedPathMember, UntaggedValue, Value, SyntaxShape, UnspannedPathMember, UntaggedValue, Value,
}; };
use nu_source::HasFallibleSpan; use nu_source::HasFallibleSpan;
use nu_value_ext::get_data_by_column_path; use nu_value_ext::get_data_by_column_path;
@ -58,17 +58,95 @@ impl WholeStreamCommand for Get {
} }
} }
pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellError> { pub async fn get(
let fields = path.clone(); args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (GetArgs { rest: column_paths }, mut input) = args.process(&registry).await?;
if column_paths.is_empty() {
let vec = input.drain_vec().await;
get_data_by_column_path( let descs = nu_protocol::merge_descriptors(&vec);
obj,
path, Ok(futures::stream::iter(descs.into_iter().map(ReturnSuccess::value)).to_output_stream())
Box::new(move |(obj_source, column_path_tried, error)| { } else {
let path_members_span = fields.maybe_span().unwrap_or_else(Span::unknown); trace!("get {:?}", column_paths);
let output_stream = input
.map(move |item| {
let output = column_paths
.iter()
.map(move |path| get_output(&item, path))
.flatten()
.collect::<Vec<_>>();
futures::stream::iter(output)
})
.flatten()
.to_output_stream();
Ok(output_stream)
}
}
fn get_output(item: &Value, path: &ColumnPath) -> Vec<Result<ReturnSuccess, ShellError>> {
match get_column_path(path, item) {
Ok(Value {
value: UntaggedValue::Primitive(Primitive::Nothing),
..
}) => vec![],
Ok(Value {
value: UntaggedValue::Table(rows),
..
}) => rows.into_iter().map(ReturnSuccess::value).collect(),
Ok(other) => vec![ReturnSuccess::value(other)],
Err(reason) => vec![ReturnSuccess::value(
UntaggedValue::Error(reason).into_untagged_value(),
)],
}
}
pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellError> {
get_data_by_column_path(obj, path, move |obj_source, column_path_tried, error| {
let path_members_span = path.maybe_span().unwrap_or_else(Span::unknown);
match &obj_source.value { match &obj_source.value {
UntaggedValue::Table(rows) => match column_path_tried { UntaggedValue::Table(rows) => {
return get_column_path_from_table_error(
rows,
column_path_tried,
&path_members_span,
);
}
UntaggedValue::Row(columns) => {
if let Some(error) = get_column_from_row_error(
columns,
column_path_tried,
&path_members_span,
obj_source,
) {
return error;
}
}
_ => {}
}
if let Some(suggestions) = did_you_mean(&obj_source, column_path_tried) {
ShellError::labeled_error(
"Unknown column",
format!("did you mean '{}'?", suggestions[0].1),
column_path_tried.span.since(path_members_span),
)
} else {
error
}
})
}
pub fn get_column_path_from_table_error(
rows: &[Value],
column_path_tried: &PathMember,
path_members_span: &Span,
) -> ShellError {
match column_path_tried {
PathMember { PathMember {
unspanned: UnspannedPathMember::String(column), unspanned: UnspannedPathMember::String(column),
.. ..
@ -93,15 +171,15 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
} }
if names.is_empty() { if names.is_empty() {
return ShellError::labeled_error_with_secondary( ShellError::labeled_error_with_secondary(
"Unknown column", "Unknown column",
primary_label, primary_label,
column_path_tried.span, column_path_tried.span,
"Appears to contain rows. Try indexing instead.", "Appears to contain rows. Try indexing instead.",
column_path_tried.span.since(path_members_span), column_path_tried.span.since(path_members_span),
); )
} else { } else {
return ShellError::labeled_error_with_secondary( ShellError::labeled_error_with_secondary(
"Unknown column", "Unknown column",
primary_label, primary_label,
column_path_tried.span, column_path_tried.span,
@ -115,8 +193,8 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
names.join(",") names.join(",")
), ),
column_path_tried.span.since(path_members_span), column_path_tried.span.since(path_members_span),
); )
}; }
} }
PathMember { PathMember {
unspanned: UnspannedPathMember::Int(idx), unspanned: UnspannedPathMember::Int(idx),
@ -130,16 +208,24 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
format!("The table only has {} rows (0 to {})", total, total - 1) format!("The table only has {} rows (0 to {})", total, total - 1)
}; };
return ShellError::labeled_error_with_secondary( ShellError::labeled_error_with_secondary(
"Row not found", "Row not found",
format!("There isn't a row indexed at {}", idx), format!("There isn't a row indexed at {}", idx),
column_path_tried.span, column_path_tried.span,
secondary_label, secondary_label,
column_path_tried.span.since(path_members_span), column_path_tried.span.since(path_members_span),
); )
} }
}, }
UntaggedValue::Row(columns) => match column_path_tried { }
pub fn get_column_from_row_error(
columns: &Dictionary,
column_path_tried: &PathMember,
path_members_span: &Span,
obj_source: &Value,
) -> Option<ShellError> {
match column_path_tried {
PathMember { PathMember {
unspanned: UnspannedPathMember::String(column), unspanned: UnspannedPathMember::String(column),
.. ..
@ -147,7 +233,7 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
let primary_label = format!("There isn't a column named '{}'", &column); let primary_label = format!("There isn't a column named '{}'", &column);
if let Some(suggestions) = did_you_mean(&obj_source, column_path_tried) { if let Some(suggestions) = did_you_mean(&obj_source, column_path_tried) {
return ShellError::labeled_error_with_secondary( Some(ShellError::labeled_error_with_secondary(
"Unknown column", "Unknown column",
primary_label, primary_label,
column_path_tried.span, column_path_tried.span,
@ -157,14 +243,15 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
&obj_source.data_descriptors().join(",") &obj_source.data_descriptors().join(",")
), ),
column_path_tried.span.since(path_members_span), column_path_tried.span.since(path_members_span),
); ))
} else {
None
} }
} }
PathMember { PathMember {
unspanned: UnspannedPathMember::Int(idx), unspanned: UnspannedPathMember::Int(idx),
.. ..
} => { } => Some(ShellError::labeled_error_with_secondary(
return ShellError::labeled_error_with_secondary(
"No rows available", "No rows available",
format!("A row at '{}' can't be indexed.", &idx), format!("A row at '{}' can't be indexed.", &idx),
column_path_tried.span, column_path_tried.span,
@ -173,81 +260,8 @@ pub fn get_column_path(path: &ColumnPath, obj: &Value) -> Result<Value, ShellErr
columns.keys().join(",") columns.keys().join(",")
), ),
column_path_tried.span.since(path_members_span), column_path_tried.span.since(path_members_span),
)
}
},
_ => {}
}
if let Some(suggestions) = did_you_mean(&obj_source, column_path_tried) {
return ShellError::labeled_error(
"Unknown column",
format!("did you mean '{}'?", suggestions[0].1),
column_path_tried.span.since(path_members_span),
);
}
error
}),
)
}
pub async fn get(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (GetArgs { rest: mut fields }, mut input) = args.process(&registry).await?;
if fields.is_empty() {
let vec = input.drain_vec().await;
let descs = nu_protocol::merge_descriptors(&vec);
Ok(futures::stream::iter(descs.into_iter().map(ReturnSuccess::value)).to_output_stream())
} else {
let member = fields.remove(0);
trace!("get {:?} {:?}", member, fields);
Ok(input
.map(move |item| {
let member = vec![member.clone()];
let column_paths = vec![&member, &fields]
.into_iter()
.flatten()
.collect::<Vec<&ColumnPath>>();
let mut output = vec![];
for path in column_paths {
let res = get_column_path(&path, &item);
match res {
Ok(got) => match got {
Value {
value: UntaggedValue::Table(rows),
..
} => {
for item in rows {
output.push(ReturnSuccess::value(item.clone()));
}
}
Value {
value: UntaggedValue::Primitive(Primitive::Nothing),
..
} => {}
other => output.push(ReturnSuccess::value(other.clone())),
},
Err(reason) => output.push(ReturnSuccess::value(
UntaggedValue::Error(reason).into_untagged_value(),
)), )),
} }
}
futures::stream::iter(output)
})
.flatten()
.to_output_stream())
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -178,11 +178,7 @@ fn evaluator(by: ColumnPath) -> Box<dyn Fn(usize, &Value) -> Result<Value, Shell
Box::new(move |_: usize, value: &Value| { Box::new(move |_: usize, value: &Value| {
let path = by.clone(); let path = by.clone();
let eval = nu_value_ext::get_data_by_column_path( let eval = nu_value_ext::get_data_by_column_path(value, &path, move |_, _, error| error);
value,
&path,
Box::new(move |(_, _, error)| error),
);
match eval { match eval {
Ok(with_value) => Ok(with_value), Ok(with_value) => Ok(with_value),

View File

@ -83,7 +83,7 @@ async fn select(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
let fetcher = get_data_by_column_path( let fetcher = get_data_by_column_path(
&value, &value,
&path, &path,
Box::new(move |(obj_source, path_member_tried, error)| { move |obj_source, path_member_tried, error| {
if let PathMember { if let PathMember {
unspanned: UnspannedPathMember::String(column), unspanned: UnspannedPathMember::String(column),
.. ..
@ -98,7 +98,7 @@ async fn select(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
} }
error error
}), },
); );
let field = path.clone(); let field = path.clone();

View File

@ -190,8 +190,8 @@ mod tests {
fn error_callback( fn error_callback(
reason: &'static str, reason: &'static str,
) -> impl FnOnce((&Value, &PathMember, ShellError)) -> ShellError { ) -> impl FnOnce(&Value, &PathMember, ShellError) -> ShellError {
move |(_obj_source, _column_path_tried, _err)| ShellError::unimplemented(reason) move |_obj_source, _column_path_tried, _err| ShellError::unimplemented(reason)
} }
fn column_path(paths: &[Value]) -> Result<Tagged<ColumnPathValue>, ShellError> { fn column_path(paths: &[Value]) -> Result<Tagged<ColumnPathValue>, ShellError> {

View File

@ -18,7 +18,7 @@ pub trait ValueExt {
fn get_data_by_column_path( fn get_data_by_column_path(
&self, &self,
path: &ColumnPath, path: &ColumnPath,
callback: Box<dyn FnOnce((&Value, &PathMember, ShellError)) -> ShellError>, callback: Box<dyn FnOnce(&Value, &PathMember, ShellError) -> ShellError>,
) -> Result<Value, ShellError>; ) -> Result<Value, ShellError>;
fn swap_data_by_column_path( fn swap_data_by_column_path(
&self, &self,
@ -66,9 +66,9 @@ impl ValueExt for Value {
fn get_data_by_column_path( fn get_data_by_column_path(
&self, &self,
path: &ColumnPath, path: &ColumnPath,
callback: Box<dyn FnOnce((&Value, &PathMember, ShellError)) -> ShellError>, get_error: Box<dyn FnOnce(&Value, &PathMember, ShellError) -> ShellError>,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
get_data_by_column_path(self, path, callback) get_data_by_column_path(self, path, get_error)
} }
fn swap_data_by_column_path( fn swap_data_by_column_path(
@ -192,11 +192,14 @@ pub fn get_data_by_member(value: &Value, name: &PathMember) -> Result<Value, She
} }
} }
pub fn get_data_by_column_path( pub fn get_data_by_column_path<F>(
value: &Value, value: &Value,
path: &ColumnPath, path: &ColumnPath,
callback: Box<dyn FnOnce((&Value, &PathMember, ShellError)) -> ShellError>, get_error: F,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError>
where
F: FnOnce(&Value, &PathMember, ShellError) -> ShellError,
{
let mut current = value.clone(); let mut current = value.clone();
for p in path.iter() { for p in path.iter() {
@ -204,24 +207,25 @@ pub fn get_data_by_column_path(
match value { match value {
Ok(v) => current = v.clone(), Ok(v) => current = v.clone(),
Err(e) => return Err(callback((&current, &p.clone(), e))), Err(e) => return Err(get_error(&current, &p, e)),
} }
} }
Ok(current) Ok(current)
} }
pub fn swap_data_by_column_path( pub fn swap_data_by_column_path<F>(
value: &Value, value: &Value,
path: &ColumnPath, path: &ColumnPath,
callback: Box<dyn FnOnce(&Value) -> Result<Value, ShellError>>, get_replacement: F,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError>
where
F: FnOnce(&Value) -> Result<Value, ShellError>,
{
let fields = path.clone(); let fields = path.clone();
let to_replace = get_data_by_column_path( let to_replace =
&value, get_data_by_column_path(&value, path, move |obj_source, column_path_tried, error| {
path,
Box::new(move |(obj_source, column_path_tried, error)| {
let path_members_span = fields.maybe_span().unwrap_or_else(Span::unknown); let path_members_span = fields.maybe_span().unwrap_or_else(Span::unknown);
match &obj_source.value { match &obj_source.value {
@ -304,7 +308,7 @@ pub fn swap_data_by_column_path(
let primary_label = format!("There isn't a column named '{}'", &column); let primary_label = format!("There isn't a column named '{}'", &column);
if let Some(suggestions) = if let Some(suggestions) =
nu_protocol::did_you_mean(&obj_source, column_path_tried) nu_protocol::did_you_mean(&obj_source, &column_path_tried)
{ {
return ShellError::labeled_error_with_secondary( return ShellError::labeled_error_with_secondary(
"Unknown column", "Unknown column",
@ -338,7 +342,7 @@ pub fn swap_data_by_column_path(
_ => {} _ => {}
} }
if let Some(suggestions) = nu_protocol::did_you_mean(&obj_source, column_path_tried) { if let Some(suggestions) = nu_protocol::did_you_mean(&obj_source, &column_path_tried) {
return ShellError::labeled_error( return ShellError::labeled_error(
"Unknown column", "Unknown column",
format!("did you mean '{}'?", suggestions[0].1), format!("did you mean '{}'?", suggestions[0].1),
@ -347,11 +351,10 @@ pub fn swap_data_by_column_path(
} }
error error
}), });
);
let to_replace = to_replace?; let to_replace = to_replace?;
let replacement = callback(&to_replace)?; let replacement = get_replacement(&to_replace)?;
value value
.replace_data_at_column_path(&path, replacement) .replace_data_at_column_path(&path, replacement)

View File

@ -1,7 +1,7 @@
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{did_you_mean, ColumnPath, Primitive, ShellTypeName, UntaggedValue, Value}; use nu_protocol::{did_you_mean, ColumnPath, Primitive, ShellTypeName, UntaggedValue, Value};
use nu_source::{span_for_spanned_list, HasSpan, SpannedItem, Tagged}; use nu_source::{span_for_spanned_list, HasSpan, SpannedItem, Tagged};
use nu_value_ext::ValueExt; use nu_value_ext::{get_data_by_column_path, ValueExt};
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum Action { pub enum Action {
@ -100,10 +100,13 @@ impl Inc {
Some(ref f) => { Some(ref f) => {
let fields = f.clone(); let fields = f.clone();
let replace_for = value.get_data_by_column_path( let replace_for = get_data_by_column_path(
&value,
&f, &f,
Box::new(move |(obj_source, column_path_tried, _)| { move |obj_source, column_path_tried, _| match did_you_mean(
match did_you_mean(&obj_source, &column_path_tried) { &obj_source,
&column_path_tried,
) {
Some(suggestions) => ShellError::labeled_error( Some(suggestions) => ShellError::labeled_error(
"Unknown column", "Unknown column",
format!("did you mean '{}'?", suggestions[0].1), format!("did you mean '{}'?", suggestions[0].1),
@ -114,8 +117,7 @@ impl Inc {
"row does not contain this column", "row does not contain this column",
span_for_spanned_list(fields.iter().map(|p| p.span)), span_for_spanned_list(fields.iter().map(|p| p.span)),
), ),
} },
}),
); );
let got = replace_for?; let got = replace_for?;