Merge pull request #109 from nushell/split_column_row

Port split column and split row
This commit is contained in:
JT 2021-10-09 17:18:12 +13:00 committed by GitHub
commit ddea4b416d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 325 additions and 56 deletions

View file

@ -43,6 +43,8 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(Select));
working_set.add_decl(Box::new(Split));
working_set.add_decl(Box::new(SplitChars));
working_set.add_decl(Box::new(SplitColumn));
working_set.add_decl(Box::new(SplitRow));
working_set.add_decl(Box::new(Sys));
working_set.add_decl(Box::new(Table));
working_set.add_decl(Box::new(Touch));

View file

@ -65,69 +65,21 @@ fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_proto
Value::List { vals, span } => Value::List {
vals: vals
.iter()
.flat_map(move |v| {
if let Ok(s) = v.as_string() {
let v_span = v.span();
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
})
.flat_map(|x| split_chars_helper(x, name))
.collect(),
span,
},
Value::Stream { stream, span } => Value::Stream {
stream: stream
.flat_map(move |v| {
if let Ok(s) = v.as_string() {
let v_span = v.span();
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
})
.flat_map(move |x| split_chars_helper(&x, name))
.into_value_stream(),
span,
},
v => {
let v_span = v.span();
if let Ok(s) = v.as_string() {
if v.as_string().is_ok() {
Value::List {
vals: s
.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect(),
vals: split_chars_helper(&v, name),
span: v_span,
}
} else {
@ -143,6 +95,28 @@ fn split_chars(call: &Call, input: Value) -> Result<nu_protocol::Value, nu_proto
})
}
fn split_chars_helper(v: &Value, name: Span) -> Vec<Value> {
if let Ok(s) = v.as_string() {
let v_span = v.span();
s.chars()
.collect::<Vec<_>>()
.into_iter()
.map(move |x| Value::String {
val: x.to_string(),
span: v_span,
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
}
// #[cfg(test)]
// mod tests {
// use super::ShellError;

View file

@ -0,0 +1,164 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
IntoValueStream, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"split column"
}
fn signature(&self) -> Signature {
Signature::build("split column")
.required(
"separator",
SyntaxShape::String,
"the character that denotes what separates columns",
)
.switch("collapse-empty", "remove empty columns", Some('c'))
.rest(
"rest",
SyntaxShape::String,
"column names to give the new columns",
)
}
fn usage(&self) -> &str {
"splits contents across multiple columns via the separator."
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
split_column(context, call, input)
}
}
fn split_column(
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let name_span = call.head;
let separator: Spanned<String> = call.req(context, 0)?;
let rest: Vec<Spanned<String>> = call.rest(context, 1)?;
let collapse_empty = call.has_flag("collapse-empty");
Ok(match input {
Value::List { vals, span } => Value::List {
vals: vals
.iter()
.map(move |x| split_column_helper(x, &separator, &rest, collapse_empty, name_span))
.collect(),
span,
},
Value::Stream { stream, span } => Value::Stream {
stream: stream
.map(move |x| split_column_helper(&x, &separator, &rest, collapse_empty, name_span))
.into_value_stream(),
span,
},
v => {
if v.as_string().is_ok() {
Value::List {
vals: vec![split_column_helper(
&v,
&separator,
&rest,
collapse_empty,
name_span,
)],
span: call.head,
}
} else {
Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: call.head,
origin: v.span(),
},
}
}
}
})
}
fn split_column_helper(
v: &Value,
separator: &Spanned<String>,
rest: &[Spanned<String>],
collapse_empty: bool,
head: Span,
) -> Value {
if let Ok(s) = v.as_string() {
let splitter = separator.item.replace("\\n", "\n");
let split_result: Vec<_> = if collapse_empty {
s.split(&splitter).filter(|s| !s.is_empty()).collect()
} else {
s.split(&splitter).collect()
};
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
// If they didn't provide column names, make up our own
let mut cols = vec![];
let mut vals = vec![];
if positional.is_empty() {
let mut gen_columns = vec![];
for i in 0..split_result.len() {
gen_columns.push(format!("Column{}", i + 1));
}
for (&k, v) in split_result.iter().zip(&gen_columns) {
cols.push(v.to_string());
vals.push(Value::String {
val: k.into(),
span: head,
});
}
} else {
for (&k, v) in split_result.iter().zip(&positional) {
cols.push(v.into());
vals.push(Value::String {
val: k.into(),
span: head,
})
}
}
Value::Record {
cols,
vals,
span: head,
}
} else {
Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: head,
origin: v.span(),
},
}
}
}
// #[cfg(test)]
// mod tests {
// use super::ShellError;
// use super::SubCommand;
// #[test]
// fn examples_work_as_expected() -> Result<(), ShellError> {
// use crate::examples::test as test_examples;
// test_examples(SubCommand {})
// }
// }

View file

@ -1,9 +1,9 @@
pub mod chars;
// pub mod column;
pub mod column;
pub mod command;
// pub mod row;
pub mod row;
pub use chars::SubCommand as SplitChars;
// pub use column::SubCommand as SplitColumn;
pub use column::SubCommand as SplitColumn;
pub use command::SplitCommand as Split;
// pub use row::SubCommand as SplitRow;
pub use row::SubCommand as SplitRow;

View file

@ -0,0 +1,116 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EvaluationContext},
IntoValueStream, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"split row"
}
fn signature(&self) -> Signature {
Signature::build("split row").required(
"separator",
SyntaxShape::String,
"the character that denotes what separates rows",
)
}
fn usage(&self) -> &str {
"splits contents over multiple rows via the separator."
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
split_row(context, call, input)
}
}
fn split_row(
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let name_span = call.head;
let separator: Spanned<String> = call.req(context, 0)?;
Ok(match input {
Value::List { vals, span } => Value::List {
vals: vals
.iter()
.flat_map(move |x| split_row_helper(x, &separator, name_span))
.collect(),
span,
},
Value::Stream { stream, span } => Value::Stream {
stream: stream
.flat_map(move |x| split_row_helper(&x, &separator, name_span))
.into_value_stream(),
span,
},
v => {
let v_span = v.span();
if v.as_string().is_ok() {
Value::List {
vals: split_row_helper(&v, &separator, name_span),
span: v_span,
}
} else {
Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: call.head,
origin: v.span(),
},
}
}
}
})
}
fn split_row_helper(v: &Value, separator: &Spanned<String>, name: Span) -> Vec<Value> {
if let Ok(s) = v.as_string() {
let splitter = separator.item.replace("\\n", "\n");
s.split(&splitter)
.filter_map(|s| {
if s.trim() != "" {
Some(Value::String {
val: s.into(),
span: v.span(),
})
} else {
None
}
})
.collect()
} else {
vec![Value::Error {
error: ShellError::PipelineMismatch {
expected: Type::String,
expected_span: name,
origin: v.span(),
},
}]
}
}
// #[cfg(test)]
// mod tests {
// use super::ShellError;
// use super::SubCommand;
// #[test]
// fn examples_work_as_expected() -> Result<(), ShellError> {
// use crate::examples::test as test_examples;
// test_examples(SubCommand {})
// }
// }

View file

@ -540,3 +540,16 @@ fn string_cell_path() -> TestResult {
"c",
)
}
#[test]
fn split_row() -> TestResult {
run_test(r#""hello world" | split row " " | get 1"#, "world")
}
#[test]
fn split_column() -> TestResult {
run_test(
r#""hello world" | split column " " | get "Column1".0"#,
"hello",
)
}