| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Creates a new instance of the Kestrun.Hosting.Options.MapRouteOptions class. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function initializes a new instance of the MapRouteOptions class, which is used to configure |
| | 6 | | routing options for Kestrun server routes. |
| | 7 | | .PARAMETER Property |
| | 8 | | A hashtable containing properties to set on the MapRouteOptions instance. The keys should match |
| | 9 | | the property names of the MapRouteOptions class. |
| | 10 | | .OUTPUTS |
| | 11 | | [Kestrun.Hosting.Options.MapRouteOptions] |
| | 12 | | A new instance of the MapRouteOptions class. |
| | 13 | | .EXAMPLE |
| | 14 | | $options = New-KrMapRouteOption -Property @{ |
| | 15 | | Path = "/myroute" |
| | 16 | | HttpVerbs = "Get", "Post" |
| | 17 | | } |
| | 18 | | This example creates a new MapRouteOptions instance with specified path and HTTP verbs. |
| | 19 | | .NOTES |
| | 20 | | This function is part of the Kestrun.Hosting module and is used to manage route options. |
| | 21 | | Maps to MapRouteOptions constructor. |
| | 22 | | .LINK |
| | 23 | | https://docs.microsoft.com/en-us/dotnet/api/kestrun.hosting.options.maprouteoptions |
| | 24 | | #> |
| | 25 | | function New-KrMapRouteOption { |
| | 26 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 27 | | [KestrunRuntimeApi('Definition')] |
| | 28 | | [CmdletBinding()] |
| | 29 | | param( |
| | 30 | | [Parameter(Mandatory, ValueFromPipeline)] |
| | 31 | | [hashtable] $Property |
| | 32 | | ) |
| | 33 | |
|
| | 34 | | process { |
| | 35 | | # -- discover the writable properties ---------------------------- |
| 1 | 36 | | $type = [Kestrun.Hosting.Options.MapRouteOptions] |
| 1 | 37 | | $writable = @{} |
| 1 | 38 | | foreach ($p in $type.GetProperties([System.Reflection.BindingFlags]::Instance -bor ` |
| | 39 | | [System.Reflection.BindingFlags]::Public)) { |
| 1 | 40 | | if ($p.SetMethod -and $p.SetMethod.IsPublic) { |
| 1 | 41 | | $writable[$p.Name.ToLowerInvariant()] = $p |
| | 42 | | } |
| | 43 | | } |
| | 44 | |
|
| | 45 | | # -- create the empty record ------------------------------------- |
| 1 | 46 | | $options = [Activator]::CreateInstance($type) |
| | 47 | |
|
| 1 | 48 | | foreach ($key in $Property.Keys) { |
| 1 | 49 | | $pName = $key.ToString().ToLowerInvariant() |
| | 50 | |
|
| | 51 | | # --- unknown key? ------------------------------------------- |
| 1 | 52 | | if (-not $writable.ContainsKey($pName)) { |
| 0 | 53 | | throw "Unknown option '$key'. Valid keys are: $($writable.Keys -join ', ')." |
| | 54 | | } |
| | 55 | |
|
| 1 | 56 | | $prop = $writable[$pName] |
| 1 | 57 | | $targetT = $prop.PropertyType |
| 1 | 58 | | $rawValue = $Property[$key] |
| | 59 | |
|
| | 60 | | # --- special case: HttpVerbs can accept strings or enum ------ |
| | 61 | | # ---------- special: HttpVerbs accepts strings or enum --------- |
| 1 | 62 | | if ($prop.Name -eq 'HttpVerbs') { |
| 1 | 63 | | $converted = @() |
| | 64 | |
|
| 2 | 65 | | foreach ($v in @($rawValue)) { |
| 1 | 66 | | if ($v -is [Kestrun.Utilities.HttpVerb]) { |
| 1 | 67 | | $converted += $v |
| | 68 | | continue |
| | 69 | | } |
| 1 | 70 | | [Kestrun.Utilities.HttpVerb] $tmpVerb = [Kestrun.Utilities.HttpVerb]::Get |
| 1 | 71 | | if ([Kestrun.Utilities.HttpVerbExtensions]::TryFromMethodString($v, [ref]$tmpVerb)) { |
| 1 | 72 | | $converted += $tmpVerb |
| | 73 | | } else { |
| 0 | 74 | | $valid = [string]::Join(', ', [Enum]::GetNames([Kestrun.Utilities.HttpVerb])) |
| 0 | 75 | | throw "Invalid HTTP verb '$v' in '$key'. Allowed values: $valid." |
| | 76 | | } |
| | 77 | | } |
| | 78 | |
|
| | 79 | | # Support both List[HttpVerb] and HttpVerb[] property types |
| 2 | 80 | | if ($prop.PropertyType -eq ([System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]])) { |
| 1 | 81 | | $list = [System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]]::new() |
| 2 | 82 | | foreach ($item in $converted) { [void]$list.Add([Kestrun.Utilities.HttpVerb]$item) } |
| 1 | 83 | | $prop.SetValue($options, $list, $null) |
| 0 | 84 | | } elseif ($prop.PropertyType.IsArray) { |
| 0 | 85 | | $prop.SetValue($options, [Kestrun.Utilities.HttpVerb[]]$converted, $null) |
| | 86 | | } else { |
| | 87 | | # Fallback: try to convert via LanguagePrimitives |
| 0 | 88 | | $prop.SetValue($options, [System.Management.Automation.LanguagePrimitives]::ConvertTo($converted, $p |
| | 89 | | } |
| | 90 | | continue |
| | 91 | | } |
| | 92 | |
|
| | 93 | | # --- normal conversion -------------------------------------- |
| | 94 | | try { |
| 1 | 95 | | $converted = [System.Management.Automation.LanguagePrimitives]::ConvertTo( |
| | 96 | | $rawValue, $targetT) |
| | 97 | | } catch { |
| 0 | 98 | | throw "Cannot convert value '$rawValue' (type $($rawValue.GetType().Name)) " + |
| 0 | 99 | | "to [$($targetT.Name)] for option '$key'." |
| | 100 | | } |
| | 101 | |
|
| 1 | 102 | | $prop.SetValue($options, $converted, $null) |
| | 103 | | } |
| | 104 | |
|
| 1 | 105 | | return $options |
| | 106 | | } |
| | 107 | | } |
| | 108 | |
|