2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-25 01:20:03 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-08-01 01:58:42 +00:00
|
|
|
use crate::object::{Primitive, TaggedDictBuilder, Value};
|
2019-05-25 01:20:03 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-01 17:00:42 +00:00
|
|
|
use log::trace;
|
2019-05-25 01:20:03 +00:00
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct SplitColumn;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SplitColumn {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
split_column(args, registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-column"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
// TODO: Signature?
|
|
|
|
// TODO: Improve error. Old error had extra info:
|
|
|
|
//
|
|
|
|
// needs parameter (e.g. split-column ",")
|
|
|
|
Signature::build("split-column").required("delimeter", SyntaxType::Any)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2019-07-23 22:22:11 +00:00
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let span = args.name_span();
|
|
|
|
let (input, args) = args.parts();
|
|
|
|
|
|
|
|
let positional: Vec<_> = args.positional.iter().flatten().cloned().collect();
|
2019-07-03 20:31:15 +00:00
|
|
|
|
2019-05-25 01:20:03 +00:00
|
|
|
Ok(input
|
2019-07-03 20:31:15 +00:00
|
|
|
.values
|
2019-07-08 16:44:53 +00:00
|
|
|
.map(move |v| match v.item {
|
2019-08-01 01:58:42 +00:00
|
|
|
Value::Primitive(Primitive::String(ref s)) => {
|
2019-06-22 03:43:37 +00:00
|
|
|
let splitter = positional[0].as_string().unwrap().replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-25 01:20:03 +00:00
|
|
|
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
|
|
|
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-26 06:54:41 +00:00
|
|
|
|
2019-05-28 02:01:37 +00:00
|
|
|
// If they didn't provide column names, make up our own
|
2019-06-22 03:43:37 +00:00
|
|
|
if (positional.len() - 1) == 0 {
|
2019-05-28 02:01:37 +00:00
|
|
|
let mut gen_columns = vec![];
|
|
|
|
for i in 0..split_result.len() {
|
|
|
|
gen_columns.push(format!("Column{}", i + 1));
|
|
|
|
}
|
|
|
|
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-06-22 20:46:16 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert(v.clone(), Primitive::String(k.into()));
|
2019-05-28 02:01:37 +00:00
|
|
|
}
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-06-22 03:43:37 +00:00
|
|
|
} else if split_result.len() == (positional.len() - 1) {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-06-22 20:46:16 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) {
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert(
|
2019-05-26 06:54:41 +00:00
|
|
|
v.as_string().unwrap(),
|
2019-06-22 20:46:16 +00:00
|
|
|
Value::Primitive(Primitive::String(k.into())),
|
2019-05-26 06:54:41 +00:00
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
} else {
|
2019-08-05 08:54:29 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(v.tag());
|
2019-06-22 03:43:37 +00:00
|
|
|
for k in positional.iter().skip(1) {
|
2019-07-09 04:31:26 +00:00
|
|
|
dict.insert(k.as_string().unwrap().trim(), Primitive::String("".into()));
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-08-01 01:58:42 +00:00
|
|
|
ReturnSuccess::value(dict.into_tagged_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 08:54:29 +00:00
|
|
|
_ => Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-06-15 23:03:49 +00:00
|
|
|
span,
|
2019-08-05 08:54:29 +00:00
|
|
|
"value originates from here",
|
|
|
|
v.span(),
|
2019-07-03 20:31:15 +00:00
|
|
|
)),
|
2019-05-25 01:20:03 +00:00
|
|
|
})
|
2019-07-03 20:31:15 +00:00
|
|
|
.to_output_stream())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|