2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 18:23:42 +02:00
|
|
|
use crate::data::{Primitive, TaggedDictBuilder, Value};
|
2019-09-11 16:36:50 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-05-25 03:20:03 +02:00
|
|
|
use crate::prelude::*;
|
2019-06-01 19:00:42 +02:00
|
|
|
use log::trace;
|
2019-05-25 03:20:03 +02:00
|
|
|
|
2019-08-20 05:15:05 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SplitColumnArgs {
|
2019-08-20 08:11:11 +02:00
|
|
|
separator: Tagged<String>,
|
2019-08-20 05:15:05 +02:00
|
|
|
rest: Vec<Tagged<String>>,
|
2019-08-27 13:30:09 +02:00
|
|
|
#[serde(rename(deserialize = "collapse-empty"))]
|
|
|
|
collapse_empty: bool,
|
2019-08-20 05:15:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct SplitColumn;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SplitColumn {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-column"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-08-20 08:11:11 +02:00
|
|
|
Signature::build("split-column")
|
2019-09-14 18:30:24 +02:00
|
|
|
.required("separator", SyntaxShape::Any)
|
2019-08-27 13:30:09 +02:00
|
|
|
.switch("collapse-empty")
|
2019-09-14 18:30:24 +02:00
|
|
|
.rest(SyntaxShape::Member)
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Split row contents across multiple columns via the separator."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, split_column)?.run()
|
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 05:15:05 +02:00
|
|
|
fn split_column(
|
2019-09-11 16:36:50 +02:00
|
|
|
SplitColumnArgs {
|
|
|
|
separator,
|
|
|
|
rest,
|
|
|
|
collapse_empty,
|
|
|
|
}: SplitColumnArgs,
|
2019-08-20 05:15:05 +02:00
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-05-25 03:20:03 +02:00
|
|
|
Ok(input
|
2019-07-03 22:31:15 +02:00
|
|
|
.values
|
2019-07-08 18:44:53 +02:00
|
|
|
.map(move |v| match v.item {
|
2019-08-01 03:58:42 +02:00
|
|
|
Value::Primitive(Primitive::String(ref s)) => {
|
2019-08-20 08:11:11 +02:00
|
|
|
let splitter = separator.replace("\\n", "\n");
|
2019-06-01 19:00:42 +02:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-25 03:20:03 +02:00
|
|
|
|
2019-08-27 13:30:09 +02:00
|
|
|
let split_result: Vec<_> = if collapse_empty {
|
|
|
|
s.split(&splitter).filter(|s| !s.is_empty()).collect()
|
2019-08-26 13:45:29 +02:00
|
|
|
} else {
|
|
|
|
s.split(&splitter).collect()
|
|
|
|
};
|
|
|
|
|
2019-06-01 19:00:42 +02:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-26 08:54:41 +02:00
|
|
|
|
2019-08-20 08:11:11 +02:00
|
|
|
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
|
|
|
|
2019-05-28 04:01:37 +02:00
|
|
|
// If they didn't provide column names, make up our own
|
2019-08-20 08:11:11 +02:00
|
|
|
if positional.len() == 0 {
|
2019-05-28 04:01:37 +02:00
|
|
|
let mut gen_columns = vec![];
|
|
|
|
for i in 0..split_result.len() {
|
|
|
|
gen_columns.push(format!("Column{}", i + 1));
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-06-22 22:46:16 +02:00
|
|
|
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
|
2019-07-09 06:31:26 +02:00
|
|
|
dict.insert(v.clone(), Primitive::String(k.into()));
|
2019-05-28 04:01:37 +02:00
|
|
|
}
|
2019-07-09 06:31:26 +02:00
|
|
|
|
2019-08-01 03:58:42 +02:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-08-20 08:11:11 +02:00
|
|
|
} else if split_result.len() == positional.len() {
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-08-20 08:11:11 +02:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
2019-08-20 05:15:05 +02:00
|
|
|
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
|
2019-05-25 03:20:03 +02:00
|
|
|
}
|
2019-08-01 03:58:42 +02:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-05-25 03:20:03 +02:00
|
|
|
} else {
|
2019-08-05 10:54:29 +02:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-08-20 08:11:11 +02:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
|
|
|
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
|
2019-05-25 03:20:03 +02:00
|
|
|
}
|
2019-08-01 03:58:42 +02:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-05-25 03:20:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 10:54:29 +02:00
|
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-10-13 06:12:43 +02:00
|
|
|
&name,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
2019-09-14 18:30:24 +02:00
|
|
|
v.tag(),
|
2019-07-03 22:31:15 +02:00
|
|
|
)),
|
2019-05-25 03:20:03 +02:00
|
|
|
})
|
2019-07-03 22:31:15 +02:00
|
|
|
.to_output_stream())
|
2019-05-25 03:20:03 +02:00
|
|
|
}
|