-
Notifications
You must be signed in to change notification settings - Fork 5
/
Decodable.swift
146 lines (100 loc) · 3.19 KB
/
Decodable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Created by Vladimir Burdukov on 27/10/17.
// Copyright © 2017 NetcoSports. All rights reserved.
//
extension CodingUserInfoKey {
static let xpath = CodingUserInfoKey(rawValue: "Gnomon.XPath")!
}
private struct _XPathKey: CodingKey {
let components: [String]
init(key: String) {
self.init(with: key.components(separatedBy: "/"))
}
init(with components: [String]) {
self.components = components
}
var next: _XPathKey {
var components = self.components
_ = components.removeFirst()
return _XPathKey(with: components)
}
var stringValue: String {
return components[0]
}
init?(stringValue: String) {
self.init(key: stringValue)
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
private extension Decoder {
func decoder(by xpath: String?) throws -> Decoder {
guard let xpath = xpath else { return self }
var container = try self.container(keyedBy: _XPathKey.self)
var key = _XPathKey(key: xpath)
while key.components.count > 1 {
container = try container.nestedContainer(keyedBy: _XPathKey.self, forKey: key)
key = key.next
}
return try container.superDecoder(forKey: key)
}
}
private struct _Single<T: Decodable & BaseModel>: Decodable {
let model: T
init(from decoder: Decoder) throws {
let decoder = try decoder.decoder(by: decoder.userInfo[.xpath] as? String)
model = try T(from: decoder)
}
}
private struct _Multiple<T: Decodable & BaseModel>: Decodable {
let models: [T]
init(from decoder: Decoder) throws {
let decoder = try decoder.decoder(by: decoder.userInfo[.xpath] as? String)
var unkeyed = try decoder.unkeyedContainer()
var models: [T] = []
while !unkeyed.isAtEnd {
models.append(try T(from: unkeyed.superDecoder()))
}
self.models = models
}
}
private struct _MultipleOptional<T: Decodable & BaseModel>: Decodable {
let models: [T?]
init(from decoder: Decoder) throws {
let decoder = try decoder.decoder(by: decoder.userInfo[.xpath] as? String)
var unkeyed = try decoder.unkeyedContainer()
var models: [T?] = []
while !unkeyed.isAtEnd {
models.append(try? T(from: unkeyed.superDecoder()))
}
self.models = models
}
}
public protocol DecodableModel: BaseModel, Decodable {
static var decoder: JSONDecoder { get }
}
public extension DecodableModel {
static var decoder: JSONDecoder { return JSONDecoder() }
}
public extension DecodableModel {
static func model(with data: Data, atPath path: String?) throws -> Self {
let decoder = Self.decoder
decoder.userInfo[.xpath] = path
let container = try decoder.decode(_Single<Self>.self, from: data)
return container.model
}
static func models(with data: Data, atPath path: String?) throws -> [Self] {
let decoder = Self.decoder
decoder.userInfo[.xpath] = path
let container = try decoder.decode(_Multiple<Self>.self, from: data)
return container.models
}
static func optionalModels(with data: Data, atPath path: String?) throws -> [Self?] {
let decoder = Self.decoder
decoder.userInfo[.xpath] = path
let container = try decoder.decode(_MultipleOptional<Self>.self, from: data)
return container.models
}
}