Skip to content

Feature: Remove Potentially Redundant -MemoryStream Param #17

@mattcargile

Description

@mattcargile

Potential to remove -MemoryStream and add as [Alias()] to -Stream. PowerShell will bind MemoryStream by default because it implements Stream. Any Stream will bind on a ValueFromPipeline. You'd have to add [ValueFromPipeline] to -Stream too.

Not sure if you thought of this and there is an issue I am missing. Below is the function I was thinking about.

function ConvertTo-String {
    [CmdletBinding(
        DefaultParameterSetName = 'Base64String',
        HelpUri = 'https://austoonz.github.io/Convert/functions/ConvertTo-String/')]
    param
    (
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = 'Base64String')]
        [ValidateNotNullOrEmpty()]
        [String[]]
        $Base64EncodedString,

        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromPipeline = $true,
            ParameterSetName = 'Stream')]
        [ValidateNotNullOrEmpty()]
        [Alias('MemoryStream')]
        [System.IO.Stream[]]
        $Stream,

        [Parameter(ParameterSetName = 'Base64String')]
        [ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF8')]
        [String]
        $Encoding = 'UTF8',

        [Parameter(Mandatory = $false, ParameterSetName = 'Base64String')]
        [Switch]
        $Decompress
    )

    begin {
        $userErrorActionPreference = $ErrorActionPreference
    }

    process {
        $splat = @{}
        switch ($PSCmdlet.ParameterSetName) {
            'Base64String' {
                $InputObject = $Base64EncodedString
                $Function = 'ConvertFrom-Base64ToString'
                $splat.Add('Encoding', $Encoding)
                if ($Decompress) {
                    $splat.Add('Decompress', $true)
                }
                break
            }

            'Stream' {
                $InputObject = $Stream
                $Function = 'ConvertFrom-MemoryStreamToString'
                break
            }
        }

        if ($InputObject) {
            $InputObject | & $Function @splat -ErrorAction $userErrorActionPreference
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions