< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 109
Line coverage: 0%
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 09/01/2025 - 04:08:24 Line coverage: 77.1% (27/35) Total lines: 107 Tag: Kestrun/Kestrun@d6f26a131219b7a7fcb4e129af3193ec2ec4892909/04/2025 - 18:11:31 Line coverage: 77.1% (27/35) Total lines: 108 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0510/13/2025 - 16:52:37 Line coverage: 0% (0/35) Total lines: 109 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

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    [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 ----------------------------
 037        $type = [Kestrun.Hosting.Options.MapRouteOptions]
 038        $writable = @{}
 039        foreach ($p in $type.GetProperties([System.Reflection.BindingFlags]::Instance -bor `
 40                    [System.Reflection.BindingFlags]::Public)) {
 041            if ($p.SetMethod -and $p.SetMethod.IsPublic) {
 042                $writable[$p.Name.ToLowerInvariant()] = $p
 43            }
 44        }
 45
 46        # -- create the empty record -------------------------------------
 047        $options = [Activator]::CreateInstance($type)
 48
 049        foreach ($key in $Property.Keys) {
 050            $pName = $key.ToString().ToLowerInvariant()
 51
 52            # --- unknown key? -------------------------------------------
 053            if (-not $writable.ContainsKey($pName)) {
 054                throw "Unknown option '$key'. Valid keys are: $($writable.Keys -join ', ')."
 55            }
 56
 057            $prop = $writable[$pName]
 058            $targetT = $prop.PropertyType
 059            $rawValue = $Property[$key]
 60
 61            # --- special case: HttpVerbs can accept strings or enum ------
 62            # ---------- special: HttpVerbs accepts strings or enum ---------
 063            if ($prop.Name -eq 'HttpVerbs') {
 064                $converted = @()
 65
 066                foreach ($v in @($rawValue)) {
 067                    if ($v -is [Kestrun.Utilities.HttpVerb]) {
 068                        $converted += $v
 69                        continue
 70                    }
 071                    [Kestrun.Utilities.HttpVerb] $tmpVerb = [Kestrun.Utilities.HttpVerb]::Get
 072                    if ([Kestrun.Utilities.HttpVerbExtensions]::TryFromMethodString($v, [ref]$tmpVerb)) {
 073                        $converted += $tmpVerb
 74                    } else {
 075                        $valid = [string]::Join(', ', [Enum]::GetNames([Kestrun.Utilities.HttpVerb]))
 076                        throw "Invalid HTTP verb '$v' in '$key'. Allowed values: $valid."
 77                    }
 78                }
 79
 80                # Support both List[HttpVerb] and HttpVerb[] property types
 081                if ($prop.PropertyType -eq ([System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]])) {
 082                    $list = [System.Collections.Generic.List[Kestrun.Utilities.HttpVerb]]::new()
 083                    foreach ($item in $converted) { [void]$list.Add([Kestrun.Utilities.HttpVerb]$item) }
 084                    $prop.SetValue($options, $list, $null)
 085                } elseif ($prop.PropertyType.IsArray) {
 086                    $prop.SetValue($options, [Kestrun.Utilities.HttpVerb[]]$converted, $null)
 87                } else {
 88                    # Fallback: try to convert via LanguagePrimitives
 089                    $prop.SetValue($options, [System.Management.Automation.LanguagePrimitives]::ConvertTo($converted, $p
 90                }
 91                continue
 92            }
 93
 94            # --- normal conversion --------------------------------------
 95            try {
 096                $converted = [System.Management.Automation.LanguagePrimitives]::ConvertTo(
 97                    $rawValue, $targetT)
 98            } catch {
 099                throw "Cannot convert value '$rawValue' (type $($rawValue.GetType().Name)) " +
 0100                "to [$($targetT.Name)] for option '$key'."
 101            }
 102
 0103            $prop.SetValue($options, $converted, $null)
 104        }
 105
 0106        return $options
 107    }
 108}
 109

Methods/Properties

New-KrMapRouteOption()