From cb5d997adf361c6a4d1405d7913d5ed4dd06dd1e Mon Sep 17 00:00:00 2001 From: Reilly Wood <26268125+rgwood@users.noreply.github.com> Date: Mon, 14 Mar 2022 13:32:33 -0700 Subject: [PATCH] Change update help+examples for creating new columns (#4849) * Change update help/examples for creating new column * Enable example tests for update command --- crates/nu-command/src/filters/update.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/filters/update.rs b/crates/nu-command/src/filters/update.rs index a70a17e50..f7c6a85a3 100644 --- a/crates/nu-command/src/filters/update.rs +++ b/crates/nu-command/src/filters/update.rs @@ -19,7 +19,7 @@ impl Command for Update { .required( "field", SyntaxShape::CellPath, - "the name of the column to update", + "the name of the column to update or create", ) .required( "replacement value", @@ -30,7 +30,7 @@ impl Command for Update { } fn usage(&self) -> &str { - "Update an existing column to have a new value." + "Update an existing column to have a new value, or create a new column." } fn run( @@ -48,13 +48,17 @@ impl Command for Update { description: "Update a column value", example: "echo {'name': 'nu', 'stars': 5} | update name 'Nushell'", result: Some(Value::Record { cols: vec!["name".into(), "stars".into()], vals: vec![Value::test_string("Nushell"), Value::test_int(5)], span: Span::test_data()}), + }, Example { + description: "Add a new column", + example: "echo {'name': 'nu', 'stars': 5} | update language 'Rust'", + result: Some(Value::Record { cols: vec!["name".into(), "stars".into(), "language".into()], vals: vec![Value::test_string("nu"), Value::test_int(5), Value::test_string("Rust")], span: Span::test_data()}), }, Example { description: "Use in block form for more involved updating logic", example: "echo [[count fruit]; [1 'apple']] | update count {|f| $f.count + 1}", result: Some(Value::List { vals: vec![Value::Record { cols: vec!["count".into(), "fruit".into()], vals: vec![Value::test_int(2), Value::test_string("apple")], span: Span::test_data()}], span: Span::test_data()}), }, Example { description: "Use in block form for more involved updating logic", - example: "echo [[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors { get authors | str collect ',' }", + example: "echo [[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|a| $a.authors | str collect ','}", result: Some(Value::List { vals: vec![Value::Record { cols: vec!["project".into(), "authors".into()], vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")], span: Span::test_data()}], span: Span::test_data()}), }] } @@ -135,3 +139,15 @@ fn update( ) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_examples() { + use crate::test_examples; + + test_examples(Update {}) + } +}