< Summary - Kestrun — Combined Coverage

Information
Class: Public.Route.New-KrMapRouteOption
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/New-KrMapRouteOption.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
77%
Covered lines: 27
Uncovered lines: 8
Coverable lines: 35
Total lines: 108
Line coverage: 77.1%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/New-KrMapRouteOption.ps1

#LineLine coverage
 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#>
 25function 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 ----------------------------
 136        $type = [Kestrun.Hosting.Options.MapRouteOptions]
 137        $writable = @{}
 138        foreach ($p in $type.GetProperties([System.Reflection.BindingFlags]::Instance -bor `
 39                    [System.Reflection.BindingFlags]::Public)) {
 140            if ($p.SetMethod -and $p.SetMethod.IsPublic) {
 141                $writable[$p.Name.ToLowerInvariant()] = $p
 42            }
 43        }
 44
 45        # -- create the empty record -------------------------------------
 146        $options = [Activator]::CreateInstance($type)
 47
 148        foreach ($key in $Property.Keys) {
 149            $pName = $key.ToString().ToLowerInvariant()
 50
 51            # --- unknown key? -------------------------------------------
 152            if (-not $writable.ContainsKey($pName)) {
 053                throw "Unknown option '$key'. Valid keys are: $($writable.Keys -join ', ')."
 54            }
 55
 156            $prop = $writable[$pName]
 157            $targetT = $prop.PropertyType
 158            $rawValue = $Property[$key]
 59
 60            # --- special case: HttpVerbs can accept strings or enum ------
 61            # ---------- special: HttpVerbs accepts strings or enum ---------
 162            if ($prop.Name -eq 'HttpVerbs') {
 163                $converted = @()
 64
 265                foreach ($v in @($rawValue)) {
 166                    if ($v -is [Kestrun.Utilities.HttpVerb]) {
 167                        $converted += $v
 68                        continue
 69                    }
 170                    [Kestrun.Utilities.HttpVerb] $tmpVerb = [Kestrun.Utilities.HttpVerb]::Get
 171                    if ([Kestrun.Utilities.HttpVerbExtensions]::TryFromMethodString($v, [ref]$tmpVerb)) {
 172                        $converted += $tmpVerb
 73                    } else {
 074                        $valid = [string]::Join(', ', [Enum]::GetNames([Kestrun.Utilities.HttpVerb]))
 075                        throw "Invalid HTTP verb '$v' in '$key'. Allowed values: $valid."
 76                    }
 77                }
 78
 79                # Support both List[HttpVerb] and HttpVerb[] property types
 280                if ($prop.PropertyType -eq ([System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]])) {
 181                    $list = [System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]]::new()
 282                    foreach ($item in $converted) { [void]$list.Add([Kestrun.Utilities.HttpVerb]$item) }
 183                    $prop.SetValue($options, $list, $null)
 084                } elseif ($prop.PropertyType.IsArray) {
 085                    $prop.SetValue($options, [Kestrun.Utilities.HttpVerb[]]$converted, $null)
 86                } else {
 87                    # Fallback: try to convert via LanguagePrimitives
 088                    $prop.SetValue($options, [System.Management.Automation.LanguagePrimitives]::ConvertTo($converted, $p
 89                }
 90                continue
 91            }
 92
 93            # --- normal conversion --------------------------------------
 94            try {
 195                $converted = [System.Management.Automation.LanguagePrimitives]::ConvertTo(
 96                    $rawValue, $targetT)
 97            } catch {
 098                throw "Cannot convert value '$rawValue' (type $($rawValue.GetType().Name)) " +
 099                "to [$($targetT.Name)] for option '$key'."
 100            }
 101
 1102            $prop.SetValue($options, $converted, $null)
 103        }
 104
 1105        return $options
 106    }
 107}
 108

Methods/Properties

New-KrMapRouteOption()