< Summary - Kestrun — Combined Coverage

Information
Class: Private.ConvertTo.ConvertTo-ThreadSafeValue
Assembly: Kestrun.PowerShell.Private
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/ConvertTo/ConvertTo-ThreadSafeValue.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 19
Coverable lines: 19
Total lines: 69
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 11/14/2025 - 12:29:34 Line coverage: 0% (0/19) Total lines: 69 Tag: Kestrun/Kestrun@5e12b09a6838e68e704cd3dc975331b9e680a626

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/ConvertTo/ConvertTo-ThreadSafeValue.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Converts collections to thread-safe equivalents.
 4.DESCRIPTION
 5    This function takes various collection types (hashtables, arrays, dictionaries)
 6    and converts them into thread-safe versions suitable for use in multi-threaded
 7    or multi-runspace scenarios.
 8.PARAMETER Value
 9    The input collection to convert.
 10.EXAMPLE
 11    # Convert a hashtable to a thread-safe hashtable
 12    $ht = @{ Key1 = 'Value1'; Key2 = 'Value2' }
 13    $threadSafeHt = ConvertTo-KrThreadSafeValue -Value $ht
 14.EXAMPLE
 15    # Convert an ArrayList to a thread-safe ArrayList
 16    $arrayList = [System.Collections.ArrayList]::new()
 17    $threadSafeArrayList = ConvertTo-KrThreadSafeValue -Value $arrayList
 18.OUTPUTS
 19    Thread-safe collection equivalent of the input. If the input is not a collection, returns it unchanged.
 20#>
 21function ConvertTo-KrThreadSafeValue {
 22    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '')]
 23    [CmdletBinding()]
 24    param(
 25        [Parameter(Mandatory)]
 26        [object]$Value
 27    )
 28
 029    if ($null -eq $Value) {
 030        return $null
 31    }
 32
 33    # --- Hashtable (@{}) ---
 034    if ($Value -is [hashtable]) {
 035        return [hashtable]::Synchronized($Value)
 36    }
 37
 38    # --- OrderedDictionary ([ordered]@{}) ---
 039    if ($Value -is [System.Collections.Specialized.OrderedDictionary]) {
 40        # Copy into a normal hashtable and wrap
 041        $ht = @{}
 042        foreach ($entry in $Value.GetEnumerator()) {
 043            $ht[$entry.Key] = $entry.Value
 44        }
 045        return [hashtable]::Synchronized($ht)
 46    }
 47
 48    # --- ArrayList ---
 049    if ($Value -is [System.Collections.ArrayList]) {
 050        return [System.Collections.ArrayList]::Synchronized($Value)
 51    }
 52
 53    # --- Any other IDictionary (generic or not, but not handled above) ---
 054    if ($Value -is [System.Collections.IDictionary]) {
 055        $dict = [System.Collections.Concurrent.ConcurrentDictionary[object, object]]::new()
 056        foreach ($entry in $Value.GetEnumerator()) {
 057            $null = $dict.TryAdd($entry.Key, $entry.Value)
 58        }
 059        return $dict
 60    }
 61
 62    # --- Arrays: treat as immutable snapshots ---
 063    if ($Value -is [Array]) {
 064        return $Value
 65    }
 66
 67    # --- PSCustomObject, scalars, etc.: just return as-is ---
 068    return $Value
 69}

Methods/Properties

ConvertTo-KrThreadSafeValue()