2019-10-29 22:04:31 +01:00
|
|
|
use crate::prelude::*;
|
2020-09-25 05:44:24 +02:00
|
|
|
use crate::utils::suggestions::suggestions;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::WholeStreamCommand;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-06-17 19:33:50 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tagged;
|
2020-06-17 19:33:50 +02:00
|
|
|
use nu_value_ext::as_string;
|
2019-10-29 22:04:31 +01:00
|
|
|
|
|
|
|
pub struct SplitBy;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct SplitByArgs {
|
2020-06-17 19:33:50 +02:00
|
|
|
column_name: Option<Tagged<String>>,
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2019-10-29 22:04:31 +01:00
|
|
|
impl WholeStreamCommand for SplitBy {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-by"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-06-17 19:33:50 +02:00
|
|
|
Signature::build("split-by").optional(
|
2019-10-29 22:04:31 +01:00
|
|
|
"column_name",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the name of the column within the nested table to split by",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-02-12 03:24:31 +01:00
|
|
|
"Creates a new table with the data from the inner tables split by the column given."
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
split_by(args).await
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
pub async fn split_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-06-04 10:42:23 +02:00
|
|
|
let name = args.call_info.name_tag.clone();
|
2020-12-18 08:53:49 +01:00
|
|
|
let (SplitByArgs { column_name }, input) = args.process().await?;
|
2020-06-04 10:42:23 +02:00
|
|
|
let values: Vec<Value> = input.collect().await;
|
2019-10-29 22:04:31 +01:00
|
|
|
|
2020-06-04 10:42:23 +02:00
|
|
|
if values.len() > 1 || values.is_empty() {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"Expected table from pipeline",
|
|
|
|
"requires a table input",
|
2020-06-17 19:33:50 +02:00
|
|
|
name,
|
2020-06-04 10:42:23 +02:00
|
|
|
));
|
|
|
|
}
|
2019-10-29 22:04:31 +01:00
|
|
|
|
2020-08-03 00:34:33 +02:00
|
|
|
let split = split(&column_name, &values[0], &name)?;
|
|
|
|
Ok(OutputStream::one(ReturnSuccess::value(split)))
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:33:50 +02:00
|
|
|
enum Grouper {
|
|
|
|
ByColumn(Option<Tagged<String>>),
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:04:31 +01:00
|
|
|
pub fn split(
|
2020-06-17 19:33:50 +02:00
|
|
|
column_name: &Option<Tagged<String>>,
|
|
|
|
values: &Value,
|
2019-10-29 22:04:31 +01:00
|
|
|
tag: impl Into<Tag>,
|
2019-11-21 15:33:14 +01:00
|
|
|
) -> Result<Value, ShellError> {
|
2020-06-17 19:33:50 +02:00
|
|
|
let name = tag.into();
|
|
|
|
|
|
|
|
let grouper = if let Some(column_name) = column_name {
|
|
|
|
Grouper::ByColumn(Some(column_name.clone()))
|
|
|
|
} else {
|
|
|
|
Grouper::ByColumn(None)
|
|
|
|
};
|
|
|
|
|
|
|
|
match grouper {
|
|
|
|
Grouper::ByColumn(Some(column_name)) => {
|
2020-07-14 15:45:19 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| {
|
2020-06-17 19:33:50 +02:00
|
|
|
match row.get_data_by_key(column_name.borrow_spanned()) {
|
|
|
|
Some(group_key) => Ok(as_string(&group_key)?),
|
|
|
|
None => Err(suggestions(column_name.borrow_tagged(), &row)),
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
2020-06-17 19:33:50 +02:00
|
|
|
});
|
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::split(&values, &Some(block), &name)
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
2020-06-17 19:33:50 +02:00
|
|
|
Grouper::ByColumn(None) => {
|
2020-07-25 14:40:35 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| as_string(row));
|
2020-06-17 19:33:50 +02:00
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::split(&values, &Some(block), &name)
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
2019-10-29 22:04:31 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-06-17 19:33:50 +02:00
|
|
|
use super::split;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-10-20 11:07:13 +02:00
|
|
|
use nu_data::utils::helpers::committers_grouped_by_date;
|
2020-08-04 00:47:19 +02:00
|
|
|
use nu_protocol::UntaggedValue;
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::*;
|
2020-10-20 11:07:13 +02:00
|
|
|
use nu_test_support::value::{date, int, row, string, table};
|
2019-10-29 22:04:31 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-08-04 00:47:19 +02:00
|
|
|
fn splits_inner_tables_by_key() {
|
2020-06-17 19:33:50 +02:00
|
|
|
let for_key = Some(String::from("country").tagged_unknown());
|
2019-10-29 22:04:31 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-08-04 00:47:19 +02:00
|
|
|
split(&for_key, &committers_grouped_by_date(), Tag::unknown()).unwrap(),
|
2019-12-04 20:52:31 +01:00
|
|
|
UntaggedValue::row(indexmap! {
|
2019-10-29 22:04:31 +01:00
|
|
|
"EC".into() => row(indexmap! {
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-07-23".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-07-23"), "chickens".into() => int(10)})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-09-24".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-09-24"), "chickens".into() => int(20)})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-10-10".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-10-10"), "chickens".into() => int(30)})
|
2019-10-29 22:04:31 +01:00
|
|
|
])
|
|
|
|
}),
|
|
|
|
"NZ".into() => row(indexmap! {
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-07-23".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-07-23"), "chickens".into() => int(5)})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-09-24".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-09-24"), "chickens".into() => int(10)})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-10-10".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-10-10"), "chickens".into() => int(15)})
|
2019-10-29 22:04:31 +01:00
|
|
|
])
|
|
|
|
}),
|
|
|
|
"US".into() => row(indexmap! {
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-07-23".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-07-23"), "chickens".into() => int(2)})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-09-24".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-09-24"), "chickens".into() => int(4)})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-10-10".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-10-10"), "chickens".into() => int(6)})
|
2019-10-29 22:04:31 +01:00
|
|
|
])
|
|
|
|
})
|
2019-11-21 15:33:14 +01:00
|
|
|
}).into_untagged_value()
|
2019-10-29 22:04:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn errors_if_key_within_some_inner_table_is_missing() {
|
2020-06-17 19:33:50 +02:00
|
|
|
let for_key = Some(String::from("country").tagged_unknown());
|
2019-10-29 22:04:31 +01:00
|
|
|
|
|
|
|
let nu_releases = row(indexmap! {
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-07-23".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => string("2019-07-23")})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-09-24".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => UntaggedValue::string("JT").into_value(Tag::from(Span::new(5,10))), "date".into() => string("2019-09-24")})
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2019-12-31 08:36:08 +01:00
|
|
|
"October 10-2019".into() => table(&[
|
2019-10-29 22:04:31 +01:00
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => string("October 10-2019")})
|
|
|
|
])
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(split(&for_key, &nu_releases, Tag::from(Span::new(5, 10))).is_err());
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-05-18 14:56:01 +02:00
|
|
|
use super::SplitBy;
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
Ok(test_examples(SplitBy {})?)
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|