Skip to content

Commit dc20c75

Browse files
Handle ordered types better
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 6d58b7c commit dc20c75

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

0 Bytes
Binary file not shown.

powershell-yaml.psm1

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ function Convert-ValueToProperType {
123123
$intTypes = @([int], [long])
124124
if ([string]::IsNullOrEmpty($Node.Tag) -eq $false) {
125125
switch ($Node.Tag) {
126+
'!' {
127+
return $Node.Value
128+
}
126129
'tag:yaml.org,2002:str' {
127130
return $Node.Value
128131
}
@@ -310,6 +313,20 @@ function Convert-OrderedHashtableToDictionary {
310313
return $Data
311314
}
312315

316+
function Convert-GenericOrderedDictionaryToOrderedDictionary {
317+
param(
318+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
319+
[System.Object]$Data
320+
)
321+
# Convert System.Collections.Generic ordered dictionaries to System.Collections.Specialized.OrderedDictionary
322+
# to preserve key order when serializing with YamlDotNet
323+
$ordered = [System.Collections.Specialized.OrderedDictionary]::new()
324+
foreach ($key in $Data.Keys) {
325+
$ordered[$key] = Convert-PSObjectToGenericObject $Data[$key]
326+
}
327+
return $ordered
328+
}
329+
313330
function Convert-ListToGenericList {
314331
param(
315332
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
@@ -333,13 +350,38 @@ function Convert-PSObjectToGenericObject {
333350
}
334351

335352
$dataType = $data.GetType()
353+
354+
# Check for OrderedDictionary types first (before generic IDictionary check)
336355
if (([System.Collections.Specialized.OrderedDictionary].IsAssignableFrom($dataType))) {
337356
return Convert-OrderedHashtableToDictionary $data
338-
} elseif (([System.Collections.IDictionary].IsAssignableFrom($dataType))) {
357+
}
358+
359+
# Check for System.Collections.Generic ordered dictionary types
360+
# These need to be converted to OrderedDictionary to preserve key order in YamlDotNet
361+
if ($dataType.IsGenericType) {
362+
$genericDef = $dataType.GetGenericTypeDefinition()
363+
$genericName = $genericDef.FullName
364+
365+
# Handle System.Collections.Generic.OrderedDictionary<K,V>
366+
if ($genericName -eq 'System.Collections.Generic.OrderedDictionary`2') {
367+
return Convert-GenericOrderedDictionaryToOrderedDictionary $data
368+
}
369+
370+
# Handle System.Collections.Generic.SortedDictionary<K,V>
371+
if ($genericName -eq 'System.Collections.Generic.SortedDictionary`2') {
372+
return Convert-GenericOrderedDictionaryToOrderedDictionary $data
373+
}
374+
}
375+
376+
# Generic IDictionary handling (for Hashtable, Dictionary, etc.)
377+
if (([System.Collections.IDictionary].IsAssignableFrom($dataType))) {
339378
return Convert-HashtableToDictionary $data
340-
} elseif (([System.Collections.IList].IsAssignableFrom($dataType))) {
379+
}
380+
381+
if (([System.Collections.IList].IsAssignableFrom($dataType))) {
341382
return Convert-ListToGenericList $data
342383
}
384+
343385
return $data
344386
}
345387

0 commit comments

Comments
 (0)