Skip to content

Commit 48b1d4c

Browse files
committed
Adds EntityID Mapper
1 parent cb71d27 commit 48b1d4c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
2670C3FE270F4E6A002FE931 /* sites-malformed.json in Resources */ = {isa = PBXBuildFile; fileRef = 2670C3FD270F4E6A002FE931 /* sites-malformed.json */; };
165165
267313312559CC930026F7EF /* PaymentGateway.swift in Sources */ = {isa = PBXBuildFile; fileRef = 267313302559CC930026F7EF /* PaymentGateway.swift */; };
166166
26731337255ACA850026F7EF /* PaymentGatewayListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26731336255ACA850026F7EF /* PaymentGatewayListMapper.swift */; };
167+
2676F4CE290AE6BB00C7A15B /* EntityIDMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2676F4CD290AE6BB00C7A15B /* EntityIDMapper.swift */; };
167168
2683D70E24456DB8002A1589 /* categories-empty.json in Resources */ = {isa = PBXBuildFile; fileRef = 2683D70D24456DB7002A1589 /* categories-empty.json */; };
168169
2683D71024456EE4002A1589 /* categories-extra.json in Resources */ = {isa = PBXBuildFile; fileRef = 2683D70F24456EE4002A1589 /* categories-extra.json */; };
169170
2685C0D2263B069500D9EE97 /* AddOnGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2685C0D1263B069500D9EE97 /* AddOnGroup.swift */; };
@@ -887,6 +888,7 @@
887888
2670C3FD270F4E6A002FE931 /* sites-malformed.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "sites-malformed.json"; sourceTree = "<group>"; };
888889
267313302559CC930026F7EF /* PaymentGateway.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentGateway.swift; sourceTree = "<group>"; };
889890
26731336255ACA850026F7EF /* PaymentGatewayListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentGatewayListMapper.swift; sourceTree = "<group>"; };
891+
2676F4CD290AE6BB00C7A15B /* EntityIDMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityIDMapper.swift; sourceTree = "<group>"; };
890892
2683D70D24456DB7002A1589 /* categories-empty.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "categories-empty.json"; sourceTree = "<group>"; };
891893
2683D70F24456EE4002A1589 /* categories-extra.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "categories-extra.json"; sourceTree = "<group>"; };
892894
2685C0D1263B069500D9EE97 /* AddOnGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddOnGroup.swift; sourceTree = "<group>"; };
@@ -2215,6 +2217,7 @@
22152217
DE2095BE279583A100171F1C /* CouponReportListMapper.swift */,
22162218
45150A9D26836A57006922EA /* CountryListMapper.swift */,
22172219
B524193E21AC5FE400D6FC0A /* DotcomDeviceMapper.swift */,
2220+
2676F4CD290AE6BB00C7A15B /* EntityIDMapper.swift */,
22182221
24F98C572502EA8800F49B68 /* FeatureFlagMapper.swift */,
22192222
DE50295C28C6068B00551736 /* JetpackUserMapper.swift */,
22202223
AEF9458A27297FF6001DCCFB /* IgnoringResponseMapper.swift */,
@@ -3240,6 +3243,7 @@
32403243
029BA53B255DFABD006171FD /* ShippingLabelPrintDataMapper.swift in Sources */,
32413244
0359EA0D27AAC5F80048DE2D /* WCPayChargeStatus.swift in Sources */,
32423245
CE430674234BA6AD0073CBFF /* RefundMapper.swift in Sources */,
3246+
2676F4CE290AE6BB00C7A15B /* EntityIDMapper.swift in Sources */,
32433247
CE0A0F1B223989670075ED8D /* ProductsRemote.swift in Sources */,
32443248
0359EA1527AAC7460048DE2D /* WCPayCardBrand.swift in Sources */,
32453249
2665032E261F4FBF0079A159 /* ProductAddOnOption.swift in Sources */,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Foundation
2+
3+
/// Mapper: Single Entity ID
4+
///
5+
struct EntityIDMapper: Mapper {
6+
7+
/// (Attempts) to convert an instance of Data into an into an ID
8+
///
9+
func map(response: Data) throws -> Int64 {
10+
let decoder = JSONDecoder()
11+
12+
return try decoder.decode(EntityIDEnvelope.self, from: response).id
13+
}
14+
}
15+
16+
/// Disposable Entity:
17+
/// Allows us to parse a product ID with JSONDecoder.
18+
///
19+
private struct EntityIDEnvelope: Decodable {
20+
private let data: [String: Int64]
21+
22+
// Extracts the entity ID from the underlying data
23+
var id: Int64 {
24+
data["id"] ?? .zero
25+
}
26+
27+
private enum CodingKeys: String, CodingKey {
28+
case data = "data"
29+
}
30+
}

0 commit comments

Comments
 (0)