Finish updating the last cases

This commit is contained in:
Jonathan Turner 2019-08-20 18:11:11 +12:00
parent 421aacee76
commit 64c129d65f
7 changed files with 48 additions and 33 deletions

View File

@ -5,7 +5,7 @@ use crate::prelude::*;
#[derive(Deserialize)] #[derive(Deserialize)]
struct NthArgs { struct NthArgs {
position: Tagged<i64>, amount: Tagged<i64>,
} }
pub struct Nth; pub struct Nth;
@ -24,12 +24,12 @@ impl WholeStreamCommand for Nth {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("nth").optional("amount", SyntaxType::Any) Signature::build("nth").required("amount", SyntaxType::Any)
} }
} }
fn nth( fn nth(
NthArgs { position: amount }: NthArgs, NthArgs { amount }: NthArgs,
RunnableContext { input, .. }: RunnableContext, RunnableContext { input, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
Ok(OutputStream::from_input( Ok(OutputStream::from_input(

View File

@ -31,8 +31,16 @@ impl WholeStreamCommand for Pick {
fn pick( fn pick(
PickArgs { rest: fields }: PickArgs, PickArgs { rest: fields }: PickArgs,
RunnableContext { input, .. }: RunnableContext, RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
if fields.len() == 0 {
return Err(ShellError::labeled_error(
"Pick requires fields",
"needs parameter",
name,
));
}
let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect();
let objects = input let objects = input

View File

@ -30,8 +30,16 @@ impl WholeStreamCommand for Reject {
fn reject( fn reject(
RejectArgs { rest: fields }: RejectArgs, RejectArgs { rest: fields }: RejectArgs,
RunnableContext { input, .. }: RunnableContext, RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
if fields.len() == 0 {
return Err(ShellError::labeled_error(
"Reject requires fields",
"needs parameter",
name,
));
}
let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect(); let fields: Vec<_> = fields.iter().map(|f| f.item.clone()).collect();
let stream = input let stream = input

View File

@ -6,6 +6,7 @@ use log::trace;
#[derive(Deserialize)] #[derive(Deserialize)]
struct SplitColumnArgs { struct SplitColumnArgs {
separator: Tagged<String>,
rest: Vec<Tagged<String>>, rest: Vec<Tagged<String>>,
} }
@ -25,32 +26,30 @@ impl WholeStreamCommand for SplitColumn {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
// TODO: Improve error. Old error had extra info: Signature::build("split-column")
// .required("separator", SyntaxType::Any)
// needs parameter (e.g. split-column ",") .rest()
Signature::build("split-column").rest()
} }
} }
fn split_column( fn split_column(
SplitColumnArgs { rest: positional }: SplitColumnArgs, SplitColumnArgs { separator, rest }: SplitColumnArgs,
RunnableContext { input, name, .. }: RunnableContext, RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
Ok(input Ok(input
.values .values
.map(move |v| match v.item { .map(move |v| match v.item {
Value::Primitive(Primitive::String(ref s)) => { Value::Primitive(Primitive::String(ref s)) => {
let positional: Vec<_> = positional.iter().map(|f| f.item.clone()).collect(); let splitter = separator.replace("\\n", "\n");
// TODO: Require at least 1 positional argument.
let splitter = positional[0].replace("\\n", "\n");
trace!("splitting with {:?}", splitter); trace!("splitting with {:?}", splitter);
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
trace!("split result = {:?}", split_result); trace!("split result = {:?}", split_result);
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
// If they didn't provide column names, make up our own // If they didn't provide column names, make up our own
if (positional.len() - 1) == 0 { if positional.len() == 0 {
let mut gen_columns = vec![]; let mut gen_columns = vec![];
for i in 0..split_result.len() { for i in 0..split_result.len() {
gen_columns.push(format!("Column{}", i + 1)); gen_columns.push(format!("Column{}", i + 1));
@ -62,16 +61,16 @@ fn split_column(
} }
ReturnSuccess::value(dict.into_tagged_value()) ReturnSuccess::value(dict.into_tagged_value())
} else if split_result.len() == (positional.len() - 1) { } else if split_result.len() == positional.len() {
let mut dict = TaggedDictBuilder::new(v.tag()); let mut dict = TaggedDictBuilder::new(v.tag());
for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) { for (&k, v) in split_result.iter().zip(positional.iter()) {
dict.insert(v, Value::Primitive(Primitive::String(k.into()))); dict.insert(v, Value::Primitive(Primitive::String(k.into())));
} }
ReturnSuccess::value(dict.into_tagged_value()) ReturnSuccess::value(dict.into_tagged_value())
} else { } else {
let mut dict = TaggedDictBuilder::new(v.tag()); let mut dict = TaggedDictBuilder::new(v.tag());
for k in positional.iter().skip(1) { for (&k, v) in split_result.iter().zip(positional.iter()) {
dict.insert(k.trim(), Primitive::String("".into())); dict.insert(v, Value::Primitive(Primitive::String(k.into())));
} }
ReturnSuccess::value(dict.into_tagged_value()) ReturnSuccess::value(dict.into_tagged_value())
} }

View File

@ -6,7 +6,7 @@ use log::trace;
#[derive(Deserialize)] #[derive(Deserialize)]
struct SplitRowArgs { struct SplitRowArgs {
rest: Vec<Tagged<String>>, separator: Tagged<String>,
} }
pub struct SplitRow; pub struct SplitRow;
@ -25,22 +25,19 @@ impl WholeStreamCommand for SplitRow {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
// TODO: Improve error. Old error had extra info: Signature::build("split-row").required("separator", SyntaxType::Any)
//
// needs parameter (e.g. split-row ",")
Signature::build("split-row").rest()
} }
} }
fn split_row( fn split_row(
SplitRowArgs { rest: positional }: SplitRowArgs, SplitRowArgs { separator }: SplitRowArgs,
RunnableContext { input, name, .. }: RunnableContext, RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let stream = input let stream = input
.values .values
.map(move |v| match v.item { .map(move |v| match v.item {
Value::Primitive(Primitive::String(ref s)) => { Value::Primitive(Primitive::String(ref s)) => {
let splitter = positional[0].item.replace("\\n", "\n"); let splitter = separator.item.replace("\\n", "\n");
trace!("splitting with {:?}", splitter); trace!("splitting with {:?}", splitter);
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect(); let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();

View File

@ -22,11 +22,13 @@ impl WholeStreamCommand for ToArray {
} }
} }
fn to_array(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> { fn to_array(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
let span = args.call_info.name_span;
let out = args.input.values.collect(); let out = args.input.values.collect();
Ok(out Ok(out
.map(|vec: Vec<_>| stream![Value::List(vec).tagged_unknown()]) // TODO: args.input should have a span .map(move |vec: Vec<_>| stream![Value::List(vec).simple_spanned(span)])
.flatten_stream() .flatten_stream()
.from_input_stream()) .from_input_stream())
} }

View File

@ -186,13 +186,14 @@ impl ShellError {
ArgumentError::MissingMandatoryPositional(name) => Diagnostic::new( ArgumentError::MissingMandatoryPositional(name) => Diagnostic::new(
Severity::Error, Severity::Error,
format!( format!(
"{} requires {}", "{} requires {} parameter",
Color::Cyan.paint(command), Color::Cyan.paint(command),
Color::Green.bold().paint(name) Color::Green.bold().paint(name.clone())
), ),
) )
.with_label(Label::new_primary(span)), .with_label(
Label::new_primary(span).with_message(format!("requires {} parameter", name)),
),
ArgumentError::MissingValueForName(name) => Diagnostic::new( ArgumentError::MissingValueForName(name) => Diagnostic::new(
Severity::Error, Severity::Error,
format!( format!(