Skip to content

Commit

Permalink
NumberProtection 增加浮點數字串轉 Int 的問題
Browse files Browse the repository at this point in the history
  • Loading branch information
Darktt committed Feb 21, 2024
1 parent b6011ed commit a4bcf41
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
4 changes: 2 additions & 2 deletions JsonDecodeProtectionTests/DecimalTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class DecimalTest: XCTestCase
{
self.jsonString = """
{
"value": 0.001
"value": 122.999999999999999
}
"""

Expand All @@ -53,6 +53,6 @@ final class DecimalTest: XCTestCase
// Assert
let result: String = self.formatter.string(from: valueNumber)!

XCTAssertTrue(result == "0.001")
XCTAssertTrue(result == "122.999999999")
}
}
25 changes: 25 additions & 0 deletions JsonDecodeProtectionTests/NumberProtectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct NumberObject: Decodable
@NumberProtection
private(set)
var profit: Decimal?

@NumberProtection
private(set)
var amount: Int?
}

final class NumberProtectionTest: XCTestCase
Expand Down Expand Up @@ -76,4 +80,25 @@ final class NumberProtectionTest: XCTestCase

XCTAssertEqual(actual, expect)
}

func testNumberProtectionSuccessForFractionString() throws
{
// Arrange
self.jsonString = """
{
"amount": "122.999999999999999"
}
"""
let jsonData: Data = self.jsonString.data(using: .utf8)!
let jsonDecoder = JSONDecoder()

// Act
let object = try jsonDecoder.decode(NumberObject.self, from: jsonData)

// Assert
let actual: Int? = object.amount
let expect: Int = 122

XCTAssertEqual(actual, expect)
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

* File > Swift Packages > Add Package Dependency
* Add https://github.com/Darktt/JsonProtection
* Select "Up to Next Major" with "1.0.6"
* Select "Up to Next Major" with "1.0.7"

## 功能說明

Expand Down
44 changes: 44 additions & 0 deletions SourceCode/JsonProtection/NumberProtection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ extension Decimal: NumberType

extension Int: NumberType
{
public
init?(_ source: String)
{
guard let decimal = Decimal(source) else {

return nil
}

self.init(decimal)
}

public
init?(_ source: Decimal)
{
Expand All @@ -128,6 +139,17 @@ extension Int: NumberType

extension Int32: NumberType
{
public
init?(_ source: String)
{
guard let decimal = Decimal(source) else {

return nil
}

self.init(decimal)
}

public
init?(_ source: Decimal)
{
Expand All @@ -139,6 +161,17 @@ extension Int32: NumberType

extension UInt: NumberType
{
public
init?(_ source: String)
{
guard let decimal = Decimal(source) else {

return nil
}

self.init(decimal)
}

public
init?(_ source: Decimal)
{
Expand All @@ -150,6 +183,17 @@ extension UInt: NumberType

extension UInt32: NumberType
{
public
init?(_ source: String)
{
guard let decimal = Decimal(source) else {

return nil
}

self.init(decimal)
}

public
init?(_ source: Decimal)
{
Expand Down

0 comments on commit a4bcf41

Please sign in to comment.