| | | 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 | | Convert a list (array) to a generic List[object], converting any nested PSObjects to generic objects. |
| | | 10 | | .DESCRIPTION |
| | | 11 | | This function takes a list (array) as input and converts it to a System.Collections.Generic.List[object]. It ensures |
| | | 12 | | The order of elements in the list is preserved in the resulting generic list. |
| | | 13 | | .PARAMETER Data |
| | | 14 | | The list (array) to convert. |
| | | 15 | | .EXAMPLE |
| | | 16 | | $list = @( "Value1", [PSCustomObject]@{ Prop1 = "Val1"; Prop2 = "Val2" } ) |
| | | 17 | | $genericList = Convert-ListToGenericList -Data $list |
| | | 18 | | # $genericList is now a List[object] with the same elements, where the second element is a generic object. |
| | | 19 | | .NOTES |
| | | 20 | | This function is designed to work with PowerShell 7.0 and above. |
| | | 21 | | #> |
| | | 22 | | function Convert-ListToGenericList { |
| | | 23 | | param( |
| | | 24 | | [Parameter(Mandatory = $false)] |
| | | 25 | | [array]$Data = @() |
| | | 26 | | ) |
| | 1 | 27 | | $ret = [System.Collections.Generic.List[object]]::new() |
| | 3 | 28 | | for ($i = 0; $i -lt $Data.Count; $i++) { |
| | 2 | 29 | | $ret.Add((Convert-PSObjectToGenericObject $Data[$i])) |
| | | 30 | | } |
| | | 31 | | # Return the generic list directly (do NOT wrap in a single-element array) so single-element |
| | | 32 | | # sequences remain proper YAML sequences and do not collapse into mappings during round-trip. |
| | 1 | 33 | | return $ret |
| | | 34 | | } |