mirror of
https://github.com/nushell/nushell
synced 2025-01-14 22:24:54 +00:00
8127b5dd24
# Description This PR adds the `merge deep` command. This allows you to merge nested records and tables/lists within records together, instead of overwriting them. The code for `merge` was reworked to support more general merging of values, so `merge` and `merge deep` use the same underlying code. `merge deep` mostly works like `merge`, except it recurses into inner records which exist in both the input and argument rather than just overwriting. For lists and by extension tables, `merge deep` has a couple different strategies for merging inner lists, which can be selected with the `--strategy` flag. These are: - `table`: Merges tables element-wise, similarly to the merge command. Non-table lists are not merged. - `overwrite`: Lists and tables are overwritten with their corresponding value from the argument, similarly to scalars. - `append`: Lists and tables in the input are appended with the corresponding list from the argument. - `prepend`: Lists and tables in the input are prepended with the corresponding list from the argument. This can also be used with the new config changes to write a monolithic record of _only_ the config values you want to change: ```nushell # in config file: const overrides = { history: { file_format: "sqlite", isolation: true } } # use append strategy for lists, e.g., menus keybindings $env.config = $env.config | merge deep --strategy=append $overrides # later, in REPL: $env.config.history # => ╭───────────────┬────────╮ # => │ max_size │ 100000 │ # => │ sync_on_enter │ true │ # => │ file_format │ sqlite │ # => │ isolation │ true │ # => ╰───────────────┴────────╯ ``` <details> <summary>Performance details</summary> For those interested, there was less than one standard deviation of difference in startup time when setting each config item individually versus using <code>merge deep</code>, so you can use <code>merge deep</code> in your config at no measurable performance cost. Here's my results: My normal config (in 0.101 style, with each `$env.config.[...]` value updated individually) ```nushell bench --pretty { ./nu -l -c '' } # => 45ms 976µs 983ns +/- 455µs 955ns ``` Equivalent config with a single `overrides` record and `merge deep -s append`: ```nushell bench --pretty { ./nu -l -c '' } # => 45ms 587µs 428ns +/- 702µs 944ns ``` </details> Huge thanks to @Bahex for designing the strategies API and helping finish up this PR while I was sick ❤️ Related: #12148 # User-Facing Changes Adds the `merge deep` command to recursively merge records. For example: ```nushell {a: {foo: 123 bar: "overwrite me"}, b: [1, 2, 3]} | merge deep {a: {bar: 456, baz: 789}, b: [4, 5, 6]} # => ╭───┬───────────────╮ # => │ │ ╭─────┬─────╮ │ # => │ a │ │ foo │ 123 │ │ # => │ │ │ bar │ 456 │ │ # => │ │ │ baz │ 789 │ │ # => │ │ ╰─────┴─────╯ │ # => │ │ ╭───┬───╮ │ # => │ b │ │ 0 │ 4 │ │ # => │ │ │ 1 │ 5 │ │ # => │ │ │ 2 │ 6 │ │ # => │ │ ╰───┴───╯ │ # => ╰───┴───────────────╯ ``` `merge deep` also has different strategies for merging inner lists and tables. For example, you can use the `append` strategy to _merge_ the inner `b` list instead of overwriting it. ```nushell {a: {foo: 123 bar: "overwrite me"}, b: [1, 2, 3]} | merge deep --strategy=append {a: {bar: 456, baz: 789}, b: [4, 5, 6]} # => ╭───┬───────────────╮ # => │ │ ╭─────┬─────╮ │ # => │ a │ │ foo │ 123 │ │ # => │ │ │ bar │ 456 │ │ # => │ │ │ baz │ 789 │ │ # => │ │ ╰─────┴─────╯ │ # => │ │ ╭───┬───╮ │ # => │ b │ │ 0 │ 1 │ │ # => │ │ │ 1 │ 2 │ │ # => │ │ │ 2 │ 3 │ │ # => │ │ │ 3 │ 4 │ │ # => │ │ │ 4 │ 5 │ │ # => │ │ │ 5 │ 6 │ │ # => │ │ ╰───┴───╯ │ # => ╰───┴───────────────╯ ``` **Note to release notes writers**: Please credit @Bahex for this PR as well 😄 # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> Added tests for deep merge - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> N/A --------- Co-authored-by: Bahex <bahey1999@gmail.com>
144 lines
3 KiB
Rust
144 lines
3 KiB
Rust
use nu_test_support::nu;
|
|
|
|
#[test]
|
|
fn table_strategy_table() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep {} | to nuon",
|
|
"{inner: [{a: 1}, {b: 2}]}",
|
|
"{inner: [{c: 3}]}"
|
|
)
|
|
.out,
|
|
"{inner: [{a: 1, c: 3}, {b: 2}]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn table_strategy_list() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep {} | to nuon",
|
|
"{a: [1, 2, 3]}",
|
|
"{a: [4, 5, 6]}"
|
|
)
|
|
.out,
|
|
"{a: [4, 5, 6]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn overwrite_strategy_table() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep --strategy=overwrite {} | to nuon",
|
|
"{inner: [{a: 1}, {b: 2}]}",
|
|
"{inner: [[c]; [3]]}"
|
|
)
|
|
.out,
|
|
"{inner: [[c]; [3]]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn overwrite_strategy_list() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep --strategy=overwrite {} | to nuon",
|
|
"{a: [1, 2, 3]}",
|
|
"{a: [4, 5, 6]}"
|
|
)
|
|
.out,
|
|
"{a: [4, 5, 6]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn append_strategy_table() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep --strategy=append {} | to nuon",
|
|
"{inner: [{a: 1}, {b: 2}]}",
|
|
"{inner: [{c: 3}]}"
|
|
)
|
|
.out,
|
|
"{inner: [{a: 1}, {b: 2}, {c: 3}]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn append_strategy_list() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep --strategy=append {} | to nuon",
|
|
"{inner: [1, 2, 3]}",
|
|
"{inner: [4, 5, 6]}"
|
|
)
|
|
.out,
|
|
"{inner: [1, 2, 3, 4, 5, 6]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn prepend_strategy_table() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep --strategy=prepend {} | to nuon",
|
|
"{inner: [{a: 1}, {b: 2}]}",
|
|
"{inner: [{c: 3}]}"
|
|
)
|
|
.out,
|
|
"{inner: [{c: 3}, {a: 1}, {b: 2}]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn prepend_strategy_list() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep --strategy=prepend {} | to nuon",
|
|
"{inner: [1, 2, 3]}",
|
|
"{inner: [4, 5, 6]}"
|
|
)
|
|
.out,
|
|
"{inner: [4, 5, 6, 1, 2, 3]}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn record_nested_with_overwrite() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep {} | to nuon",
|
|
"{a: {b: {c: {d: 123, e: 456}}}}",
|
|
"{a: {b: {c: {e: 654, f: 789}}}}"
|
|
)
|
|
.out,
|
|
"{a: {b: {c: {d: 123, e: 654, f: 789}}}}"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn single_row_table() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep {} | to nuon",
|
|
"[[a]; [{foo: [1, 2, 3]}]]",
|
|
"[[a]; [{bar: [4, 5, 6]}]]"
|
|
)
|
|
.out,
|
|
"[[a]; [{foo: [1, 2, 3], bar: [4, 5, 6]}]]"
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn multi_row_table() {
|
|
assert_eq!(
|
|
nu!(
|
|
"{} | merge deep {} | to nuon ",
|
|
"[[a b]; [{inner: {foo: abc}} {inner: {baz: ghi}}]]",
|
|
"[[a b]; [{inner: {bar: def}} {inner: {qux: jkl}}]]"
|
|
)
|
|
.out,
|
|
"[[a, b]; [{inner: {foo: abc, bar: def}}, {inner: {baz: ghi, qux: jkl}}]]"
|
|
)
|
|
}
|