forked from extern/nushell
Refactoring and more group-by flexibility.
This commit is contained in:
@ -1,61 +1,28 @@
|
||||
use indexmap::IndexMap;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
||||
use nu_source::{Tag, Tagged, TaggedItem};
|
||||
use nu_value_ext::{as_string, get_data_by_key};
|
||||
use nu_source::Tag;
|
||||
use nu_value_ext::as_string;
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn group(
|
||||
column_name: Option<Tagged<String>>,
|
||||
values: &[Value],
|
||||
grouper: Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
|
||||
values: &Value,
|
||||
grouper: &Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
|
||||
tag: impl Into<Tag>,
|
||||
) -> Result<Value, ShellError> {
|
||||
let tag = tag.into();
|
||||
|
||||
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
|
||||
|
||||
for value in values {
|
||||
let group_key = if let Some(ref column_name) = column_name {
|
||||
get_data_by_key(&value, column_name.borrow_spanned())
|
||||
for value in values.table_entries() {
|
||||
let group_key = if let Some(ref grouper) = grouper {
|
||||
grouper(&value)
|
||||
} else {
|
||||
Some(value.clone())
|
||||
as_string(&value)
|
||||
};
|
||||
|
||||
if let Some(group_key) = group_key {
|
||||
let group_key = if let Some(ref grouper) = grouper {
|
||||
grouper(&group_key)
|
||||
} else {
|
||||
as_string(&group_key)
|
||||
};
|
||||
let group = groups.entry(group_key?).or_insert(vec![]);
|
||||
group.push((*value).clone());
|
||||
} else {
|
||||
let column_name = column_name.unwrap_or_else(|| String::from("").tagged(&tag));
|
||||
|
||||
let possibilities = value.data_descriptors();
|
||||
|
||||
let mut possible_matches: Vec<_> = possibilities
|
||||
.iter()
|
||||
.map(|x| (natural::distance::levenshtein_distance(x, &column_name), x))
|
||||
.collect();
|
||||
|
||||
possible_matches.sort();
|
||||
|
||||
if !possible_matches.is_empty() {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
format!("did you mean '{}'?", possible_matches[0].1),
|
||||
column_name.tag(),
|
||||
));
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Unknown column",
|
||||
"row does not contain this column",
|
||||
column_name.tag(),
|
||||
));
|
||||
}
|
||||
}
|
||||
let group = groups.entry(group_key?).or_insert(vec![]);
|
||||
group.push((*value).clone());
|
||||
}
|
||||
|
||||
let mut out = TaggedDictBuilder::new(&tag);
|
||||
|
@ -1,3 +1,5 @@
|
||||
pub mod group;
|
||||
pub mod split;
|
||||
|
||||
pub use crate::utils::data::group::group;
|
||||
pub use crate::utils::data::split::split;
|
||||
|
@ -12,7 +12,7 @@ use num_traits::Zero;
|
||||
const ERR_EMPTY_DATA: &str = "Cannot perform aggregate math operation on empty data";
|
||||
|
||||
pub fn columns_sorted(
|
||||
_group_by_name: Option<String>,
|
||||
_group_by_name: Option<Tagged<String>>,
|
||||
value: &Value,
|
||||
tag: impl Into<Tag>,
|
||||
) -> Vec<Tagged<String>> {
|
||||
@ -61,7 +61,7 @@ pub fn columns_sorted(
|
||||
}
|
||||
|
||||
pub fn t_sort(
|
||||
group_by_name: Option<String>,
|
||||
group_by_name: Option<Tagged<String>>,
|
||||
split_by_name: Option<String>,
|
||||
value: &Value,
|
||||
tag: impl Into<Tag>,
|
||||
@ -454,12 +454,13 @@ mod tests {
|
||||
}
|
||||
|
||||
fn nu_releases_grouped_by_date() -> Result<Value, ShellError> {
|
||||
let key = String::from("date").tagged_unknown();
|
||||
group(&key, nu_releases_commiters(), Tag::unknown())
|
||||
let key = Some(String::from("date").tagged_unknown());
|
||||
let sample = table(&nu_releases_committers());
|
||||
group(&key, &sample, Tag::unknown())
|
||||
}
|
||||
|
||||
fn nu_releases_sorted_by_date() -> Result<Value, ShellError> {
|
||||
let key = String::from("date");
|
||||
let key = String::from("date").tagged(Tag::unknown());
|
||||
|
||||
t_sort(
|
||||
Some(key),
|
||||
@ -481,7 +482,7 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
fn nu_releases_commiters() -> Vec<Value> {
|
||||
fn nu_releases_committers() -> Vec<Value> {
|
||||
vec![
|
||||
row(
|
||||
indexmap! {"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("August 23-2019")},
|
||||
@ -515,7 +516,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn show_columns_sorted_given_a_column_to_sort_by() -> Result<(), ShellError> {
|
||||
let by_column = String::from("date");
|
||||
let by_column = String::from("date").tagged(Tag::unknown());
|
||||
|
||||
assert_eq!(
|
||||
columns_sorted(
|
||||
@ -535,7 +536,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn sorts_the_tables() -> Result<(), ShellError> {
|
||||
let group_by = String::from("date");
|
||||
let group_by = String::from("date").tagged(Tag::unknown());
|
||||
|
||||
assert_eq!(
|
||||
t_sort(
|
||||
|
Reference in New Issue
Block a user