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

Align default parameter handling #78

Open
MariusStorhaug opened this issue Oct 26, 2023 · 1 comment · May be fixed by #207
Open

Align default parameter handling #78

MariusStorhaug opened this issue Oct 26, 2023 · 1 comment · May be fixed by #207
Assignees

Comments

@MariusStorhaug
Copy link
Contributor

MariusStorhaug commented Oct 26, 2023

Need a separate snippet or function to take care of defaults from the function, basically merging $PSBoundParameters and $PSCmdlet.Parameters (or something of the likes) to get the defaults if not provided in $PSBoundParameters. This will make it so that the default actually get used in the body for API calls.

Something like:

    $PSCmdlet.MyInvocation.MyCommand.Parameters.GetEnumerator() | ForEach-Object {
        $paramName = $_.Key
        $paramDefaultValue = Get-Variable -Name $paramName -ValueOnly -ErrorAction SilentlyContinue
        $providedValue = $PSBoundParameters[$paramName]
        Write-Verbose "[$paramName]"
        Write-Verbose "  - Default:  [$paramDefaultValue]"
        Write-Verbose "  - Provided: [$providedValue]"
        if (-not $PSBoundParameters.ContainsKey($paramName) -and ($null -ne $paramDefaultValue)) {
            Write-Verbose '  - Using default value'
            $PSBoundParameters[$paramName] = $paramDefaultValue
        } else {
            Write-Verbose '  - Using provided value'
        }
    }

    $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
    Remove-HashtableEntry -Hashtable $body -RemoveNames 'Owner', 'Repo' -RemoveTypes 'SwitchParameter'
@MariusStorhaug
Copy link
Contributor Author

Alot of processing to handle body params... Need to remove this and just go back to just handle it with a hashtable:

$body = @{
   ...
}

Faster to do it like this than processing data back and forth... :)

@MariusStorhaug MariusStorhaug moved this from Todo to In Progress in GitHub PowerShell Module Dec 15, 2024
@MariusStorhaug MariusStorhaug self-assigned this Dec 15, 2024
@MariusStorhaug MariusStorhaug linked a pull request Dec 15, 2024 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.

1 participant