Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect: don't require a closure #12788

Merged
merged 7 commits into from
May 17, 2024

Conversation

devyn
Copy link
Contributor

@devyn devyn commented May 7, 2024

Description

This changes the collect command so that it doesn't require a closure. Still allowed, optionally.

Before:

open foo.json | insert foo bar | collect { save -f foo.json }

After:

open foo.json | insert foo bar | collect | save -f foo.json

The closure argument isn't really necessary, as collect values are also supported as PipelineData.

User-Facing Changes

  • collect command changed

Tests + Formatting

Example changed to reflect.

After Submitting

  • release notes
  • we may want to deprecate the closure arg?

@IanManske
Copy link
Member

IanManske commented May 7, 2024

we may want to deprecate the closure arg?

I would be in favor of deprecating the closure arg. We would have to figure out another way to implement the $in variable before that. Although, we might be able to just hide/remove the closure arg in the signature, but still have the parser pass the closure arg to collect.

@fdncred fdncred added pr:breaking-change This PR implies a change affecting users and has to be noted in the release notes pr:commands This PR changes our commands in some way labels May 7, 2024
@WindSoilder
Copy link
Collaborator

WindSoilder commented May 8, 2024

I don't think it's a breaking change because it converts from a required parameter to optional parameter, so I will remove the label

@WindSoilder WindSoilder removed the pr:breaking-change This PR implies a change affecting users and has to be noted in the release notes label May 8, 2024
@WindSoilder
Copy link
Collaborator

WindSoilder commented May 9, 2024

I think we also need to change parser, it uses collect command to build $in variable (at line 6249).

fn wrap_expr_with_collect(working_set: &mut StateWorkingSet, expr: &Expression) -> Expression {
let span = expr.span;
if let Some(decl_id) = working_set.find_decl(b"collect") {
let mut output = vec![];
let var_id = IN_VARIABLE_ID;
let mut signature = Signature::new("");
signature.required_positional.push(PositionalArg {
var_id: Some(var_id),
name: "$in".into(),
desc: String::new(),
shape: SyntaxShape::Any,
default_value: None,
});

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To deliver on the promise for the open ... | collect | save ... round trip we should strip the metadata if it matches DataSource::FilePath. The others should probably remain if we don't want to commit to this side effect but I would say that DataSource::FilePath is currently an implementation detail for the particular challenge of inplace modifying a file.

@devyn
Copy link
Contributor Author

devyn commented May 11, 2024

Does this make sense? I changed the save command to not check the metadata if it's a Value, because there shouldn't be an issue with reading streams in that case

@sholderbach
Copy link
Member

I changed the save command to not check the metadata if it's a Value, because there shouldn't be an issue with reading streams in that case

In my view @IanManske's suggestion is an orthogonal improvement.

I think for this PR on collect we can make stronger guarantees for collect if you have following lazy operations:

open cycle.txt | collect | filter { true } | save cycle.txt

@devyn
Copy link
Contributor Author

devyn commented May 17, 2024

Okay, I think I've fixed it to be correct. File path metadata is removed on collect, and it's also set on the input of the closure rather than the output

Added a couple tests to save that use collect to ensure this works the right way

Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Good additional tests

@sholderbach sholderbach merged commit c10aa2c into nushell:main May 17, 2024
15 checks passed
@devyn devyn deleted the collect-without-closure branch May 17, 2024 16:46
FilipAndersson245 pushed a commit to FilipAndersson245/nushell that referenced this pull request May 18, 2024
# Description

This changes the `collect` command so that it doesn't require a closure.
Still allowed, optionally.

Before:

```nushell
open foo.json | insert foo bar | collect { save -f foo.json }
```

After:

```nushell
open foo.json | insert foo bar | collect | save -f foo.json
```

The closure argument isn't really necessary, as collect values are also
supported as `PipelineData`.

# User-Facing Changes
- `collect` command changed

# Tests + Formatting
Example changed to reflect.

# After Submitting
- [ ] release notes
- [ ] we may want to deprecate the closure arg?
@hustcer hustcer added this to the v0.94.0 milestone May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:commands This PR changes our commands in some way
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants