From b86c5aab52a9140e1614830ebacc6a934a03da81 Mon Sep 17 00:00:00 2001 From: Andrew Edwards Date: Sun, 2 Sep 2018 21:04:51 -0400 Subject: [PATCH] Optional properties --- Sources/Stripe/Models/Sources/Source.swift | 71 +++++++++++----------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/Sources/Stripe/Models/Sources/Source.swift b/Sources/Stripe/Models/Sources/Source.swift index b646867..cef36ff 100644 --- a/Sources/Stripe/Models/Sources/Source.swift +++ b/Sources/Stripe/Models/Sources/Source.swift @@ -25,14 +25,14 @@ public struct StripeSource: StripeModel { public var created: Date? public var currency: StripeCurrency? public var flow: Flow? - public var livemode: Bool + public var livemode: Bool? public var metadata: [String: String] public var owner: StripeOwner? public var receiver: StripeReceiver? public var redirect: SourceRedirect? public var statementDescriptor: String? public var status: StripeStatus? - public var type: SourceType + public var type: SourceType? public var usage: String? public var card: StripeBasicCard? public var threeDSecure: ThreeDSecure? @@ -55,7 +55,7 @@ public struct StripeSource: StripeModel { created = try container.decodeIfPresent(Date.self, forKey: .created) currency = try container.decodeIfPresent(StripeCurrency.self, forKey: .currency) flow = try container.decodeIfPresent(Flow.self, forKey: .flow) - livemode = try container.decode(Bool.self, forKey: .livemode) + livemode = try container.decodeIfPresent(Bool.self, forKey: .livemode) metadata = try container.decode([String: String].self, forKey: .metadata) owner = try container.decodeIfPresent(StripeOwner.self, forKey: .owner) receiver = try container.decodeIfPresent(StripeReceiver.self, forKey: .receiver) @@ -63,38 +63,39 @@ public struct StripeSource: StripeModel { statementDescriptor = try container.decodeIfPresent(String.self, forKey: .statementDescriptor) status = try container.decodeIfPresent(StripeStatus.self, forKey: .status) usage = try container.decodeIfPresent(String.self, forKey: .usage) - type = try container.decode(SourceType.self, forKey: .type) - - switch type { - case .card: - card = try container.decodeIfPresent(StripeBasicCard.self, forKey: .card) - - case .threeDSecure: - threeDSecure = try container.decodeIfPresent(ThreeDSecure.self, forKey: .threeDSecure) - - case .giropay: - giropay = try container.decodeIfPresent(Giropay.self, forKey: .giropay) - - case .sepaDebit: - sepaDebit = try container.decodeIfPresent(SepaDebit.self, forKey: .sepaDebit) - - case .ideal: - ideal = try container.decodeIfPresent(iDEAL.self, forKey: .ideal) - - case .sofort: - sofort = try container.decodeIfPresent(SOFORT.self, forKey: .sofort) - - case .bancontact: - bancontact = try container.decodeIfPresent(Bancontact.self, forKey: .bancontact) - - case .alipay: - alipay = try container.decodeIfPresent(Alipay.self, forKey: .alipay) - - case .p24: - p24 = try container.decodeIfPresent(P24.self, forKey: .p24) - - case .achCreditTransfer: - achCreditTransfer = try container.decodeIfPresent(ACHCreditTransfer.self, forKey: .achCreditTransfer) + type = try container.decodeIfPresent(SourceType.self, forKey: .type) + if let sourceType = type { + switch sourceType { + case .card: + card = try container.decodeIfPresent(StripeBasicCard.self, forKey: .card) + + case .threeDSecure: + threeDSecure = try container.decodeIfPresent(ThreeDSecure.self, forKey: .threeDSecure) + + case .giropay: + giropay = try container.decodeIfPresent(Giropay.self, forKey: .giropay) + + case .sepaDebit: + sepaDebit = try container.decodeIfPresent(SepaDebit.self, forKey: .sepaDebit) + + case .ideal: + ideal = try container.decodeIfPresent(iDEAL.self, forKey: .ideal) + + case .sofort: + sofort = try container.decodeIfPresent(SOFORT.self, forKey: .sofort) + + case .bancontact: + bancontact = try container.decodeIfPresent(Bancontact.self, forKey: .bancontact) + + case .alipay: + alipay = try container.decodeIfPresent(Alipay.self, forKey: .alipay) + + case .p24: + p24 = try container.decodeIfPresent(P24.self, forKey: .p24) + + case .achCreditTransfer: + achCreditTransfer = try container.decodeIfPresent(ACHCreditTransfer.self, forKey: .achCreditTransfer) + } } }