allow Range to expand to array-like when converting to json (#8047)

# Description
Fixes #8002, which expands ranges `1..3` to expand to array-like when
saving and converting to json. Now,

```
> 1..3 | save foo.json
# foo.json
[
    1,
    2,
    3
]


> 1..3 | to json
[
    1,
    2,
    3
]
```
# User-Facing Changes

_(List of all changes that impact the user experience here. This helps
us keep track of breaking changes.)_

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- [X] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [X] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- [X] `cargo test --workspace` to check that all tests pass

# 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.
This commit is contained in:
David Matos
2023-02-24 22:31:33 +01:00
committed by GitHub
parent 253b223e65
commit 42f0b55de0
8 changed files with 94 additions and 0 deletions

View File

@ -48,6 +48,8 @@ impl Command for ToJson {
let use_tabs = call.has_flag("tabs");
let span = call.head;
// allow ranges to expand and turn into array
let input = input.try_expand_range()?;
let value = input.into_value(span);
let json_value = value_to_json_value(&value)?;

View File

@ -34,6 +34,7 @@ impl Command for ToNuon {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let input = input.try_expand_range()?;
Ok(Value::String {
val: to_nuon(call, input)?,
span: call.head,

View File

@ -39,6 +39,7 @@ impl Command for ToText {
} else {
"\n"
};
let input = input.try_expand_range()?;
if let PipelineData::ListStream(stream, _) = input {
Ok(PipelineData::ExternalStream {

View File

@ -64,6 +64,7 @@ impl Command for ToXml {
let head = call.head;
let config = engine_state.get_config();
let pretty: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "pretty")?;
let input = input.try_expand_range()?;
to_xml(input, head, pretty, config)
}
}

View File

@ -38,6 +38,7 @@ impl Command for ToYaml {
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let input = input.try_expand_range()?;
to_yaml(input, head)
}
}