From de1c4e6c8894915825d6bd67cc7ed85d26b25be6 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Sun, 13 Oct 2019 22:50:45 +0200 Subject: [PATCH] Implements from-ssv --- src/cli.rs | 1 + src/commands/from_ssv.rs | 88 ++++++++++++++++++++++++++++++++++++++-- tests/filters_test.rs | 12 +++--- 3 files changed, 91 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 38e2474faf..5bfd7ff681 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -272,6 +272,7 @@ pub async fn cli() -> Result<(), Box> { whole_stream_command(Env), whole_stream_command(FromCSV), whole_stream_command(FromTSV), + whole_stream_command(FromSSV), whole_stream_command(FromINI), whole_stream_command(FromBSON), whole_stream_command(FromJSON), diff --git a/src/commands/from_ssv.rs b/src/commands/from_ssv.rs index 0da2b6f562..59ba2f2f8f 100644 --- a/src/commands/from_ssv.rs +++ b/src/commands/from_ssv.rs @@ -33,11 +33,91 @@ impl WholeStreamCommand for FromSSV { } } +fn from_ssv_string_to_value( + s: &str, + headerless: bool, + tag: impl Into, +) -> Result, &str> { + let mut lines = s.lines(); + let tag = tag.into(); + + let headers = lines + .next() + .expect("No content.") + .split_whitespace() + .map(|s| s.to_owned()) + .collect::>(); + + let header_row = if headerless { + (0..headers.len()) + .map(|i| format!("Column{}", i + 1)) + .collect::>() + } else { + headers + }; + + let rows = lines + .map(|l| { + let mut row = TaggedDictBuilder::new(tag); + for (column, value) in header_row.iter().zip(l.split_whitespace()) { + row.insert_tagged( + column.to_owned(), + Value::Primitive(Primitive::String(String::from(value))).tagged(tag), + ) + } + row.into_tagged_value() + }) + .collect(); + + Ok(Tagged::from_item(Value::Table(rows), tag)) +} + fn from_ssv( - FromSSVArgs { - headerless: headerless, - }: FromSSVArgs, + FromSSVArgs { headerless }: FromSSVArgs, RunnableContext { input, name, .. }: RunnableContext, ) -> Result { - unimplemented!() + let stream = async_stream! { + let values: Vec> = input.values.collect().await; + let mut concat_string = String::new(); + let mut latest_tag: Option = None; + + for value in values { + let value_tag = value.tag(); + latest_tag = Some(value_tag); + match value.item { + Value::Primitive(Primitive::String(s)) => { + concat_string.push_str(&s); + concat_string.push_str("\n"); + + } + _ => yield Err(ShellError::labeled_error_with_secondary ( + "Expected a string from pipeline", + "requires string input", + name, + "value originates from here", + value_tag + )), + } + } + + match from_ssv_string_to_value(&concat_string, headerless, name) { + Ok(x) => match x { + Tagged { item: Value::Table(list), ..} => { + for l in list { yield ReturnSuccess::value(l) } + } + x => yield ReturnSuccess::value(x) + }, + Err(_) => if let Some(last_tag) = latest_tag { + yield Err(ShellError::labeled_error_with_secondary( + "Could not parse as SSV", + "input cannot be parsed ssv", + name, + "value originates from here", + last_tag, + )) + } + } + }; + + Ok(stream.to_output_stream()) } diff --git a/tests/filters_test.rs b/tests/filters_test.rs index 70fd752967..ed841af4ca 100644 --- a/tests/filters_test.rs +++ b/tests/filters_test.rs @@ -359,7 +359,7 @@ fn converts_from_tsv_text_skipping_headers_to_structured_table() { fn converts_from_ssv_text_to_structured_table() { Playground::setup("filter_from_ssv_test_1", |dirs, sandbox| { sandbox.with_files(vec![FileWithContentToBeTrimmed( - "oc_get_svc.ssv", + "oc_get_svc.txt", r#" NAME LABELS SELECTOR IP PORT(S) docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP @@ -371,15 +371,15 @@ fn converts_from_ssv_text_to_structured_table() { let actual = nu!( cwd: dirs.test(), h::pipeline( r#" - open oc_get_svc.ssv + open oc_get_svc.txt | from-ssv | nth 0 - | get NAME + | get IP | echo $it "# )); - assert_eq!(actual, "docker-registry"); + assert_eq!(actual, "172.30.78.158"); }) } @@ -387,7 +387,7 @@ fn converts_from_ssv_text_to_structured_table() { fn converts_from_ssv_text_skipping_headers_to_structured_table() { Playground::setup("filter_from_ssv_test_2", |dirs, sandbox| { sandbox.with_files(vec![FileWithContentToBeTrimmed( - "oc_get_svc.ssv", + "oc_get_svc.txt", r#" NAME LABELS SELECTOR IP PORT(S) docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP @@ -399,7 +399,7 @@ fn converts_from_ssv_text_skipping_headers_to_structured_table() { let actual = nu!( cwd: dirs.test(), h::pipeline( r#" - open oc_get_svc.ssv + open oc_get_svc.txt | from-ssv --headerless | nth 2 | get Column2