From 92c4097f8d4e86f7b2ecd088d7b00449c5390bd7 Mon Sep 17 00:00:00 2001 From: Vincenzo Carlino Date: Sun, 15 Jan 2023 19:16:29 +0100 Subject: [PATCH] Rename `to url` command to `url build-query` (#7702) # Description Refactor command: "to url" in: "to url query". Changed usage sentence. Closes: #7495 # User-Facing Changes Now we get a query string from a record or table by using command: "to url query". ``` > help to url query Convert record or table into query string applying percent-encoding. Usage: > to url query Flags: -h, --help - Display the help message for this command Signatures: | to url query -> | to url query -> Examples: Outputs a query string representing the contents of this record > { mode:normal userid:31415 } | to url query Outputs a query string representing the contents of this 1-row table > [[foo bar]; ["1" "2"]] | to url query Outputs a query string representing the contents of this record > {a:"AT&T", b: "AT T"} | to url query ``` # Tests + Formatting Added this test: ``` Example { description: "Outputs a query string representing the contents of this record", example: r#"{a:"AT&T", b: "AT T"} | to url query"#, result: Some(Value::test_string("a=AT%26T&b=AT+T")), }, ``` to ensure percent-encoding. # After Submitting If PR is accepted I'll open another PR on documentation to notify changes on [this.](https://github.com/nushell/nushell.github.io/blob/main/book/commands/to_url.md) --- crates/nu-command/src/default_context.rs | 2 +- crates/nu-command/src/formats/to/mod.rs | 2 -- .../to/url.rs => network/url/build_query.rs} | 31 ++++++++++++------- crates/nu-command/src/network/url/mod.rs | 2 ++ .../tests/format_conversions/url.rs | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) rename crates/nu-command/src/{formats/to/url.rs => network/url/build_query.rs} (75%) diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index f8e978a099..553bd20a68 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -355,7 +355,6 @@ pub fn create_default_context() -> EngineState { Use, Upsert, Where, - ToUrl, ToXml, ToYaml, }; @@ -437,6 +436,7 @@ pub fn create_default_context() -> EngineState { Fetch, Post, Url, + UrlBuildQuery, UrlEncode, UrlParse, Port, diff --git a/crates/nu-command/src/formats/to/mod.rs b/crates/nu-command/src/formats/to/mod.rs index 14bbf91dff..4a08574a2f 100644 --- a/crates/nu-command/src/formats/to/mod.rs +++ b/crates/nu-command/src/formats/to/mod.rs @@ -8,13 +8,11 @@ mod nuon; mod text; mod toml; mod tsv; -mod url; mod xml; mod yaml; pub use self::csv::ToCsv; pub use self::toml::ToToml; -pub use self::url::ToUrl; pub use command::To; pub use html::ToHtml; pub use json::ToJson; diff --git a/crates/nu-command/src/formats/to/url.rs b/crates/nu-command/src/network/url/build_query.rs similarity index 75% rename from crates/nu-command/src/formats/to/url.rs rename to crates/nu-command/src/network/url/build_query.rs index f8213876e4..238f306994 100644 --- a/crates/nu-command/src/formats/to/url.rs +++ b/crates/nu-command/src/network/url/build_query.rs @@ -5,38 +5,47 @@ use nu_protocol::{ }; #[derive(Clone)] -pub struct ToUrl; +pub struct SubCommand; -impl Command for ToUrl { +impl Command for SubCommand { fn name(&self) -> &str { - "to url" + "url build-query" } fn signature(&self) -> Signature { - Signature::build("to url") + Signature::build("url build-query") .input_output_types(vec![ (Type::Record(vec![]), Type::String), (Type::Table(vec![]), Type::String), ]) - .category(Category::Formats) + .category(Category::Network) } fn usage(&self) -> &str { - "Convert record or table into URL-encoded text" + "Converts record or table into query string applying percent-encoding." + } + + fn search_terms(&self) -> Vec<&str> { + vec!["convert", "record", "table"] } fn examples(&self) -> Vec { vec![ Example { - description: "Outputs a URL string representing the contents of this record", - example: r#"{ mode:normal userid:31415 } | to url"#, + description: "Outputs a query string representing the contents of this record", + example: r#"{ mode:normal userid:31415 } | url build-query"#, result: Some(Value::test_string("mode=normal&userid=31415")), }, Example { - description: "Outputs a URL string representing the contents of this 1-row table", - example: r#"[[foo bar]; ["1" "2"]] | to url"#, + description: "Outputs a query string representing the contents of this 1-row table", + example: r#"[[foo bar]; ["1" "2"]] | url build-query"#, result: Some(Value::test_string("foo=1&bar=2")), }, + Example { + description: "Outputs a query string representing the contents of this record", + example: r#"{a:"AT&T", b: "AT T"} | url build-query"#, + result: Some(Value::test_string("a=AT%26T&b=AT+T")), + }, ] } @@ -110,6 +119,6 @@ mod test { fn test_examples() { use crate::test_examples; - test_examples(ToUrl {}) + test_examples(SubCommand {}) } } diff --git a/crates/nu-command/src/network/url/mod.rs b/crates/nu-command/src/network/url/mod.rs index d2845bc7e1..4602a7ffc4 100644 --- a/crates/nu-command/src/network/url/mod.rs +++ b/crates/nu-command/src/network/url/mod.rs @@ -1,3 +1,4 @@ +mod build_query; mod encode; mod parse; mod url_; @@ -5,5 +6,6 @@ mod url_; use url::{self}; pub use self::parse::SubCommand as UrlParse; +pub use build_query::SubCommand as UrlBuildQuery; pub use encode::SubCommand as UrlEncode; pub use url_::Url; diff --git a/crates/nu-command/tests/format_conversions/url.rs b/crates/nu-command/tests/format_conversions/url.rs index 2187fa3822..366065395b 100644 --- a/crates/nu-command/tests/format_conversions/url.rs +++ b/crates/nu-command/tests/format_conversions/url.rs @@ -6,7 +6,7 @@ fn can_encode_and_decode_urlencoding() { cwd: "tests/fixtures/formats", pipeline( r#" open sample.url - | to url + | url build-query | from url | get cheese "#