PowerShell / PowerShell/PowerShell

Add operator or attribute to disable auto unrolling of pipe

Open
#20,848 32 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue-Enhancement Needs-Triage
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

Summary of the new feature / enhancement

As already discussed in other tickets the behavior of the pipe automatically unwrapping arrays of one is "by design" and not a bug, I'd like to request the addition of a new operator or attribute that changes this behavior in a non-breaking way instead of changing it for the pipe itself globally. Also it is very unintuitive that using the grouping operator has the opposite effect, instead of grouping everything together and signalling that it should be treated as a single input it'll cause the pipe to unroll the input.

Users may reasonably expect these two to be identical, but they're not:

$a = ConvertTo-Json -InputObject ('[[[0,1,2],[3,4,5]]]' | ConvertFrom-Json -NoEnumerate)
$b = ('[[[0,1,2],[3,4,5]]]' | ConvertFrom-Json -NoEnumerate) | ConvertTo-Json
$c = '[[[0,1,2],[3,4,5]]]' | ConvertFrom-Json -NoEnumerate | ConvertTo-Json
$a -eq $b
# False
$a -eq $c
# True

This isn't an issue as long as we use the output only ever as input to other PowerShell functions, but it becomes a big issue once we're interacting with other systems by e.g. writing a config file or preparing a REST-API call.

Proposed technical implementation details (optional)

Currently it is possible to somewhat work around this issue by using wrapping powershell classes, custom types, or explicite type definitions. However none of them is intuitive, self documenting or obvious to unsuspecting users.
Also it hinders using the pipe and makes code less powershelly by having to pass everything as an argument instead of using the pipe. And even then it may get lost within 3rd party module code and require if-else wrapping to fix the returned data structure afterward again.

1 Add an option to OutputTypeAttribute

e.g. by changing it's definition to:
[OutputType([<TypeLiteral>], ParameterSetName="<Name>", PipelineSupportAutoUnrolling=<Bool>)]

the name is WIP, as it would default to True this way which is not ideal, as most other attributes currently default to false and only when specified are true. This one however would have to be specified with =$false to get the new behavior.
An alternative name could be SuppressAutoUnrolling but that suggests that the auto unrolling is more of a bug than a feature...

2 Add a new Attribute

Similar to 1, but instead of adding it to the OutputType Attribute make it it's own. e.g. [SuppressAutoUnrolling()]

3 Add a new pipe operator

Instead of specifying the pipe behavior with the function definition (which at least allows to write wrapper functions or handle the enumeration internally) adding a new pipe operator in addition to |, && and ||, would allow developers to better account for the auto unrolling behavior right where it causes problems, when piping.
E.g. |> or ~| could be introduced to be almost like a normal pipe, but disable the unrolling of arrays with only one element and keep them as they are. This is probably the most flexible solution without breaking existing behavior and function.

It could be used like this:

$a = ConvertTo-Json -InputObject ('[[[0,1,2],[3,4,5]]]' | ConvertFrom-Json -NoEnumerate)
$b = ('[[[0,1,2],[3,4,5]]]' | ConvertFrom-Json -NoEnumerate) |> ConvertTo-Json
$c = '[[[0,1,2],[3,4,5]]]' | ConvertFrom-Json -NoEnumerate |> ConvertTo-Json
$a -eq $b
# True
$a -eq $c
# True

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or implementation entry points. Start with the reproducible ConvertFrom-Json -NoEnumerate and ConvertTo-Json pipeline examples, then review the three proposed surfaces: OutputTypeAttribute, a new attribute, and a new pipe operator. Done means a non-breaking way to preserve single-element arrays through the pipe with equivalent JSON results.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.