Skip to content

Commit

Permalink
Fix handling of numerical configuration properties across PS5 and PS7 (
Browse files Browse the repository at this point in the history
…#262)

`ConvertFrom-Json` defaults numbers to different types depending on the PowerShell version.

PS5 defaults numbers to `[Int32]` while PS7 defaults numbers to `[Int64]`, but both versions default to `[Int32]` in a `[PSCustomObject]`.

Due to this, if you had a number config value saved, on PS7 it would be ignored and the default value would be used because the expected and actual types wouldn't match.

I've updated `Resolve-PropertyValue` to have equivalence logic for number types to avoid this issue.
  • Loading branch information
HowardWolosky committed Jul 10, 2020
1 parent 79e5ac2 commit 683187a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions GitHubConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,18 @@ function Resolve-PropertyValue
if ($Type -eq 'Boolean') { $typeType = [Boolean] }
if ($Type -eq 'Int32') { $typeType = [Int32] }
if ($Type -eq 'Int64') { $typeType = [Int64] }
$numberEquivalents = @('Int32', 'Int64', 'long', 'int')

if (Test-PropertyExists -InputObject $InputObject -Name $Name)
{
if ($InputObject.$Name -is $typeType)
if (($InputObject.$Name -is $typeType) -or
(($Type -in $numberEquivalents) -and ($InputObject.$Name.GetType().Name -in $numberEquivalents)))
{
return $InputObject.$Name
}
else
{
$message = "The locally cached $Name configuration was not of type $Type. Reverting to default value."
$message = "The locally cached $Name configuration was not of type $Type (it was $($InputObject.$Name.GetType())). Reverting to default value."
Write-Log -Message $message -Level Warning
return $DefaultValue
}
Expand Down

0 comments on commit 683187a

Please sign in to comment.