diff --git a/crates/nu-command/src/network/url/build_query.rs b/crates/nu-command/src/network/url/build_query.rs index 1daabb507f..8b563a2bbd 100644 --- a/crates/nu-command/src/network/url/build_query.rs +++ b/crates/nu-command/src/network/url/build_query.rs @@ -42,6 +42,11 @@ impl Command for SubCommand { example: r#"{a:"AT&T", b: "AT T"} | url build-query"#, result: Some(Value::test_string("a=AT%26T&b=AT+T")), }, + Example { + description: "Outputs a query string representing the contents of this record", + example: r#"{a: ["one", "two"], b: "three"} | url build-query"#, + result: Some(Value::test_string("a=one&a=two&b=three")), + }, ] } @@ -66,30 +71,44 @@ fn to_url(input: PipelineData, head: Span) -> Result { Value::Record { ref val, .. } => { let mut row_vec = vec![]; for (k, v) in &**val { - match v.coerce_string() { - Ok(s) => { - row_vec.push((k.clone(), s)); - } - _ => { - return Err(ShellError::UnsupportedInput { - msg: "Expected a record with string values".to_string(), - input: "value originates from here".into(), - msg_span: head, - input_span: span, - }); + match v { + Value::List { ref vals, .. } => { + for v_item in vals { + row_vec.push(( + k.clone(), + v_item.coerce_string().map_err(|_| { + ShellError::UnsupportedInput { + msg: "Expected a record with list of string values" + .to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + } + })?, + )); + } } + _ => row_vec.push(( + k.clone(), + v.coerce_string() + .map_err(|_| ShellError::UnsupportedInput { + msg: + "Expected a record with string or list of string values" + .to_string(), + input: "value originates from here".into(), + msg_span: head, + input_span: span, + })?, + )), } } - match serde_urlencoded::to_string(row_vec) { - Ok(s) => Ok(s), - _ => Err(ShellError::CantConvert { - to_type: "URL".into(), - from_type: value.get_type().to_string(), - span: head, - help: None, - }), - } + serde_urlencoded::to_string(row_vec).map_err(|_| ShellError::CantConvert { + to_type: "URL".into(), + from_type: value.get_type().to_string(), + span: head, + help: None, + }) } // Propagate existing errors Value::Error { error, .. } => Err(*error),