< Summary - Kestrun — Combined Coverage

Information
Class: Public.Yaml.ConvertTo-KrYaml
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Yaml/ConvertTo-KrYaml.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
90%
Covered lines: 10
Uncovered lines: 1
Coverable lines: 11
Total lines: 79
Line coverage: 90.9%
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: 90.9% (10/11) Total lines: 79 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Yaml/ConvertTo-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 PowerShell object or hashtable to a YAML string.
 10.DESCRIPTION
 11    The ConvertTo-KrYaml cmdlet converts a PowerShell object or hashtable to a
 12    YAML string. This is useful for serializing data in a human-readable format.
 13.PARAMETER InputObject
 14    The PowerShell object or hashtable to convert. This parameter is mandatory and accepts input from the pipeline.
 15.PARAMETER Options
 16    Specifies serialization options for the YAML output. This parameter is available in the 'Options' parameter set.
 17.PARAMETER JsonCompatible
 18    If specified, the YAML output will be formatted to be compatible with JSON. This parameter is available in the 'NoOp
 19.PARAMETER KeepArray
 20    If specified, the output will always be an array, even if there is only a single input object. By default, a single 
 21
 22.NOTES
 23    This cmdlet requires PowerShell 7.0 or later.
 24    It uses the Kestrun.Utilities.Yaml library for YAML serialization.
 25.EXAMPLE
 26    $obj = [PSCustomObject]@{ Name = "John"; Age = 30; Skills = @("PowerShell", "YAML") }
 27    $yaml = $obj | ConvertTo-KrYaml
 28    # Outputs the YAML representation of the object to the console.
 29.EXAMPLE
 30    $obj = [PSCustomObject]@{ Name = "John"; Age = 30; Skills = @("PowerShell", "YAML") }
 31    $obj | ConvertTo-KrYaml -OutFile "output.yaml" -Force
 32    # Saves the YAML representation of the object to 'output.yaml', overwriting the file if it already exists.
 33.EXAMPLE
 34    $obj = [PSCustomObject]@{ Name = "John"; Age = 30; Skills = @("PowerShell", "YAML") }
 35    $yaml = $obj | ConvertTo-KrYaml -JsonCompatible
 36    # Outputs the YAML representation of the object in a JSON-compatible format to the console.
 37#>
 38function ConvertTo-KrYaml {
 39    [KestrunRuntimeApi('Everywhere')]
 40    [OutputType([string])]
 41    [CmdletBinding(DefaultParameterSetName = 'NoOptions')]
 42    param(
 43        [Parameter(ValueFromPipeline = $true, Position = 0)]
 44        [System.Object]$InputObject,
 45
 46        [Parameter(ParameterSetName = 'Options')]
 47        [Kestrun.Utilities.Yaml.SerializationOptions]$Options = [Kestrun.Utilities.Yaml.SerializationOptions]::Roundtrip
 48
 49        [Parameter(ParameterSetName = 'NoOptions')]
 50        [switch]$JsonCompatible,
 51
 52        [switch]$KeepArray
 53    )
 54    begin {
 155        $d = [System.Collections.Generic.List[object]]::new()
 56    }
 57    process {
 158        if ($null -ne $InputObject) {
 159            $d.Add($InputObject)
 60        }
 61    }
 62    end {
 163        if ($d -eq $null -or $d.Count -eq 0) {
 64            return
 65        }
 266        if ($d.Count -eq 1 -and !($KeepArray)) {
 167            $d = $d[0]
 68        }
 169        $norm = Convert-PSObjectToGenericObject -InputObject $d
 70
 171        if ( $JsonCompatible.IsPresent) {
 72            # No indent options :~(
 073            $Options = [Kestrun.Utilities.Yaml.SerializationOptions]::JsonCompatible
 74        }
 75
 176        $out = [Kestrun.Utilities.Yaml.YamlHelper]::ToYaml($norm, $Options)
 177        return $out
 78    }
 79}

Methods/Properties

ConvertTo-KrYaml()