| | | 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 | | [OutputType([Kestrun.Hosting.Options.MapRouteOptions])] |
| | | 29 | | [CmdletBinding()] |
| | | 30 | | param( |
| | | 31 | | [Parameter(Mandatory, ValueFromPipeline)] |
| | | 32 | | [hashtable] $Property |
| | | 33 | | ) |
| | | 34 | | |
| | | 35 | | process { |
| | | 36 | | # -- discover the writable properties ---------------------------- |
| | 0 | 37 | | $type = [Kestrun.Hosting.Options.MapRouteOptions] |
| | 0 | 38 | | $writable = @{} |
| | 0 | 39 | | foreach ($p in $type.GetProperties([System.Reflection.BindingFlags]::Instance -bor ` |
| | | 40 | | [System.Reflection.BindingFlags]::Public)) { |
| | 0 | 41 | | if ($p.SetMethod -and $p.SetMethod.IsPublic) { |
| | 0 | 42 | | $writable[$p.Name.ToLowerInvariant()] = $p |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | # -- create the empty record ------------------------------------- |
| | 0 | 47 | | $options = [Activator]::CreateInstance($type) |
| | | 48 | | |
| | 0 | 49 | | foreach ($key in $Property.Keys) { |
| | 0 | 50 | | $pName = $key.ToString().ToLowerInvariant() |
| | | 51 | | |
| | | 52 | | # --- unknown key? ------------------------------------------- |
| | 0 | 53 | | if (-not $writable.ContainsKey($pName)) { |
| | 0 | 54 | | throw "Unknown option '$key'. Valid keys are: $($writable.Keys -join ', ')." |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | $prop = $writable[$pName] |
| | 0 | 58 | | $targetT = $prop.PropertyType |
| | 0 | 59 | | $rawValue = $Property[$key] |
| | | 60 | | |
| | | 61 | | # --- special case: HttpVerbs can accept strings or enum ------ |
| | | 62 | | # ---------- special: HttpVerbs accepts strings or enum --------- |
| | 0 | 63 | | if ($prop.Name -eq 'HttpVerbs') { |
| | 0 | 64 | | $converted = @() |
| | | 65 | | |
| | 0 | 66 | | foreach ($v in @($rawValue)) { |
| | 0 | 67 | | if ($v -is [Kestrun.Utilities.HttpVerb]) { |
| | 0 | 68 | | $converted += $v |
| | | 69 | | continue |
| | | 70 | | } |
| | 0 | 71 | | [Kestrun.Utilities.HttpVerb] $tmpVerb = [Kestrun.Utilities.HttpVerb]::Get |
| | 0 | 72 | | if ([Kestrun.Utilities.HttpVerbExtensions]::TryFromMethodString($v, [ref]$tmpVerb)) { |
| | 0 | 73 | | $converted += $tmpVerb |
| | | 74 | | } else { |
| | 0 | 75 | | $valid = [string]::Join(', ', [Enum]::GetNames([Kestrun.Utilities.HttpVerb])) |
| | 0 | 76 | | throw "Invalid HTTP verb '$v' in '$key'. Allowed values: $valid." |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | # Support both List[HttpVerb] and HttpVerb[] property types |
| | 0 | 81 | | if ($prop.PropertyType -eq ([System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]])) { |
| | 0 | 82 | | $list = [System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]]::new() |
| | 0 | 83 | | foreach ($item in $converted) { [void]$list.Add([Kestrun.Utilities.HttpVerb]$item) } |
| | 0 | 84 | | $prop.SetValue($options, $list, $null) |
| | 0 | 85 | | } elseif ($prop.PropertyType.IsArray) { |
| | 0 | 86 | | $prop.SetValue($options, [Kestrun.Utilities.HttpVerb[]]$converted, $null) |
| | | 87 | | } else { |
| | | 88 | | # Fallback: try to convert via LanguagePrimitives |
| | 0 | 89 | | $prop.SetValue($options, [System.Management.Automation.LanguagePrimitives]::ConvertTo($converted, $p |
| | | 90 | | } |
| | | 91 | | continue |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | # --- normal conversion -------------------------------------- |
| | | 95 | | try { |
| | 0 | 96 | | $converted = [System.Management.Automation.LanguagePrimitives]::ConvertTo( |
| | | 97 | | $rawValue, $targetT) |
| | | 98 | | } catch { |
| | 0 | 99 | | throw "Cannot convert value '$rawValue' (type $($rawValue.GetType().Name)) " + |
| | 0 | 100 | | "to [$($targetT.Name)] for option '$key'." |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | $prop.SetValue($options, $converted, $null) |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | return $options |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |