nushell/crates/nu-command/src
Yash Thakur 0303d709e6
Spread operator in record literals (#11144)
Goes towards implementing #10598, which asks for a spread operator in
lists, in records, and when calling commands (continuation of #11006,
which only implements it in lists)

# Description
This PR is for adding a spread operator that can be used when building
records. Additional functionality can be added later.

Changes:

- Previously, the `Expr::Record` variant held `(Expression, Expression)`
pairs. It now holds instances of an enum `RecordItem` (the name isn't
amazing) that allows either a key-value mapping or a spread operator.
- `...` will be treated as the spread operator when it appears before
`$`, `{`, or `(` inside records (no whitespace allowed in between) (not
implemented yet)
- The error message for duplicate columns now includes the column name
itself, because if two spread records are involved in such an error, you
can't tell which field was duplicated from the spans alone

`...` will still be treated as a normal string outside records, and even
in records, it is not treated as a spread operator when not followed
immediately by a `$`, `{`, or `(`.

# User-Facing Changes
Users will be able to use `...` when building records.

```
> let rec = { x: 1, ...{ a: 2 } }
> $rec
╭───┬───╮
│ x │ 1 │
│ a │ 2 │
╰───┴───╯
> { foo: bar, ...$rec, baz: blah }
╭─────┬──────╮
│ foo │ bar  │
│ x   │ 1    │
│ a   │ 2    │
│ baz │ blah │
╰─────┴──────╯
```
If you want to update a field of a record, you'll have to use `merge`
instead:
```
> { ...$rec, x: 5 }
Error: nu:🐚:column_defined_twice

  × Record field or table column used twice: x
   ╭─[entry #2:1:1]
 1 │  { ...$rec, x: 5 }
   ·       ──┬─  ┬
   ·         │   ╰── field redefined here
   ·         ╰── field first defined here
   ╰────
> $rec | merge { x: 5 }
╭───┬───╮
│ x │ 5 │
│ a │ 2 │
╰───┴───╯
```

# Tests + Formatting

# After Submitting
2023-11-29 18:31:31 +01:00
..
bytes Convert ShellError::UnsupportedInput to named fields (#10971) 2023-11-07 23:25:32 +01:00
charting Use Vec for Closure captures (#10940) 2023-11-08 00:43:28 +01:00
conversions into binary -c: return 0 as single byte (#11068) 2023-11-16 04:09:31 -06:00
database Add stor family of commands (#11170) 2023-11-29 08:02:46 -08:00
date Improve case insensitivity consistency (#10884) 2023-11-08 23:58:54 +01:00
debug Move more commands to opaque Record type (#11122) 2023-11-22 23:48:48 +01:00
env Convert FileNotFoundCustom to named fields (#11123) 2023-11-21 17:30:21 -06:00
experimental Add functions for each Value case (#9736) 2023-07-21 08:20:33 -05:00
filesystem Convert more ShellError variants to named fields (#11173) 2023-11-28 06:43:51 -06:00
filters Add metadata to some filters (#11160) 2023-11-27 12:16:34 -06:00
formats Spread operator in record literals (#11144) 2023-11-29 18:31:31 +01:00
generators remove the unfold command (#10773) 2023-11-17 06:50:20 +08:00
hash Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
help Convert ShellError::AliasNotFound to named fields (#11118) 2023-11-21 16:24:08 +08:00
math Move more commands to opaque Record type (#11122) 2023-11-22 23:48:48 +01:00
misc Add long options for misc and network (#10753) 2023-10-19 18:16:44 +02:00
network Convert more ShellError variants to named fields (#11173) 2023-11-28 06:43:51 -06:00
path Convert more ShellError variants to named fields (#11173) 2023-11-28 06:43:51 -06:00
platform Convert more ShellError variants to named fields (#11173) 2023-11-28 06:43:51 -06:00
random Add long options for platform and random (#10776) 2023-10-19 22:04:33 +02:00
removed Rename misused "deprecation" to removal (#10000) 2023-08-15 07:17:31 +12:00
shells Fix usage for the exit command. (#9450) 2023-06-16 10:09:02 +02:00
stor Add stor family of commands (#11170) 2023-11-29 08:02:46 -08:00
strings remove size command in favor of str stats (#10784) 2023-11-17 06:49:19 +08:00
system Convert more ShellError variants to named fields (#11173) 2023-11-28 06:43:51 -06:00
viewers optimize/clean up a few of the table changes (#11076) 2023-11-16 11:37:46 -06:00
default_context.rs Add stor family of commands (#11170) 2023-11-29 08:02:46 -08:00
example_test.rs feat: Add unfold command (#10489) 2023-09-30 09:08:06 -05:00
lib.rs Add stor family of commands (#11170) 2023-11-29 08:02:46 -08:00
progress_bar.rs cp progress bar implementation (#8012) 2023-02-22 11:57:38 -08:00
sort_utils.rs Improve case insensitivity consistency (#10884) 2023-11-08 23:58:54 +01:00