Skip to content

Commit 01cba92

Browse files
Merge pull request #146 from gabriel-samfira/fix-int-parsing
Switch from "Any" number style when parsing number
2 parents bd9c4f3 + 2b12502 commit 01cba92

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

Tests/powershell-yaml.Tests.ps1

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ wishlist:
203203
- product : A Cool Book.
204204
quantity : 1
205205
description : I love that Cool Book.
206+
aStringTatLooksLikeAFloat: 55,34
207+
aStringThatLooksLikeAnInt: 2018+
208+
scientificNotationInt: 1e+3
209+
scientificNotationBigInt: 1e+40
210+
intWithTag: !!int "42"
211+
scientificNotationIntWithTag: !!int "1e+3"
206212
price : 55.34
207213
total: 4443.52
208214
int64: $([int64]::MaxValue)
@@ -244,7 +250,13 @@ bools:
244250
product = "A Cool Book.";
245251
quantity = 1;
246252
description = "I love that Cool Book.";
247-
price = 55.34
253+
price = 55.34;
254+
aStringTatLooksLikeAFloat = "55,34"
255+
aStringThatLooksLikeAnInt = "2018+"
256+
scientificNotationInt = [int32]1000
257+
scientificNotationBigInt = [System.Numerics.BigInteger]::Parse("10000000000000000000000000000000000000000")
258+
intWithTag = 42
259+
scientificNotationIntWithTag = 1000
248260
}
249261
);
250262
total = 4443.52;
@@ -289,6 +301,18 @@ bools:
289301
$product['quantity'] | Should -Be $expectedProduct['quantity']
290302
$product['description'] | Should -Be $expectedProduct['description']
291303
$product['price'] | Should -Be $expectedProduct['price']
304+
$product['aStringTatLooksLikeAFloat'] | Should -Be $expectedProduct['aStringTatLooksLikeAFloat']
305+
$product['aStringTatLooksLikeAFloat'] | Should -BeOfType ([string])
306+
$product['aStringThatLooksLikeAnInt'] | Should -Be $expectedProduct['aStringThatLooksLikeAnInt']
307+
$product['aStringThatLooksLikeAnInt'] | Should -BeOfType ([string])
308+
$product['scientificNotationInt'] | Should -Be $expectedProduct['scientificNotationInt']
309+
$product['scientificNotationInt'] | Should -BeOfType ([int32])
310+
$product['scientificNotationBigInt'] | Should -Be $expectedProduct['scientificNotationBigInt']
311+
$product['scientificNotationBigInt'] | Should -BeOfType ([System.Numerics.BigInteger])
312+
$product['intWithTag'] | Should -Be $expectedProduct['intWithTag']
313+
$product['intWithTag'] | Should -BeOfType ([int32])
314+
$product['scientificNotationIntWithTag'] | Should -Be $expectedProduct['scientificNotationIntWithTag']
315+
$product['scientificNotationIntWithTag'] | Should -BeOfType ([int32])
292316
$res['total'] | Should -Be $expected['total']
293317
$res['note'] | Should -Be $expected['note']
294318

powershell-yaml.psm1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ function Convert-ValueToProperType {
147147
$parsedValue = [Convert]::ToInt64($Node.Value.Substring(2), 16)
148148
}
149149
default {
150-
if (![System.Numerics.BigInteger]::TryParse($Node.Value, [Globalization.NumberStyles]::Any, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
150+
if (![System.Numerics.BigInteger]::TryParse($Node.Value, @([Globalization.NumberStyles]::Float, [Globalization.NumberStyles]::Integer), [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
151151
Throw ("failed to parse scalar {0} as long" -f $Node)
152152
}
153153
}
154154
}
155155
} else {
156-
if (![System.Numerics.BigInteger]::TryParse($Node.Value, [Globalization.NumberStyles]::Any, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
156+
if (![System.Numerics.BigInteger]::TryParse($Node.Value, @([Globalization.NumberStyles]::Float, [Globalization.NumberStyles]::Integer), [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
157157
Throw ("failed to parse scalar {0} as long" -f $Node)
158158
}
159159
}
@@ -179,7 +179,7 @@ function Convert-ValueToProperType {
179179
}
180180
}
181181
}
182-
if (![double]::TryParse($Node.Value, [Globalization.NumberStyles]::Any, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
182+
if (![double]::TryParse($Node.Value, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)) {
183183
Throw ("failed to parse scalar {0} as double" -f $Node)
184184
}
185185
return $parsedValue
@@ -205,7 +205,7 @@ function Convert-ValueToProperType {
205205
}
206206

207207
$parsedValue = New-Object -TypeName ([System.Numerics.BigInteger].FullName)
208-
$result = [System.Numerics.BigInteger]::TryParse($Node, [Globalization.NumberStyles]::Any, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)
208+
$result = [System.Numerics.BigInteger]::TryParse($Node, @([Globalization.NumberStyles]::Float, [Globalization.NumberStyles]::Integer), [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)
209209
if($result) {
210210
$types = @([int], [long])
211211
foreach($i in $types){
@@ -216,11 +216,10 @@ function Convert-ValueToProperType {
216216
}
217217
return $parsedValue
218218
}
219-
220219
$types = @([double], [decimal])
221220
foreach($i in $types){
222221
$parsedValue = New-Object -TypeName $i.FullName
223-
$result = $i::TryParse($Node, [Globalization.NumberStyles]::Any, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)
222+
$result = $i::TryParse($Node, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)
224223
if( $result ) {
225224
return $parsedValue
226225
}

0 commit comments

Comments
 (0)