Skip to content

Commit

Permalink
Fix losing case sensitive keys
Browse files Browse the repository at this point in the history
Powershell is case aware but case insensitive for keys. By default if
we create a hashtable @{}, keys are case insensitive. To force case
sensitivity in hashtables, we need to call the C# constructor.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  • Loading branch information
gabriel-samfira committed Aug 27, 2024
1 parent 0d5b89f commit 7980c7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Tests/powershell-yaml.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -669,4 +669,13 @@ int64: 9223372036854775807
} | Should -Not -Throw
}
}

Describe 'Case insensitive keys in mappings' {
It 'should deserialize both keys' {
$yaml = '{"a": 1, "A": 2}'
$result = ConvertFrom-Yaml -Yaml $yaml
$result.a | Should -Be 1
$result.A | Should -Be 2
}
}
}
2 changes: 1 addition & 1 deletion powershell-yaml.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function Convert-YamlMappingToHashtable {
[switch] $Ordered
)
PROCESS {
if ($Ordered) { $ret = [ordered]@{} } else { $ret = @{} }
if ($Ordered) { $ret = [System.Collections.Specialized.OrderedDictionary]::new() } else { $ret = [hashtable]::new() }
foreach($i in $Node.Children.Keys) {
$ret[$i.Value] = Convert-YamlDocumentToPSObject $Node.Children[$i] -Ordered:$Ordered
}
Expand Down

0 comments on commit 7980c7c

Please sign in to comment.