Skip to content

Commit

Permalink
Merge pull request #8 from skelpo/develop
Browse files Browse the repository at this point in the history
Decode String or Int for Order.Item.quantity and Payment.Item.quantity Properties
  • Loading branch information
calebkleveter authored Jan 23, 2019
2 parents 7db8317 + ac33585 commit 0680ad5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
18 changes: 13 additions & 5 deletions Sources/PayPal/Models/Order/OrderItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,23 @@ extension Order {
public init(from decoder: Decoder)throws {
let container = try decoder.container(keyedBy: Order.Item.CodingKeys.self)

guard let quantityValue = try Int(container.decode(String.self, forKey: .quantity)) else {
throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "Cannot get int from given string")
let quantity: Int
do {
quantity = try container.decode(Int.self, forKey: .quantity)
} catch let error as DecodingError {
guard case DecodingError.typeMismatch(_, _) = error else { throw error }
guard let value = try Int(container.decode(String.self, forKey: .quantity)) else {
throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "Cannot get int from given string")
}
quantity = value
}
guard let priceValue = try Decimal(string: container.decode(String.self, forKey: .price)) else {

guard let price = try Decimal(string: container.decode(String.self, forKey: .price)) else {
throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "Cannot get decimal from given string")
}

self.price = try Failable<Decimal, TenDigits<Decimal>>(priceValue)
self.quantity = try Failable<Int, TenDigits<Int>>(quantityValue)
self.price = try price.failable()
self.quantity = try quantity.failable()
self.sku = try container.decode(Optional127String.self, forKey: .sku)
self.name = try container.decode(Optional127String.self, forKey: .name)
self.description = try container.decode(Optional127String.self, forKey: .description)
Expand Down
18 changes: 12 additions & 6 deletions Sources/PayPal/Models/Payment/Items/PaymentItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ extension Payment {
public init(from decoder: Decoder)throws {
let container = try decoder.container(keyedBy: Payment.Item.CodingKeys.self)

let strInt = try container.decode(String.self, forKey: .quantity)
let strDec = try container.decode(String.self, forKey: .price)
guard let quantity = Int(strInt) else {
throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "String not convertible to int")
let quantity: Int
do {
quantity = try container.decode(Int.self, forKey: .quantity)
} catch let error as DecodingError {
guard case DecodingError.typeMismatch(_, _) = error else { throw error }
guard let value = try Int(container.decode(String.self, forKey: .quantity)) else {
throw DecodingError.dataCorruptedError(forKey: .quantity, in: container, debugDescription: "Given string not convertible to int")
}
quantity = value
}
guard let price = Decimal(string: strDec) else {
throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "String not convertible to decimal")

guard let price = try Decimal(string: container.decode(String.self, forKey: .price)) else {
throw DecodingError.dataCorruptedError(forKey: .price, in: container, debugDescription: "Given string not convertible to decimal")
}

self.quantity = try quantity.failable()
Expand Down

0 comments on commit 0680ad5

Please sign in to comment.