mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 22:57:59 +02:00
Use fixed column name for query xml
output (#16461)
This is a breaking change. Refs https://discord.com/channels/601130461678272522/615329862395101194/1406413134402551869 Depends on (and includes) nushell/nushell#16459, which should be merged first Partially addresses nushell/nushell#15717 ## Motivation and technical details Variable column names are hard to work with. What's worse, currently Nushell truncates the column name, making it even more unpredictable. The only reliable way to deal with that is to rename the results column like this: ```nushell .... | query xml "//item/title|//title" | rename string_value | ... ``` so why would we make our users jump through these hoops? To provide some parallels, `query db` does not add the query into its output, and neither does `get`. The choice of the column name comes from XPath spec: https://www.w3.org/TR/xpath-10/#dt-string-value, adapted to Nushell conventions ## Release notes summary - What our users need to know `query xml` outputs the results (for nodeset results) in a column with a fixed name. Before: ```nushell open -r tests/fixtures/formats/jt.xml | query xml '//channel/title|//item/title' # => ╭───┬───────────────────────────────────────────╮ # => │ # │ //channel/title|/... │ # => ├───┼───────────────────────────────────────────┤ # => │ 0 │ JT │ # => │ 1 │ Creating crossplatform Rust terminal apps │ # => ╰───┴───────────────────────────────────────────╯ ``` Now: ```nushell open -r tests/fixtures/formats/jt.xml | query xml '//channel/title|//item/title' # => ╭───┬───────────────────────────────────────────╮ # => │ # │ string_value │ # => ├───┼───────────────────────────────────────────┤ # => │ 0 │ JT │ # => │ 1 │ Creating crossplatform Rust terminal apps │ # => ╰───┴───────────────────────────────────────────╯ ``` ## Tasks after submitting <!-- Remove any tasks which aren't relevant for your PR, or add your own --> - [ ] Update the [documentation](https://github.com/nushell/nushell.github.io)
This commit is contained in:
@@ -89,19 +89,10 @@ pub fn execute_xpath_query(
|
||||
Ok(sxd_xpath::Value::Number(n)) => Ok(Value::float(n, call.head)),
|
||||
Ok(sxd_xpath::Value::String(s)) => Ok(Value::string(s, call.head)),
|
||||
Ok(sxd_xpath::Value::Nodeset(ns)) => {
|
||||
// Some xpath statements can be long, so let's truncate it with ellipsis
|
||||
let mut key = query_string.clone();
|
||||
if query_string.len() >= 20 {
|
||||
key.truncate(17);
|
||||
key += "...";
|
||||
} else {
|
||||
key = query_string.to_string();
|
||||
};
|
||||
|
||||
let mut records: Vec<Value> = vec![];
|
||||
for n in ns.document_order() {
|
||||
records.push(Value::record(
|
||||
record! {key.clone() => Value::string(n.string_value(), call.head)},
|
||||
record! {"string_value" => Value::string(n.string_value(), call.head)},
|
||||
call.head,
|
||||
));
|
||||
}
|
||||
@@ -205,7 +196,7 @@ mod tests {
|
||||
|
||||
let expected = Value::list(
|
||||
vec![Value::test_record(record! {
|
||||
"//qp:b/text()" => Value::string("yay", Span::test_data()),
|
||||
"string_value" => Value::string("yay", Span::test_data()),
|
||||
})],
|
||||
Span::test_data(),
|
||||
);
|
||||
@@ -293,7 +284,7 @@ mod tests {
|
||||
|
||||
let expected = Value::list(
|
||||
vec![Value::test_record(record! {
|
||||
"/elt" => Value::string("hello", Span::test_data()),
|
||||
"string_value" => Value::string("hello", Span::test_data()),
|
||||
})],
|
||||
Span::test_data(),
|
||||
);
|
||||
|
Reference in New Issue
Block a user