this repo has no description
at main 158 lines 5.2 kB view raw
1// 2// StringlyKeyedDecodingContainerWrapper.swift 3// URLQueryItemCoder 4// 5// Created by Kyle Hughes on 2/23/23. 6// 7 8internal struct StringlyKeyedDecodingContainerWrapper<Key, Base> where 9 Key: CodingKey, 10 Base: KeyedDecodingContainerProtocol, 11 Base.Key == StringCodingKey 12{ 13 internal let base: Base 14 15 // MARK: Internal Initialization 16 17 internal init(_ base: Base) { 18 self.base = base 19 } 20} 21 22// MARK: - KeyedDecodingContainerProtocol Extension 23 24extension StringlyKeyedDecodingContainerWrapper: KeyedDecodingContainerProtocol { 25 // MARK: Internal Instance Interface 26 27 @inlinable 28 internal var allKeys: [Key] { 29 base 30 .allKeys 31 .lazy 32 .map(\.stringValue) 33 .compactMap(Key.init) 34 } 35 36 @inlinable 37 internal var codingPath: [any CodingKey] { 38 base.codingPath 39 } 40 41 @inlinable 42 internal func contains(_ key: Key) -> Bool { 43 base.contains(StringCodingKey(stringValue: key.stringValue)) 44 } 45 46 @inlinable 47 internal func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { 48 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 49 } 50 51 @inlinable 52 internal func decode(_ type: String.Type, forKey key: Key) throws -> String { 53 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 54 } 55 56 @inlinable 57 internal func decode(_ type: Double.Type, forKey key: Key) throws -> Double { 58 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 59 } 60 61 @inlinable 62 internal func decode(_ type: Float.Type, forKey key: Key) throws -> Float { 63 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 64 } 65 66 @inlinable 67 internal func decode(_ type: Int.Type, forKey key: Key) throws -> Int { 68 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 69 } 70 71 @inlinable 72 internal func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { 73 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 74 } 75 76 @inlinable 77 internal func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { 78 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 79 } 80 81 @inlinable 82 internal func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { 83 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 84 } 85 86 @inlinable 87 internal func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { 88 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 89 } 90 91 @inlinable 92 internal func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { 93 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 94 } 95 96 @inlinable 97 internal func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { 98 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 99 } 100 101 @inlinable 102 internal func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { 103 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 104 } 105 106 @inlinable 107 internal func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { 108 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 109 } 110 111 @inlinable 112 internal func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { 113 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 114 } 115 116 @inlinable 117 internal func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable { 118 try base.decode(type, forKey: StringCodingKey(stringValue: key.stringValue)) 119 } 120 121 @inlinable 122 internal func decodeNil(forKey key: Key) throws -> Bool { 123 try base.decodeNil(forKey: StringCodingKey(stringValue: key.stringValue)) 124 } 125 126 @inlinable 127 internal func nestedContainer<NestedKey>( 128 keyedBy type: NestedKey.Type, 129 forKey key: Key 130 ) throws -> KeyedDecodingContainer<NestedKey> where NestedKey: CodingKey { 131 try base.nestedContainer(keyedBy: type, forKey: StringCodingKey(stringValue: key.stringValue)) 132 } 133 134 @inlinable 135 internal func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer { 136 try base.nestedUnkeyedContainer(forKey: StringCodingKey(stringValue: key.stringValue)) 137 } 138 139 @inlinable 140 internal func superDecoder() throws -> Decoder { 141 try base.superDecoder() 142 } 143 144 @inlinable 145 internal func superDecoder(forKey key: Key) throws -> Decoder { 146 try base.superDecoder(forKey: StringCodingKey(stringValue: key.stringValue)) 147 } 148} 149 150// MARK: - Extension for Stringly-Keyed Decoding Containers 151 152extension KeyedDecodingContainerProtocol where Key == StringCodingKey { 153 // MARK: Internal Instance Interface 154 155 internal func wrapped<Key>() -> StringlyKeyedDecodingContainerWrapper<Key, Self> { 156 StringlyKeyedDecodingContainerWrapper(self) 157 } 158}