nushell/crates/nu-command/src
Hudson Clark cb754befe9
fix: Ensure consistent vals and cols when parsing with --flexible (#10814)
# Description
`from tsv` and `from csv` both support a `--flexible` flag. This flag
can be used to "allow the number of fields in records to be variable".

Previously, a record's invariant that `rec.cols.len() == rec.vals.len()`
could be broken during parsing. This can cause runtime errors as in
#10693. Other commands, like `select` were also affected.

The inconsistencies are somewhat hard to see, as most nushell code
assumes an equal number of columns and values.

# Before

### Fewer values than columns
```nushell
> let record = (echo "one,two\n1" | from csv --flexible | first)
# There are two columns
> $record | columns | to nuon
[one, two]
# But only one value
> $record | values | to nuon
[1]
# And printing the record doesn't show the second column!
> $record | to nuon
{one: 1}
```

### More values than columns
```nushell
> let record = (echo "one,two\n1,2,3" | from csv --flexible | first)
# There are two columns
> $record | columns | to nuon
[one, two]
# But three values
> $record | values | to nuon
[1, 2, 3]
# And printing the record doesn't show the third value!
> $record | to nuon
{one: 1, two: 2}
```
# After

### Fewer values than columns
```nushell
> let record = (echo "one,two\n1" | from csv --flexible | first)
# There are two columns
> $record | columns | to nuon
[one, two]
# And a matching number of values
> $record | values | to nuon
[1, null]
# And printing the record works as expected
> $record | to nuon
{one: 1, two: null}
```

### More values than columns
```nushell
> let record = (echo "one,two\n1,2,3" | from csv --flexible | first)
# There are two columns
> $record | columns | to nuon
[one, two]
# And a matching number of values
> $record | values | to nuon
[1, 2]
# And printing the record works as expected
> $record | to nuon
{one: 1, two: 2}
```

# User-Facing Changes
Using the `--flexible` flag with `from csv` and `from tsv` will not
result in corrupted record state.

# 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 std testing; testing run-tests --path
crates/nu-std"` 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
> ```
-->

# 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.
-->
2023-10-24 15:54:26 -05:00
..
bytes Add long options for bits and bytes (#10601) 2023-10-05 18:45:28 +02:00
charting Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
conversions remove into decimal (#10341) 2023-10-10 20:05:44 +02:00
database Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
date fix clippy (#10659) 2023-10-10 03:31:15 +13:00
debug Make debug info lazy (#10728) 2023-10-24 12:48:05 -05:00
env Fix editor config for reedline and config nu/env (#10535) 2023-09-29 16:36:03 +02:00
experimental Add functions for each Value case (#9736) 2023-07-21 08:20:33 -05:00
filesystem Finish removing profile command and related data (#10807) 2023-10-22 14:06:53 +03:00
filters Finish removing profile command and related data (#10807) 2023-10-22 14:06:53 +03:00
formats fix: Ensure consistent vals and cols when parsing with --flexible (#10814) 2023-10-24 15:54:26 -05:00
generators add unfold back with a deprecation warning (#10771) 2023-10-19 19:23:06 +02:00
hash Move Value to helpers, separate span call (#10121) 2023-09-03 07:27:29 -07:00
help Add 'help escapes' command for quick reference of nushell string escapes (#10522) 2023-09-30 09:04:27 -05:00
math Add long options for generators and math (#10752) 2023-10-19 18:17:42 +02:00
misc Add long options for misc and network (#10753) 2023-10-19 18:16:44 +02:00
network Add long options for misc and network (#10753) 2023-10-19 18:16:44 +02:00
path Add long options for path (#10775) 2023-10-19 22:07:01 +02:00
platform Move ansi link from extra to default feature, close #10792 (#10801) 2023-10-21 11:04:37 -05: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
strings Deprecate size to str stats (#10798) 2023-10-21 11:21:34 -05:00
system Improve registry value return types (#10806) 2023-10-23 07:21:27 -05:00
viewers Finish removing profile command and related data (#10807) 2023-10-22 14:06:53 +03:00
default_context.rs Finish removing profile command and related data (#10807) 2023-10-22 14:06:53 +03:00
example_test.rs feat: Add unfold command (#10489) 2023-09-30 09:08:06 -05:00
lib.rs Move eval_hook to nu-cmd-base (#10146) 2023-08-29 23:46:50 +02:00
progress_bar.rs cp progress bar implementation (#8012) 2023-02-22 11:57:38 -08:00
sort_utils.rs Use slices directly instead of &Vec (#10328) 2023-09-12 11:38:20 +08:00