Skip to content

Commit

Permalink
Merge pull request #37 from santisq/36-fix-encodingtransformation-for…
Browse files Browse the repository at this point in the history
…-encoding-and-int-instances-wrapped-in-psobject

Fix `EncodingTransformation` for `Encoding` and `int` instances wrapped in `PSObject`
  • Loading branch information
santisq authored Nov 6, 2024
2 parents ef826cd + f397433 commit 15cf8e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion module/PSCompression.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'bin/netstandard2.0/PSCompression.dll'

# Version number of this module.
ModuleVersion = '2.0.9'
ModuleVersion = '2.0.10'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
11 changes: 8 additions & 3 deletions src/PSCompression/EncodingTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ public sealed class EncodingTransformation : ArgumentTransformationAttribute
{
public override object Transform(
EngineIntrinsics engineIntrinsics,
object inputData) =>
inputData switch
object inputData)
{
inputData = inputData is PSObject pso
? pso.BaseObject
: inputData;

return inputData switch
{
Encoding enc => enc,
int num => Encoding.GetEncoding(num),
string str => ParseStringEncoding(str),
PSObject pso when pso.BaseObject is string str => ParseStringEncoding(str),
_ => throw new ArgumentTransformationMetadataException(
$"Could not convert input '{inputData}' to a valid Encoding object."),
};
}

private Encoding ParseStringEncoding(string str) =>
str.ToLowerInvariant() switch
Expand Down
15 changes: 11 additions & 4 deletions tests/ZipEntryCmdlets.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,18 @@ Describe 'ZipEntry Cmdlets' {
Should -BeOfType ([string])
}

It 'Should not throw when a string wrapped in PSObject is passed as Encdoing argument' {
It 'Should not throw when an instance wrapped in PSObject is passed as Encdoing argument' {
$enc = Write-Output utf8
$zip | Get-ZipEntry -Type Archive |
Get-ZipEntryContent -Encoding $enc |
Should -BeOfType ([string])
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
Should -Not -Throw

$enc = [System.Text.Encoding]::UTF8 | Write-Output
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
Should -Not -Throw

$enc = [System.Text.Encoding]::UTF8.CodePage | Write-Output
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
Should -Not -Throw
}

It 'Can read bytes from zip file entries' {
Expand Down

0 comments on commit 15cf8e4

Please sign in to comment.