Declare input and output types of commands (#6796)

* Add failing test that list of ints and floats is List<Number>

* Start defining subtype relation

* Make it possible to declare input and output types for commands

- Enforce them in tests

* Declare input and output types of commands

* Add formatted signatures to `help commands` table

* Revert SyntaxShape::Table -> Type::Table change

* Revert unnecessary derive(Hash) on SyntaxShape

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
Dan Davison
2022-11-09 16:55:05 -05:00
committed by GitHub
parent f878276de7
commit df94052180
238 changed files with 2315 additions and 756 deletions

View File

@ -1,7 +1,7 @@
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Value,
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -13,11 +13,14 @@ impl Command for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("split chars").category(Category::Strings)
Signature::build("split chars")
.input_output_types(vec![(Type::String, Type::List(Box::new(Type::String)))])
.vectorizes_over_list(true)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Split a string's characters into separate rows"
"Split a string into a list of characters"
}
fn search_terms(&self) -> Vec<&str> {
@ -26,7 +29,7 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Split the string's characters into separate rows",
description: "Split the string into a list of characters",
example: "'hello' | split chars",
result: Some(Value::List {
vals: vec![

View File

@ -2,7 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
#[derive(Clone)]
@ -15,6 +16,14 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("split column")
.input_output_types(vec![
(Type::String, Type::Table(vec![])),
(
// TODO: no test coverage (is this behavior a bug or a feature?)
Type::List(Box::new(Type::String)),
Type::Table(vec![]),
),
])
.required(
"separator",
SyntaxShape::String,
@ -71,7 +80,7 @@ impl Command for SubCommand {
},
Example {
description: "Split a string into columns of char and remove the empty columns",
example: "echo 'abc' | split column -c ''",
example: "'abc' | split column -c ''",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec![
@ -89,6 +98,25 @@ impl Command for SubCommand {
span: Span::test_data(),
}),
},
Example {
description: "Split a list of strings into a table",
example: "['a-b' 'c-d'] | split column -",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["column1".to_string(), "column2".to_string()],
vals: vec![Value::test_string("a"), Value::test_string("b")],
span: Span::test_data(),
},
Value::Record {
cols: vec!["column1".to_string(), "column2".to_string()],
vals: vec![Value::test_string("c"), Value::test_string("d")],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
}
}
@ -161,15 +189,14 @@ fn split_column_helper(
}
}
// #[cfg(test)]
// mod tests {
// use super::ShellError;
// use super::SubCommand;
#[cfg(test)]
mod test {
use super::*;
// #[test]
// fn examples_work_as_expected() -> Result<(), ShellError> {
// use crate::examples::test as test_examples;
#[test]
fn test_examples() {
use crate::test_examples;
// test_examples(SubCommand {})
// }
// }
test_examples(SubCommand {})
}
}

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value,
Category, Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -15,6 +15,10 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("split list")
.input_output_types(vec![(
Type::List(Box::new(Type::Any)),
Type::List(Box::new(Type::List(Box::new(Type::Any)))),
)])
.required(
"separator",
SyntaxShape::Any,
@ -128,3 +132,15 @@ fn split_list(
}
.into_pipeline_data())
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -2,7 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
Value,
};
#[derive(Clone)]
@ -15,6 +16,8 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("split row")
.input_output_types(vec![(Type::String, Type::List(Box::new(Type::String)))])
.vectorizes_over_list(true)
.required(
"separator",
SyntaxShape::String,
@ -133,15 +136,14 @@ fn split_row_helper(
}
}
// #[cfg(test)]
// mod tests {
// use super::ShellError;
// use super::SubCommand;
#[cfg(test)]
mod test {
use super::*;
// #[test]
// fn examples_work_as_expected() -> Result<(), ShellError> {
// use crate::examples::test as test_examples;
#[test]
fn test_examples() {
use crate::test_examples;
// test_examples(SubCommand {})
// }
// }
test_examples(SubCommand {})
}
}

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -16,6 +16,8 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("split words")
.input_output_types(vec![(Type::String, Type::List(Box::new(Type::String)))])
.vectorizes_over_list(true)
.category(Category::Strings)
// .switch(
// "ignore-hyphenated",