Skip to content

Commit

Permalink
Refactor JSON codable code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Solovev committed Feb 14, 2024
1 parent e08e502 commit 10d3d9d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import Foundation

extension Capability.PaymentMethod {
/// Represents Capability.PaymentMethod.Bank JSON object for communication with Omise API
public struct Bank: Codable, Hashable {
public struct Bank: Hashable {
public let name: String
public let code: String
public let isActive: Bool
}
}

enum CodingKeys: String, CodingKey {
case name
case code
case isActive = "active"
}
extension Capability.PaymentMethod.Bank: Codable {
enum CodingKeys: String, CodingKey {
case name
case code
case isActive = "active"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import Foundation

extension Capability.PaymentMethod {
/// Represents Capability.PaymentMethod.Provider JSON object for communication with Omise API
public enum Provider: String, Codable, Hashable {
public enum Provider: String, Hashable {
case alipayPlus = "Alipay_plus"
case rms = "RMS"
case unknown
}
}

public init(from decoder: Decoder) throws {
self = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? .unknown
}
extension Capability.PaymentMethod.Provider: Codable {
public init(from decoder: Decoder) throws {
self = try Self(rawValue: decoder.singleValueContainer().decode(RawValue.self)) ?? .unknown
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

extension Capability {
/// Represents Capability.PaymentMethod JSON object for communication with Omise API
public struct PaymentMethod: Codable, Equatable {
public struct PaymentMethod: Equatable {
public let name: String
public let currencies: Set<String>
public let cardBrands: Set<String>?
Expand All @@ -19,17 +19,19 @@ extension Capability {
self.provider = provider
}

private enum CodingKeys: String, CodingKey {
case name
case currencies
case installmentTerms = "installment_terms"
case cardBrands = "card_brands"
case banks
case provider
}

var isCardType: Bool {
name == "card"
}
}
}

extension Capability.PaymentMethod: Codable {
private enum CodingKeys: String, CodingKey {
case name
case currencies
case installmentTerms = "installment_terms"
case cardBrands = "card_brands"
case banks
case provider
}
}

0 comments on commit 10d3d9d

Please sign in to comment.