Skip to content

Commit 991ba4a

Browse files
committed
Make PassJSON and OrderJSON Codable
1 parent e0848c5 commit 991ba4a

File tree

4 files changed

+56
-91
lines changed

4 files changed

+56
-91
lines changed

Sources/Orders/OrderJSON.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public enum OrderJSON {
33
/// A protocol that defines the structure of a `order.json` file.
44
///
55
/// > Tip: See the [`Order`](https://developer.apple.com/documentation/walletorders/order) object to understand the keys.
6-
public protocol Properties: Encodable {
6+
public protocol Properties: Codable {
77
/// The date and time when the customer created the order, in RFC 3339 format.
88
var createdAt: String { get }
99

@@ -48,7 +48,7 @@ extension OrderJSON {
4848
/// A protocol that represents the merchant associated with the order.
4949
///
5050
/// > Tip: See the [`Order.Merchant`](https://developer.apple.com/documentation/walletorders/merchant) object to understand the keys.
51-
public protocol Merchant: Encodable {
51+
public protocol Merchant: Codable {
5252
/// The localized display name of the merchant.
5353
var displayName: String { get }
5454

@@ -64,7 +64,7 @@ extension OrderJSON {
6464
/// A protocol that represents the details of a barcode for an order.
6565
///
6666
/// > Tip: See the [`Order.Barcode`](https://developer.apple.com/documentation/walletorders/barcode) object to understand the keys.
67-
public protocol Barcode: Encodable {
67+
public protocol Barcode: Codable {
6868
/// The format of the barcode.
6969
var format: BarcodeFormat { get }
7070

@@ -80,24 +80,24 @@ extension OrderJSON {
8080

8181
extension OrderJSON {
8282
/// The type of order this bundle represents.
83-
public enum OrderType: String, Encodable {
83+
public enum OrderType: String, Codable {
8484
case ecommerce
8585
}
8686

8787
/// A high-level status of the order, used for display purposes.
88-
public enum OrderStatus: String, Encodable {
88+
public enum OrderStatus: String, Codable {
8989
case completed
9090
case cancelled
9191
case open
9292
}
9393

9494
/// The version of the schema used for the order.
95-
public enum SchemaVersion: Int, Encodable {
95+
public enum SchemaVersion: Int, Codable {
9696
case v1 = 1
9797
}
9898

9999
/// The format of the barcode.
100-
public enum BarcodeFormat: String, Encodable {
100+
public enum BarcodeFormat: String, Codable {
101101
case pdf417
102102
case qr
103103
case aztec

Sources/Passes/PassJSON.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public enum PassJSON {
33
/// A protocol that defines the structure of a `pass.json` file.
44
///
55
/// > Tip: See the [`Pass`](https://developer.apple.com/documentation/walletpasses/pass) object to understand the keys.
6-
public protocol Properties: Encodable {
6+
public protocol Properties: Codable {
77
/// A short description that iOS accessibility technologies use for a pass.
88
var description: String { get }
99

@@ -34,7 +34,7 @@ extension PassJSON {
3434
/// A protocol that represents the information to display in a field on a pass.
3535
///
3636
/// > Tip: See the [`PassFieldContent`](https://developer.apple.com/documentation/walletpasses/passfieldcontent) object to understand the keys.
37-
public protocol PassFieldContent: Encodable {
37+
public protocol PassFieldContent: Codable {
3838
/// A unique key that identifies a field in the pass; for example, `departure-gate`.
3939
var key: String { get }
4040

@@ -49,7 +49,7 @@ extension PassJSON {
4949
/// A protocol that represents the groups of fields that display the information for a boarding pass.
5050
///
5151
/// > Tip: See the [`Pass.BoardingPass`](https://developer.apple.com/documentation/walletpasses/pass/boardingpass) object to understand the keys.
52-
public protocol BoardingPass: Encodable {
52+
public protocol BoardingPass: Codable {
5353
/// The type of transit for a boarding pass.
5454
///
5555
/// This key is invalid for other types of passes.
@@ -64,7 +64,7 @@ extension PassJSON {
6464
/// A protocol that represents a barcode on a pass.
6565
///
6666
/// > Tip: See the [`Pass.Barcodes`](https://developer.apple.com/documentation/walletpasses/pass/barcodes) object to understand the keys.
67-
public protocol Barcodes: Encodable {
67+
public protocol Barcodes: Codable {
6868
/// The format of the barcode.
6969
///
7070
/// The barcode format `PKBarcodeFormatCode128` isn’t supported for watchOS.
@@ -83,7 +83,7 @@ extension PassJSON {
8383
/// A protocol that represents a location that the system uses to show a relevant pass.
8484
///
8585
/// > Tip: See the [`Pass.Locations`](https://developer.apple.com/documentation/walletpasses/pass/locations) object to understand the keys.
86-
public protocol Locations: Encodable {
86+
public protocol Locations: Codable {
8787
/// The latitude, in degrees, of the location.
8888
var latitude: Double { get }
8989

@@ -96,7 +96,7 @@ extension PassJSON {
9696
/// An object that represents the near-field communication (NFC) payload the device passes to an Apple Pay terminal.
9797
///
9898
/// > Tip: See the [`Pass.NFC`](https://developer.apple.com/documentation/walletpasses/pass/nfc) object to understand the keys.
99-
public protocol NFC: Encodable {
99+
public protocol NFC: Codable {
100100
/// The payload the device transmits to the Apple Pay terminal.
101101
///
102102
/// The size must be no more than 64 bytes.
@@ -112,13 +112,13 @@ extension PassJSON {
112112

113113
extension PassJSON {
114114
/// The version of the file format.
115-
public enum FormatVersion: Int, Encodable {
115+
public enum FormatVersion: Int, Codable {
116116
/// The value must be `1`.
117117
case v1 = 1
118118
}
119119

120120
/// The type of transit for a boarding pass.
121-
public enum TransitType: String, Encodable {
121+
public enum TransitType: String, Codable {
122122
case air = "PKTransitTypeAir"
123123
case boat = "PKTransitTypeBoat"
124124
case bus = "PKTransitTypeBus"
@@ -127,7 +127,7 @@ extension PassJSON {
127127
}
128128

129129
/// The format of the barcode.
130-
public enum BarcodeFormat: String, Encodable {
130+
public enum BarcodeFormat: String, Codable {
131131
case pdf417 = "PKBarcodeFormatPDF417"
132132
case qr = "PKBarcodeFormatQR"
133133
case aztec = "PKBarcodeFormatAztec"

Tests/OrdersTests/TestOrder.swift

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
import Foundation
22
import Orders
33

4-
extension OrderJSON.SchemaVersion: Decodable {}
5-
extension OrderJSON.OrderType: Decodable {}
6-
extension OrderJSON.OrderStatus: Decodable {}
4+
struct TestOrder: OrderJSON.Properties {
5+
var schemaVersion = OrderJSON.SchemaVersion.v1
6+
var orderTypeIdentifier = "order.com.example.swift-wallet"
7+
var orderIdentifier = UUID().uuidString
8+
var orderType = OrderJSON.OrderType.ecommerce
9+
var orderNumber = "HM090772020864"
10+
var createdAt = Date.now.ISO8601Format()
11+
var updatedAt = Date.now.ISO8601Format()
12+
var status = OrderJSON.OrderStatus.open
13+
var merchant = MerchantData()
14+
var orderManagementURL = "https://www.example.com/"
15+
var authenticationToken = UUID().uuidString
16+
var webServiceURL = "https://www.example.com/api/orders/"
717

8-
struct TestOrder: OrderJSON.Properties, Decodable {
9-
let schemaVersion = OrderJSON.SchemaVersion.v1
10-
let orderTypeIdentifier = "order.com.example.swift-wallet"
11-
let orderIdentifier = UUID().uuidString
12-
let orderType = OrderJSON.OrderType.ecommerce
13-
let orderNumber = "HM090772020864"
14-
let createdAt = Date.now.ISO8601Format()
15-
let updatedAt = Date.now.ISO8601Format()
16-
let status = OrderJSON.OrderStatus.open
17-
let merchant = MerchantData()
18-
let orderManagementURL = "https://www.example.com/"
19-
let authenticationToken = UUID().uuidString
20-
21-
private let webServiceURL = "https://www.example.com/api/orders/"
22-
23-
enum CodingKeys: String, CodingKey {
24-
case schemaVersion
25-
case orderTypeIdentifier, orderIdentifier, orderType, orderNumber
26-
case createdAt, updatedAt
27-
case status, merchant
28-
case orderManagementURL, authenticationToken, webServiceURL
29-
}
30-
31-
struct MerchantData: OrderJSON.Merchant, Decodable {
32-
let merchantIdentifier = "com.example.pet-store"
33-
let displayName = "Pet Store"
34-
let url = "https://www.example.com/"
35-
let logo = "pet_store_logo.png"
36-
37-
enum CodingKeys: String, CodingKey {
38-
case merchantIdentifier, displayName, url, logo
39-
}
18+
struct MerchantData: OrderJSON.Merchant {
19+
var merchantIdentifier = "com.example.pet-store"
20+
var displayName = "Pet Store"
21+
var url = "https://www.example.com/"
22+
var logo = "pet_store_logo.png"
4023
}
4124
}

Tests/PassesTests/TestPass.swift

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
import Foundation
22
import Passes
33

4-
extension PassJSON.FormatVersion: Decodable {}
5-
extension PassJSON.BarcodeFormat: Decodable {}
6-
extension PassJSON.TransitType: Decodable {}
7-
8-
struct TestPass: PassJSON.Properties, Decodable {
9-
let description = "Test Pass"
10-
let formatVersion = PassJSON.FormatVersion.v1
11-
let organizationName = "example"
12-
let passTypeIdentifier = "pass.com.example.swift-wallet"
13-
let serialNumber = UUID().uuidString
14-
let teamIdentifier = "K6512ZA2S5"
15-
16-
private let webServiceURL = "https://www.example.com/api/passes/"
17-
let authenticationToken = UUID().uuidString
18-
private let logoText = "Vapor Community"
19-
private let sharingProhibited = true
20-
let backgroundColor = "rgb(207, 77, 243)"
21-
let foregroundColor = "rgb(255, 255, 255)"
22-
23-
let barcodes = Barcode(message: "test")
24-
struct Barcode: PassJSON.Barcodes, Decodable {
25-
let format = PassJSON.BarcodeFormat.qr
26-
let message: String
27-
let messageEncoding = "iso-8859-1"
28-
29-
enum CodingKeys: String, CodingKey {
30-
case format, message, messageEncoding
31-
}
4+
struct TestPass: PassJSON.Properties {
5+
var description = "Test Pass"
6+
var formatVersion = PassJSON.FormatVersion.v1
7+
var organizationName = "example"
8+
var passTypeIdentifier = "pass.com.example.swift-wallet"
9+
var serialNumber = UUID().uuidString
10+
var teamIdentifier = "K6512ZA2S5"
11+
var webServiceURL = "https://www.example.com/api/passes/"
12+
var authenticationToken = UUID().uuidString
13+
var logoText = "Vapor Community"
14+
var sharingProhibited = true
15+
var backgroundColor = "rgb(207, 77, 243)"
16+
var foregroundColor = "rgb(255, 255, 255)"
17+
18+
var barcodes = Barcode(message: "test")
19+
struct Barcode: PassJSON.Barcodes {
20+
var format = PassJSON.BarcodeFormat.qr
21+
var message: String
22+
var messageEncoding = "iso-8859-1"
3223
}
3324

34-
let boardingPass = Boarding(transitType: .air)
35-
struct Boarding: PassJSON.BoardingPass, Decodable {
25+
var boardingPass = Boarding(transitType: .air)
26+
struct Boarding: PassJSON.BoardingPass {
3627
let transitType: PassJSON.TransitType
3728
let headerFields: [PassField]
3829
let primaryFields: [PassField]
3930
let secondaryFields: [PassField]
4031
let auxiliaryFields: [PassField]
4132
let backFields: [PassField]
4233

43-
struct PassField: PassJSON.PassFieldContent, Decodable {
34+
struct PassField: PassJSON.PassFieldContent {
4435
let key: String
4536
let label: String
4637
let value: String
@@ -55,13 +46,4 @@ struct TestPass: PassJSON.Properties, Decodable {
5546
self.transitType = transitType
5647
}
5748
}
58-
59-
enum CodingKeys: String, CodingKey {
60-
case description
61-
case formatVersion
62-
case organizationName, passTypeIdentifier, serialNumber, teamIdentifier
63-
case webServiceURL, authenticationToken
64-
case logoText, sharingProhibited, backgroundColor, foregroundColor
65-
case barcodes, boardingPass
66-
}
6749
}

0 commit comments

Comments
 (0)