Skip to content

Commit 58c5980

Browse files
Address PR review feedback
1 parent 7923add commit 58c5980

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/functions/private/ConvertTo-LuaTable.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,21 @@
7979
return $InputObject.ToString([System.Globalization.CultureInfo]::InvariantCulture)
8080
}
8181

82-
if ($InputObject -is [float] -or $InputObject -is [double] -or
83-
$InputObject -is [decimal] -or $InputObject -is [single]) {
82+
if ($InputObject -is [double]) {
83+
if ([double]::IsNaN($InputObject) -or [double]::IsInfinity($InputObject)) {
84+
throw "Cannot serialize non-finite double value '$InputObject' to Lua. Lua numeric literals do not support NaN or Infinity."
85+
}
86+
return $InputObject.ToString([System.Globalization.CultureInfo]::InvariantCulture)
87+
}
88+
89+
if ($InputObject -is [float] -or $InputObject -is [single]) {
90+
if ([single]::IsNaN($InputObject) -or [single]::IsInfinity($InputObject)) {
91+
throw "Cannot serialize non-finite single value '$InputObject' to Lua. Lua numeric literals do not support NaN or Infinity."
92+
}
93+
return $InputObject.ToString([System.Globalization.CultureInfo]::InvariantCulture)
94+
}
95+
96+
if ($InputObject -is [decimal]) {
8497
return $InputObject.ToString([System.Globalization.CultureInfo]::InvariantCulture)
8598
}
8699

@@ -155,7 +168,7 @@
155168
}
156169

157170
# Handle PSCustomObject
158-
if ($InputObject -is [psobject]) {
171+
if ($InputObject -is [System.Management.Automation.PSCustomObject]) {
159172
$properties = $InputObject.PSObject.Properties | Where-Object { $_.MemberType -eq 'NoteProperty' }
160173
if (-not $properties) {
161174
return '{}'

src/functions/public/Lua/ConvertTo-Lua.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [int] / [long] -> Lua integer
1717
- [float] / [double] -> Lua float
1818
- [bool] -> Lua boolean (true/false)
19-
- $null -> omitted (nil means absent in Lua)
19+
- $null -> top-level input serializes as nil; null-valued properties/keys are omitted
2020
2121
.EXAMPLE
2222
```powershell

tests/Lua.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,35 @@ Describe 'ConvertTo-Lua' {
973973
$result | Should -Be '{x=10}'
974974
}
975975
}
976+
977+
Context 'Non-finite float values' {
978+
It 'Throws on NaN double' {
979+
{ ConvertTo-Lua -InputObject ([double]::NaN) } | Should -Throw '*NaN*'
980+
}
981+
982+
It 'Throws on Infinity double' {
983+
{ ConvertTo-Lua -InputObject ([double]::PositiveInfinity) } | Should -Throw '*Infinity*'
984+
}
985+
986+
It 'Throws on negative Infinity double' {
987+
{ ConvertTo-Lua -InputObject ([double]::NegativeInfinity) } | Should -Throw '*Infinity*'
988+
}
989+
}
990+
991+
Context '.NET object string fallback' {
992+
It 'Serializes DateTime as string instead of empty table' {
993+
$result = ConvertTo-Lua -InputObject ([datetime]'2026-01-01') -Compress
994+
$result | Should -Not -Be '{}'
995+
$result | Should -Match '^".*"$'
996+
}
997+
998+
It 'Serializes Guid as string instead of empty table' {
999+
$guid = [guid]::NewGuid()
1000+
$result = ConvertTo-Lua -InputObject $guid -Compress
1001+
$result | Should -Not -Be '{}'
1002+
$result | Should -Match '^".*"$'
1003+
}
1004+
}
9761005
}
9771006

9781007
Describe 'Round-trip conversion' {

0 commit comments

Comments
 (0)