-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDictionaryDecoder.swift
149 lines (114 loc) · 4.33 KB
/
DictionaryDecoder.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
147
148
149
//
// DictionaryDecoder.swift
// Copyright © 2018 NBC News Digital. All rights reserved.
//
import Foundation
typealias DictionaryType = [AnyHashable: Any]
struct DictionaryDecoder: Decoder {
var codingPath: [CodingKey] {
return []
}
var userInfo: [CodingUserInfoKey: Any] {
return [:]
}
private let dictionary: DictionaryType
init(_ dictionary: DictionaryType) {
self.dictionary = dictionary
}
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key: CodingKey {
return KeyedDecodingContainer(DictionaryContainer(dictionary))
}
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "no arrays"))
}
func singleValueContainer() throws -> SingleValueDecodingContainer {
throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: ""))
}
}
struct DictionaryContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
private let dictionary: DictionaryType
var codingPath: [CodingKey] {
return []
}
var allKeys: [K] {
return dictionary.keys
.compactMap({ $0 as? String })
.compactMap({ K(stringValue: $0) })
}
init(_ dictionary: DictionaryType) {
self.dictionary = dictionary
}
func contains(_ key: K) -> Bool {
return dictionary.keys.contains(key.stringValue)
}
func decodeNil(forKey key: K) throws -> Bool {
return false
}
func decode(_ type: Bool.Type, forKey key: K) throws -> Bool {
return try decode(forKey: key)
}
func decode(_ type: Int.Type, forKey key: K) throws -> Int {
return try decode(forKey: key)
}
func decode(_ type: Int8.Type, forKey key: K) throws -> Int8 {
return try decode(forKey: key)
}
func decode(_ type: Int16.Type, forKey key: K) throws -> Int16 {
return try decode(forKey: key)
}
func decode(_ type: Int32.Type, forKey key: K) throws -> Int32 {
return try decode(forKey: key)
}
func decode(_ type: Int64.Type, forKey key: K) throws -> Int64 {
return try decode(forKey: key)
}
func decode(_ type: UInt.Type, forKey key: K) throws -> UInt {
return try decode(forKey: key)
}
func decode(_ type: UInt8.Type, forKey key: K) throws -> UInt8 {
return try decode(forKey: key)
}
func decode(_ type: UInt16.Type, forKey key: K) throws -> UInt16 {
return try decode(forKey: key)
}
func decode(_ type: UInt32.Type, forKey key: K) throws -> UInt32 {
return try decode(forKey: key)
}
func decode(_ type: UInt64.Type, forKey key: K) throws -> UInt64 {
return try decode(forKey: key)
}
func decode(_ type: Float.Type, forKey key: K) throws -> Float {
return try decode(forKey: key)
}
func decode(_ type: Double.Type, forKey key: K) throws -> Double {
return try decode(forKey: key)
}
func decode(_ type: String.Type, forKey key: K) throws -> String {
return try decode(forKey: key)
}
func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T: Decodable {
return try decode(forKey: key)
}
private func decode<T>(forKey key: K) throws -> T where T: Decodable {
guard let val = dictionary[key.stringValue] else {
throw DecodingError.keyNotFound(key, .init(codingPath: [key], debugDescription: ""))
}
guard let tval = val as? T else {
throw DecodingError.typeMismatch(T.self, .init(codingPath: [key], debugDescription: ""))
}
return tval
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey {
throw DecodingError.valueNotFound(type, .init(codingPath: [key], debugDescription: "nested containers not supported"))
}
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
throw DecodingError.valueNotFound(type(of: self), .init(codingPath: [key], debugDescription: "nested containers not supported"))
}
func superDecoder() throws -> Decoder {
throw NSError()
}
func superDecoder(forKey key: K) throws -> Decoder {
throw NSError()
}
typealias Key = K
}