< Summary - Kestrun — Combined Coverage

Information
Class: Public.Yaml.ConvertFrom-KrYaml
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Yaml/ConvertFrom-KrYaml.ps1
Tag: Kestrun/Kestrun@ca54e35c77799b76774b3805b6f075cdbc0c5fbe
Line coverage
39%
Covered lines: 22
Uncovered lines: 34
Coverable lines: 56
Total lines: 185
Line coverage: 39.2%
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 10/13/2025 - 16:52:37 Line coverage: 34% (17/50) Total lines: 161 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e01/09/2026 - 02:57:32 Line coverage: 39.2% (22/56) Total lines: 185 Tag: Kestrun/Kestrun@e3a5419e6c64edac795b8d501a4f334796c09139

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Yaml/ConvertFrom-KrYaml.ps1

#LineLine coverage
 1# Portions derived from PowerShell-Yaml (https://github.com/cloudbase/powershell-yaml)
 2# Copyright (c) 2016–2024 Cloudbase Solutions Srl
 3# Licensed under the Apache License, Version 2.0 (Apache-2.0).
 4# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 5# Modifications Copyright (c) 2025 Kestrun Contributors
 6
 7<#
 8.SYNOPSIS
 9    Converts a YAML string to a PowerShell object or hashtable.
 10.DESCRIPTION
 11    The ConvertFrom-KrYaml cmdlet converts a YAML string to a PowerShell object or
 12    hashtable. By default, it returns a PSCustomObject, but you can specify the
 13    -AsHashtable switch to get a hashtable instead.
 14.PARAMETER Yaml
 15    The YAML string to convert. This parameter is mandatory and accepts input from the pipeline.
 16.PARAMETER YamlBytes
 17    The YAML content as a byte array to convert. This parameter is mandatory when using the 'Bytes' parameter set.
 18.PARAMETER AllDocuments
 19    If specified, all documents in a multi-document YAML stream will be returned as an array. By default, only the first
 20.PARAMETER UseMergingParser
 21    If specified, the YAML parser will support the merging key (<<) for merging mappings.
 22    This is useful for YAML documents that use anchors and aliases to merge mappings.
 23.EXAMPLE
 24    $yaml = @"
 25    name: John
 26    age: 30
 27    skills:
 28      - PowerShell
 29      - YAML
 30    "@
 31    $obj = $yaml | ConvertFrom-KrYaml
 32    # Outputs a PSCustomObject with properties Name, Age, and Skills.
 33.EXAMPLE
 34    $yaml = @"
 35    ---
 36    name: John
 37    age: 30
 38    ---
 39    name: Jane Doe
 40    age: 25
 41    "@
 42    $objs = $yaml | ConvertFrom-KrYaml -AllDocuments
 43    # Outputs an array of two PSCustomObjects, one for each document in the YAML stream.
 44.EXAMPLE
 45    $yaml = @"
 46    defaults: &defaults
 47      adapter: postgres
 48      host: localhost
 49    development:
 50      database: dev_db
 51      <<: *defaults
 52    test:
 53      database: test_db
 54      <<: *defaults
 55    "@
 56    $obj = $yaml | ConvertFrom-KrYaml -UseMergingParser
 57    # Outputs a PSCustomObject with merged properties for 'development' and 'test' sections
 58    # using the 'defaults' anchor.
 59    $obj | Format-List
 60.EXAMPLE
 61    $yamlBytes = [System.Text.Encoding]::UTF8.GetBytes(@"
 62    name: John
 63    age: 30
 64    "@)
 65    $obj = ConvertFrom-KrYaml -YamlBytes $yamlBytes
 66    # Outputs a PSCustomObject with properties Name and Age.
 67.NOTES
 68    This cmdlet requires PowerShell 7.0 or later.
 69    It uses the Kestrun.Utilities.Yaml library for YAML deserialization.
 70#>
 71function ConvertFrom-KrYaml {
 72    [KestrunRuntimeApi('Everywhere')]
 73    [CmdletBinding(DefaultParameterSetName = 'String')]
 74    param(
 75        [Parameter(ValueFromPipeline = $true, Position = 0 , ParameterSetName = 'String')]
 76        [string]$Yaml,
 77
 78        [Parameter(ParameterSetName = 'Bytes')]
 79        [byte[]]$YamlBytes,
 80
 81        [Parameter()]
 82        [switch]$AllDocuments = $false,
 83
 84        [Parameter()]
 85        [switch]$UseMergingParser = $false
 86    )
 87
 88    begin {
 189        $builder = [System.Text.StringBuilder]::new()
 90    }
 91    process {
 192        if ($PSCmdlet.ParameterSetName -eq 'Bytes') {
 193            $str = [System.Text.Encoding]::UTF8.GetString($YamlBytes)
 194            if ($builder.Length -gt 0) {
 095                [void]$builder.Append("`n")
 96            }
 197            [void]$builder.Append($str)
 198        } elseif ($PSCmdlet.ParameterSetName -eq 'String') {
 199            if ($Yaml -is [string]) {
 1100                if ($builder.Length -gt 0) {
 0101                    [void]$builder.Append("`n")
 102                }
 1103                [void]$builder.Append($Yaml)
 104            }
 105        }
 106    }
 107
 108    end {
 1109        $d = $builder.ToString()
 1110        if ([string]::IsNullOrEmpty($d)) {
 111            return
 112        }
 1113        $yamlStream = [Kestrun.Utilities.Yaml.YamlLoader]::GetYamlDocuments($d, $UseMergingParser)
 1114        $documents = $yamlStream.Documents
 1115        if (!$documents -or !$documents.Count) {
 116            return
 117        }
 1118        $firstRoot = $documents[0].RootNode
 119
 120        # Extract datesAsStrings values (if present) from the parsed YAML object BEFORE conversion so we can restore the
 1121        $rawDatesAsStrings = $null
 1122        if ($firstRoot -is [System.Collections.IDictionary] -and $firstRoot.Keys -contains 'datesAsStrings') {
 0123            $seq = $firstRoot['datesAsStrings']
 0124            if ($seq -is [System.Collections.IList]) {
 125                # Collect the original values as strings, preserving their lexical form
 0126                $rawDatesAsStrings = @()
 0127                foreach ($item in $seq) {
 0128                    $rawDatesAsStrings += $item.ToString()
 129                }
 130            }
 131        }
 132
 1133        if ($documents.Count -eq 1 -and -not $AllDocuments) {
 134            # Always request ordered conversion so original key order from YAML is preserved by default.
 1135            $single = [Kestrun.Utilities.Yaml.YamlTypeConverter]::ConvertYamlDocumentToPSObject($firstRoot, $true)
 1136            $single = Convert-DateTimeOffsetToDateTime $single
 3137            if ($rawDatesAsStrings -and ($single -is [System.Collections.IDictionary]) -and ($single.Keys -contains 'dat
 0138                $seq = $single['datesAsStrings']
 0139                if ($seq -is [System.Collections.IList]) {
 0140                    for ($i = 0; $i -lt $seq.Count -and $i -lt $rawDatesAsStrings.Count; $i++) {
 141                        # Only replace if parser turned it into DateTime
 0142                        if ($seq[$i] -is [datetime] -or $seq[$i] -is [DateTimeOffset]) {
 0143                            $seq[$i] = $rawDatesAsStrings[$i]
 144                        }
 145                    }
 146                }
 147            }
 1148            return $single
 149        }
 0150        if (-not $AllDocuments) {
 151            # Always request ordered conversion so original key order from YAML is preserved by default.
 0152            $single = [Kestrun.Utilities.Yaml.YamlTypeConverter]::ConvertYamlDocumentToPSObject($firstRoot, $true)
 0153            $single = Convert-DateTimeOffsetToDateTime $single
 0154            if ($rawDatesAsStrings -and ($single -is [System.Collections.IDictionary]) -and ($single.Keys -contains 'dat
 0155                $seq = $single['datesAsStrings']
 0156                if ($seq -is [System.Collections.IList]) {
 0157                    for ($i = 0; $i -lt $seq.Count -and $i -lt $rawDatesAsStrings.Count; $i++) {
 0158                        if ($seq[$i] -is [datetime] -or $seq[$i] -is [DateTimeOffset]) {
 0159                            $seq[$i] = $rawDatesAsStrings[$i]
 160                        }
 161                    }
 162                }
 163            }
 0164            return $single
 165        }
 0166        $ret = @()
 0167        foreach ($i in $documents) {
 168            # Always preserve order in each document.
 0169            $val = [Kestrun.Utilities.Yaml.YamlTypeConverter]::ConvertYamlDocumentToPSObject($i.RootNode, $true)
 0170            $val = Convert-DateTimeOffsetToDateTime $val
 0171            if ($rawDatesAsStrings -and ($val -is [System.Collections.IDictionary]) -and ($val.Keys -contains 'datesAsSt
 0172                $seq = $val['datesAsStrings']
 0173                if ($seq -is [System.Collections.IList]) {
 0174                    for ($i2 = 0; $i2 -lt $seq.Count -and $i2 -lt $rawDatesAsStrings.Count; $i2++) {
 0175                        if ($seq[$i2] -is [datetime] -or $seq[$i2] -is [DateTimeOffset]) {
 0176                            $seq[$i2] = $rawDatesAsStrings[$i2]
 177                        }
 178                    }
 179                }
 180            }
 0181            $ret += $val
 182        }
 0183        return $ret
 184    }
 185}

Methods/Properties

ConvertFrom-KrYaml()