< Summary - Kestrun — Combined Coverage

Information
Class: Public.SharedState.Set-KrSharedState
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/SharedState/Set-KrSharedState.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
57%
Covered lines: 4
Uncovered lines: 3
Coverable lines: 7
Total lines: 79
Line coverage: 57.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 08/26/2025 - 14:53:17 Line coverage: 100% (1/1) Total lines: 42 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 17:02:01 Line coverage: 100% (1/1) Total lines: 48 Tag: Kestrun/Kestrun@f3880b25ea131298aa2f8b1e0d0a8d55eb160bc009/04/2025 - 22:37:32 Line coverage: 100% (1/1) Total lines: 49 Tag: Kestrun/Kestrun@afb7aadc0a8a42bfa2b51ea62c8a6e2cf63faec611/14/2025 - 12:29:34 Line coverage: 57.1% (4/7) Total lines: 79 Tag: Kestrun/Kestrun@5e12b09a6838e68e704cd3dc975331b9e680a626

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/SharedState/Set-KrSharedState.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Defines or updates a global variable accessible across Kestrun scripts.
 4
 5.DESCRIPTION
 6    Stores a value in the Kestrun global variable table. Variables may be marked
 7    as read-only to prevent accidental modification.
 8    If the variable already exists, its value is updated. If it does not exist,
 9    it is created.
 10.PARAMETER Server
 11    The Kestrun server instance.
 12.PARAMETER Global
 13    If specified, the variable is stored in the global shared state.
 14.PARAMETER Name
 15    Name of the variable to create or update.
 16.PARAMETER Value
 17    Value to assign to the variable.
 18.PARAMETER AllowsValueType
 19    If specified, allows the variable to hold value types (e.g., int, bool).
 20.PARAMETER ThreadSafe
 21    If specified, the value is converted to a thread-safe representation.
 22.EXAMPLE
 23    Set-KrSharedState -Name "MyVariable" -Value "Hello, World!"
 24    This creates a global variable "MyVariable" with the value "Hello, World!".
 25.EXAMPLE
 26    Set-KrSharedState -Name "MyNamespace.MyVariable" -Value @{item=42}
 27    This creates a global variable "MyNamespace.MyVariable" with the value @{item=42}.
 28.NOTES
 29    This function is part of the Kestrun.SharedState module and is used to define or update global variables.
 30#>
 31function Set-KrSharedState {
 32    [KestrunRuntimeApi('Definition')]
 33    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 34    [CmdletBinding(defaultParameterSetName = 'Server')]
 35    param(
 36        [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Server')]
 37        [Kestrun.Hosting.KestrunHost]$Server,
 38
 39        [Parameter(Mandatory = $true, ParameterSetName = 'Global')]
 40        [switch]$Global,
 41
 42        [Parameter(Mandatory)]
 43        [string]$Name,
 44
 45        [Parameter(Mandatory)]
 46        [object]$Value,
 47
 48        [Parameter()]
 49        [switch]$AllowsValueType,
 50
 51        [Parameter()]
 52        [switch]$ThreadSafe
 53
 54    )
 55    begin {
 156        if (-not $Global.IsPresent) {
 57            # Ensure the server instance is resolved
 058            $Server = Resolve-KestrunServer -Server $Server
 59        }
 60    }
 61    process {
 162        if ($ThreadSafe.IsPresent) {
 063            $Value = ConvertTo-KrThreadSafeValue -Value $Value
 64        }
 165        if ($Global.IsPresent) {
 66            # Retrieve from server instance
 167            return [Kestrun.SharedState.GlobalStore]::Set($Name,
 68                $Value,
 69                $AllowsValueType.IsPresent  # Allow value types if specified
 70            )
 71        }
 72        # Define or update the variable; throws if it was already read-only
 073        $null = $Server.SharedState.Set(
 74            $Name,
 75            $Value,
 76            $AllowsValueType.IsPresent  # Allow value types if specified
 77        )
 78    }
 79}

Methods/Properties

Set-KrSharedState()