Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated example to use current syntax
  • Loading branch information
dkattan committed Oct 19, 2023
commit d10a68084f23e903fb08e7e9a807337783ac6b8f
113 changes: 87 additions & 26 deletions Draft/RealTime-Parameter-Bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,107 @@ Facilitate PowerShell's dynamic parameter block by allowing `RuntimeDefinedParam

Suppose a user wants to interactively install different versions of Microsoft Edge, filtered by Product, Platform, and Architecture. With this proposed change, once the user selects a Product, the subsequent options for Platform are filtered based on that selection. Once a Platform is selected, the Architecture choices are then filtered, and so forth.

The sample code below demonstrates how this experience might look with the proposed changes:
The sample code below demonstrates how this experience _might_ look with the proposed changes:

```powershell
param()
dynamicparam
{
New-ParameterCollection -Parameters @(
$EdgeVersions = Invoke-RestMethod https://edgeupdates.microsoft.com/api/products
New-RuntimeDefinedParameter -Name Product -ValidValues $EdgeVersions.Product -Type ([string]) -Mandatory
if($Product)
{
$EdgeVersions = $EdgeVersions | ?{$_.Product -eq $Product}
}
$Platforms = $EdgeVersions | %{ $_.Releases } | select -Unique -Expand Platform
New-RuntimeDefinedParameter -Name Platform -ValidValues $Platforms -Type ([string]) -Mandatory
if($Platform)
{
$EdgeVersions = $EdgeVersions | ?{$_.Releases.Platform -eq $Platform}
}
$EdgeVersions = Invoke-RestMethod https://edgeupdates.microsoft.com/api/products

$productParamAttributes = [Collection[Attribute]]@(
([ValidateSet]::new($EdgeVersions.Product)),
([Parameter]@{ Mandatory = $true })
)
[RuntimeDefinedParameter]::new('Product', [string], $productParamAttributes)

if($Product)
{
$EdgeVersions = $EdgeVersions | ?{$_.Product -eq $Product}
$Platforms = $EdgeVersions | %{ $_.Releases } | select -Unique -Expand Platform

$platformParamAttributes = [Collection[Attribute]]@(
([ValidateSet]::new($Platforms)),
([Parameter]@{ Mandatory = $true })
)
[RuntimeDefinedParameter]::new('Platform', [string], $platformParamAttributes)

if($Platform)
{
$EdgeVersions = $EdgeVersions | ?{$_.Releases.Platform -eq $Platform}
$Architectures = $EdgeVersions.Releases.Architecture | select -Unique | sort
New-RuntimeDefinedParameter -Name Architecture -ValidValues $Architectures -Type ([string]) -Mandatory

$architectureParamAttributes = [Collection[Attribute]]@(
([ValidateSet]::new($Architectures)),
([Parameter]@{ Mandatory = $true })
)
[RuntimeDefinedParameter]::new('Architecture', [string], $architectureParamAttributes)

if($Architecture)
{
$EdgeVersions = $EdgeVersions | ?{$_.Releases.Architecture -eq $Architecture}
$EdgeVersions = $EdgeVersions | ?{$_.Releases.Architecture -eq $Architecture}
$versionToInstallValues = $EdgeVersions.Releases.ProductVersion | select -Unique

$versionParamAttributes = [Collection[Attribute]]@(
([ValidateSet]::new($versionToInstallValues)),
([Parameter]@{ Mandatory = $true })
)
[RuntimeDefinedParameter]::new('Version', [string], $versionParamAttributes)
}
New-RuntimeDefinedParameter -Name VersionToInstall -ValidValues ($EdgeVersions.Releases.ProductVersion | select -Unique) -Type ([string]) -Mandatory
)
}
end
{
}
}
```

Upon execution, users will be prompted incrementally:

```output
Product: [List of Products]
Platform: [Filtered List of Platforms]
Architecture: [Filtered List of Architectures]
VersionToInstall: [Filtered List of Versions]
Version: [Filtered List of Versions]
```

The syntax could be even more concise with user-supplied helper function `New-Parameter`
```powershell
function New-Parameter {
param(
[string]$Name,
[Type]$Type = [object],
[string[]]$ValidValues,
[switch]$Mandatory
)

$attributes = [Collection[Attribute]]@(
([ValidateSet]::new($ValidValues)),
([Parameter]@{ Mandatory = $Mandatory })
)

return [RuntimeDefinedParameter]::new($Name, $Type, $attributes)
}

Function Get-EdgeUpdates
{
param()
dynamicparam
{
$EdgeVersions = Invoke-RestMethod https://edgeupdates.microsoft.com/api/products
New-Parameter -Name Product -ValidValues $EdgeVersions.Product -Type ([string]) -Mandatory
if($Product)
{
$EdgeReleases = $EdgeVersions | ?{$_.Product -eq $Product} | select -Expand Releases # Beta,Stable,Dev,Canary
New-Parameter -Name Platform -ValidValues ($EdgeReleases.Platform | select -Unique) -Type ([string]) -Mandatory # Windows,Linux,MacOS,Android
if($Platform)
{
$EdgeReleases = $EdgeReleases | ?{$_.Platform -eq $Platform}
New-Parameter -Name Architecture -ValidValues ($EdgeReleases.Architecture | select -Unique) -Type ([string]) -Mandatory # x86,x64,arm64,universal
if($Architecture)
{
$EdgeReleases = $EdgeReleases | ?{$_.Architecture -eq $Architecture}
New-Parameter -Name Version -ValidValues ($EdgeReleases.ProductVersion | select -Unique) -Type ([string]) -Mandatory
}
}
}
}
end
{
}
}
```

## Specification
Expand Down