Skip to content

Show ? for optional entries when displaying CellPaths#14042

Merged
fdncred merged 2 commits intonushell:mainfrom
aionescu:cellpath-display-optional
Oct 29, 2024
Merged

Show ? for optional entries when displaying CellPaths#14042
fdncred merged 2 commits intonushell:mainfrom
aionescu:cellpath-display-optional

Conversation

@aionescu
Copy link
Copy Markdown
Contributor

@aionescu aionescu commented Oct 9, 2024

Description

This PR makes the Display implementation for CellPath show a ? suffix on every optional entry, which makes the output consistent with the language syntax.

Before this PR, the printing of cell paths was confusing, e.g. $.x and $.x? were both printed as x. Now, the second one is printed as x?.

User-Facing Changes

The formatting of cell paths now matches the syntax used to create them, reducing confusion.

Tests + Formatting

All tests pass, including stdlib tests.

After Submitting

@aionescu
Copy link
Copy Markdown
Contributor Author

aionescu commented Oct 9, 2024

Implementation note:

I'm pretty annoyed by the duplicated if check. I wanted to use an or-pattern (PathMember::Int { val, optional, .. } | PathMember::String { val, optional, .. } =>) but that doesn't work because val has different types in each branch.

Extracting the check into a function or macro also seems overkill for just 2 repetitions.

If anyone has better suggestions, please let me know.

@aionescu
Copy link
Copy Markdown
Contributor Author

aionescu commented Oct 9, 2024

Ok, I forgot to pass --workspace to cargo test. That revealed some failing select tests. I updated them to expect the question marks in the output's column names.

While this looks pretty weird, select can already produce weird column names, e.g. { x: { y: 2 } } | select $.x.y produces { "x.y": 2 }. Now { x: { y: 2 } } | select $.x?.y produces { "x?.y": 2 } instead of { "x.y": 2 }.

However, in (what I assume is) the very common case of selecting a single optional column (select x?), the output column is now named x?. This could be a pretty annoying breaking change. A possible workaround is to use get -i x | wrap x instead.

@sholderbach
Copy link
Copy Markdown
Member

While this looks pretty weird, select can already produce weird column names, e.g. { x: { y: 2 } } | select $.x.y produces { "x.y": 2 }. Now { x: { y: 2 } } | select $.x?.y produces { "x?.y": 2 } instead of { "x.y": 2 }.

Yeah, discovered this behavior of select generating new column names from the cell-path's when fixing a bug as well

Apart from that whole thing (mis?)using the Display implementation there is another problem in the representation

Thanks for fixing the optional cell-path part!

@sholderbach sholderbach added the A:cell-path-semantics All around the cell path type and its semantics for access label Oct 9, 2024
@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Oct 10, 2024

@sholderbach does that mean we're ready to land this?

@sholderbach
Copy link
Copy Markdown
Member

I kind of think we should decouple select's behavior from the changes we plan to make to get a better cell path output.

@aionescu do you want to implement the rest of the improvements to the cell path display output as @IanManske and I discussed in #13362 and #14090 ?

@aionescu
Copy link
Copy Markdown
Contributor Author

@sholderbach Yeah, they look easy enough (famous last words?). Should I add them all to this PR, or make a separate one?

@sholderbach
Copy link
Copy Markdown
Member

Should I add them all to this PR, or make a separate one?

Whatever is practical to you, if you want to update the select tests a few times or once. But if it is quicker to get the pieces done as individual PR its fine as well. We only don't want to drag too many breaking changes over multiple releases, but that should be doable.

@fdncred fdncred merged commit 03015ed into nushell:main Oct 29, 2024
@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Oct 29, 2024

Thanks

@github-actions github-actions bot added this to the v0.100.0 milestone Oct 29, 2024
@hustcer
Copy link
Copy Markdown
Contributor

hustcer commented Nov 2, 2024

I think it's actually a breaking change:

# Before this PR the following line works:
http get https://api.github.com/repos/nushell/nushell/releases/latest | select -i tag_name assets?.id? | flatten | rename -c {'assets.id': id}
# After this PR the above line will broken and should be changed to:
http get https://api.github.com/repos/nushell/nushell/releases/latest | select -i tag_name assets?.id? | flatten | rename -c {'assets?.id?': id}

It's weird that after this change the column names will have a ? suffix if select with -i, it may be unacceptable?:

[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | select -i version name | columns
 0   version? 
 1   name? 

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Nov 2, 2024

Good catch. The column names should be required to have ? for renames and selects even if they're optional. Let's revert.

fdncred added a commit that referenced this pull request Nov 2, 2024
@aionescu aionescu deleted the cellpath-display-optional branch November 2, 2024 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:cell-path-semantics All around the cell path type and its semantics for access

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants