|
| 1 | +// JSONAPIDecoder.swift |
| 2 | +// |
| 3 | +// Created by Pirush Prechathavanich on 4/4/18. |
| 4 | +// Copyright © 2018 Nimble. All rights reserved. |
| 5 | +// |
| 6 | + |
| 7 | +import Foundation |
| 8 | + |
| 9 | +public class JSONAPIDecoder: JSONDecoder { |
| 10 | + |
| 11 | + private typealias ResourceDictionary = [ResourceIdentifier: Resource] |
| 12 | + |
| 13 | + private let decoder: JSONEncoder |
| 14 | + |
| 15 | + init(decoder: JSONEncoder = JSONEncoder()) { |
| 16 | + self.decoder = decoder |
| 17 | + } |
| 18 | + |
| 19 | + public override func decode<T>(_ type: T.Type, from data: Data) throws -> T where T: Decodable { |
| 20 | + let jsonAPIObject = try super.decode(JSONAPIObject.self, from: data) |
| 21 | + |
| 22 | + let includedData = jsonAPIObject.included ?? [] |
| 23 | + let dictionary = includedDictionary(from: includedData) |
| 24 | + |
| 25 | + switch jsonAPIObject.type { |
| 26 | + case .data(let data): |
| 27 | + return try decode(data, including: dictionary, into: type) |
| 28 | + case .meta(let meta): |
| 29 | + return try decode(meta, into: type) |
| 30 | + case .errors(let errors): |
| 31 | + throw errors |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public func decodeWithMeta<Value: Decodable, Meta: Decodable>( |
| 36 | + value valueType: Value.Type, |
| 37 | + meta metaType: Meta.Type, |
| 38 | + from data: Data |
| 39 | + ) throws -> (value: Value, meta: Meta) { |
| 40 | + let jsonAPIObject = try super.decode(JSONAPIObject.self, from: data) |
| 41 | + |
| 42 | + let includedData = jsonAPIObject.included ?? [] |
| 43 | + let dictionary = includedDictionary(from: includedData) |
| 44 | + |
| 45 | + switch jsonAPIObject.type { |
| 46 | + case .data(let data): |
| 47 | + return ( |
| 48 | + value: try decode(data, including: dictionary, into: valueType), |
| 49 | + meta: try decode(jsonAPIObject.meta ?? .nil, into: metaType) |
| 50 | + ) |
| 51 | + case .meta(let meta): |
| 52 | + throw Errors.JSONAPIDecodingError.unableToDecode( |
| 53 | + reason: "No data field. Only contains meta: \(meta)" |
| 54 | + ) |
| 55 | + case .errors(let errors): |
| 56 | + throw errors |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +// MARK: - Private |
| 63 | + |
| 64 | +extension JSONAPIDecoder { |
| 65 | + |
| 66 | + private func decode<T: Decodable>(_ meta: JSON, into type: T.Type) throws -> T { |
| 67 | + let data = try decoder.encode(meta) |
| 68 | + return try super.decode(type, from: data) |
| 69 | + } |
| 70 | + |
| 71 | + private func decode<T: Decodable>(_ dataType: DataType<Resource>, |
| 72 | + including includedDictionary: ResourceDictionary, |
| 73 | + into type: T.Type) throws -> T { |
| 74 | + switch dataType { |
| 75 | + case .single(let resource): |
| 76 | + return try decode(resource, including: includedDictionary, into: type) |
| 77 | + case .collection(let resources): |
| 78 | + return try decodeCollection(of: resources, including: includedDictionary, into: type) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private func decode<T: Decodable>(_ resource: Resource, |
| 83 | + including includedDictionary: ResourceDictionary, |
| 84 | + into type: T.Type) throws -> T { |
| 85 | + let dictionary = try resolvedAttributes(of: resource, including: includedDictionary) |
| 86 | + let data = try decoder.encode(dictionary) |
| 87 | + return try super.decode(type, from: data) |
| 88 | + } |
| 89 | + |
| 90 | + private func decodeCollection<T: Decodable>(of resources: [Resource], |
| 91 | + including includedDictionary: ResourceDictionary, |
| 92 | + into type: T.Type) throws -> T { |
| 93 | + let collection = try resources.compactMap { try resolvedAttributes(of: $0, including: includedDictionary) } |
| 94 | + let data = try decoder.encode(collection) |
| 95 | + return try super.decode(type, from: data) |
| 96 | + } |
| 97 | + |
| 98 | + private func includedDictionary(from includedData: [Resource]) -> ResourceDictionary { |
| 99 | + return includedData.reduce(into: [:]) { dictionary, resource in |
| 100 | + let identifier = ResourceIdentifier(id: resource.id, type: resource.type) |
| 101 | + dictionary[identifier] = resource |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private func resolvedAttributes(of resource: Resource, |
| 106 | + including includedDictionary: ResourceDictionary) throws -> JSON? { |
| 107 | + var attributes = resource.attributes?.nested ?? [:] |
| 108 | + attributes[Resource.CodingKeys.id.rawValue] = .string(resource.id) |
| 109 | + attributes[Resource.CodingKeys.type.rawValue] = .string(resource.type) |
| 110 | + |
| 111 | + try resource.relationships?.forEach { key, relationship in |
| 112 | + guard let type = relationship.data else { return } |
| 113 | + switch type { |
| 114 | + case .single(let identifier): |
| 115 | + let includedResource = try getResource(from: includedDictionary, for: identifier) |
| 116 | + attributes[key] = try resolvedAttributes(of: includedResource, including: includedDictionary) |
| 117 | + |
| 118 | + case .collection(let identifiers): |
| 119 | + let includedAttributes = try identifiers |
| 120 | + .map { try getResource(from: includedDictionary, for: $0) } |
| 121 | + .compactMap { try resolvedAttributes(of: $0, including: includedDictionary) } |
| 122 | + attributes[key] = .array(includedAttributes) |
| 123 | + } |
| 124 | + } |
| 125 | + return .nested(attributes) |
| 126 | + } |
| 127 | + |
| 128 | + private func getResource(from includedDictionary: ResourceDictionary, |
| 129 | + for identifier: ResourceIdentifier) throws -> Resource { |
| 130 | + guard let resource = includedDictionary[identifier] else { |
| 131 | + throw Errors.JSONAPIDecodingError.resourceNotFound(identifier: identifier) |
| 132 | + } |
| 133 | + return resource |
| 134 | + } |
| 135 | +} |
0 commit comments