Skip to content

Commit f841d7e

Browse files
authored
Rename custom CodingKey types (#311)
1 parent cec9eaf commit f841d7e

File tree

8 files changed

+49
-44
lines changed

8 files changed

+49
-44
lines changed

Sources/JBirdCodableSupport/Decoder/ArrayDecoder.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
5757
func decodeNil() throws -> Bool {
5858
guard currentIndex < (count ?? 0) else {
5959
let context = DecodingError.Context(
60-
codingPath: codingPath + [IndexKey(currentIndex)],
60+
codingPath: codingPath + [IndexCodingKey(currentIndex)],
6161
debugDescription: "Unkeyed container is at end."
6262
)
6363
throw DecodingError.valueNotFound(Any?.self, context)
@@ -81,7 +81,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
8181
return try value.convert()
8282
} catch {
8383
let context = DecodingError.Context(
84-
codingPath: codingPath + [IndexKey(index)],
84+
codingPath: codingPath + [IndexCodingKey(index)],
8585
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
8686
underlyingError: error
8787
)
@@ -100,7 +100,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
100100
return try value.convert()
101101
} catch {
102102
let context = DecodingError.Context(
103-
codingPath: codingPath + [IndexKey(index)],
103+
codingPath: codingPath + [IndexCodingKey(index)],
104104
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
105105
underlyingError: error
106106
)
@@ -119,7 +119,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
119119
return try value.convert()
120120
} catch {
121121
let context = DecodingError.Context(
122-
codingPath: codingPath + [IndexKey(index)],
122+
codingPath: codingPath + [IndexCodingKey(index)],
123123
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
124124
underlyingError: error
125125
)
@@ -138,7 +138,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
138138
return try value.convert()
139139
} catch {
140140
let context = DecodingError.Context(
141-
codingPath: codingPath + [IndexKey(index)],
141+
codingPath: codingPath + [IndexCodingKey(index)],
142142
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
143143
underlyingError: error
144144
)
@@ -157,7 +157,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
157157
return try value.convert()
158158
} catch {
159159
let context = DecodingError.Context(
160-
codingPath: codingPath + [IndexKey(index)],
160+
codingPath: codingPath + [IndexCodingKey(index)],
161161
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
162162
underlyingError: error
163163
)
@@ -176,7 +176,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
176176
return try value.convert()
177177
} catch {
178178
let context = DecodingError.Context(
179-
codingPath: codingPath + [IndexKey(index)],
179+
codingPath: codingPath + [IndexCodingKey(index)],
180180
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
181181
underlyingError: error
182182
)
@@ -195,7 +195,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
195195
return try value.convert()
196196
} catch {
197197
let context = DecodingError.Context(
198-
codingPath: codingPath + [IndexKey(index)],
198+
codingPath: codingPath + [IndexCodingKey(index)],
199199
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
200200
underlyingError: error
201201
)
@@ -214,7 +214,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
214214
return try value.convert()
215215
} catch {
216216
let context = DecodingError.Context(
217-
codingPath: codingPath + [IndexKey(index)],
217+
codingPath: codingPath + [IndexCodingKey(index)],
218218
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
219219
underlyingError: error
220220
)
@@ -233,7 +233,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
233233
return try value.convert()
234234
} catch {
235235
let context = DecodingError.Context(
236-
codingPath: codingPath + [IndexKey(index)],
236+
codingPath: codingPath + [IndexCodingKey(index)],
237237
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
238238
underlyingError: error
239239
)
@@ -252,7 +252,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
252252
return try value.convert()
253253
} catch {
254254
let context = DecodingError.Context(
255-
codingPath: codingPath + [IndexKey(index)],
255+
codingPath: codingPath + [IndexCodingKey(index)],
256256
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
257257
underlyingError: error
258258
)
@@ -271,7 +271,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
271271
return try value.convert()
272272
} catch {
273273
let context = DecodingError.Context(
274-
codingPath: codingPath + [IndexKey(index)],
274+
codingPath: codingPath + [IndexCodingKey(index)],
275275
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
276276
underlyingError: error
277277
)
@@ -290,7 +290,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
290290
return try value.convert()
291291
} catch {
292292
let context = DecodingError.Context(
293-
codingPath: codingPath + [IndexKey(index)],
293+
codingPath: codingPath + [IndexCodingKey(index)],
294294
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
295295
underlyingError: error
296296
)
@@ -309,7 +309,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
309309
return try value.convert()
310310
} catch {
311311
let context = DecodingError.Context(
312-
codingPath: codingPath + [IndexKey(index)],
312+
codingPath: codingPath + [IndexCodingKey(index)],
313313
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
314314
underlyingError: error
315315
)
@@ -328,7 +328,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
328328
return try value.convert()
329329
} catch {
330330
let context = DecodingError.Context(
331-
codingPath: codingPath + [IndexKey(index)],
331+
codingPath: codingPath + [IndexCodingKey(index)],
332332
debugDescription: "Expected \(type) but found \(value.backingTypeDescription)",
333333
underlyingError: error
334334
)
@@ -346,7 +346,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
346346
let nestedDecoder = InternalDecoder(
347347
storage: decoder.storage,
348348
value: value,
349-
codingPath: codingPath + [IndexKey(index)],
349+
codingPath: codingPath + [IndexCodingKey(index)],
350350
userInfo: decoder.userInfo,
351351
parent: decoder
352352
)
@@ -360,7 +360,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
360360
let nestedDecoder = InternalDecoder(
361361
storage: decoder.storage,
362362
value: value,
363-
codingPath: codingPath + [IndexKey(index)],
363+
codingPath: codingPath + [IndexCodingKey(index)],
364364
userInfo: decoder.userInfo,
365365
parent: decoder
366366
)
@@ -373,7 +373,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
373373
let nestedDecoder = InternalDecoder(
374374
storage: decoder.storage,
375375
value: value,
376-
codingPath: codingPath + [IndexKey(index)],
376+
codingPath: codingPath + [IndexCodingKey(index)],
377377
userInfo: decoder.userInfo,
378378
parent: decoder
379379
)
@@ -386,7 +386,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
386386
return InternalDecoder(
387387
storage: decoder.storage,
388388
value: value,
389-
codingPath: codingPath + [IndexKey(index)],
389+
codingPath: codingPath + [IndexCodingKey(index)],
390390
userInfo: decoder.userInfo,
391391
parent: decoder
392392
)
@@ -399,7 +399,7 @@ final class ArrayDecoder: UnkeyedDecodingContainer {
399399
) throws -> (JSON, Int) {
400400
guard !isAtEnd else {
401401
let context = DecodingError.Context(
402-
codingPath: codingPath + [IndexKey(currentIndex)],
402+
codingPath: codingPath + [IndexCodingKey(currentIndex)],
403403
debugDescription: "Couldn't find value"
404404
)
405405
throw DecodingError.valueNotFound(type, context)

Sources/JBirdCodableSupport/Decoder/ObjectDecoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ final class ObjectDecoder<Key>: KeyedDecodingContainerProtocol where Key: Coding
389389
return InternalDecoder(
390390
storage: decoder.storage,
391391
value: value,
392-
codingPath: codingPath + [SuperKey()],
392+
codingPath: codingPath + [SuperCodingKey()],
393393
userInfo: decoder.userInfo,
394394
parent: decoder
395395
)

Sources/JBirdCodableSupport/Encoder/ArrayEncoder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class ArrayEncoder: UnkeyedEncodingContainer {
142142
) throws where T: Encodable {
143143
let nestedEncoder = InternalEncoder(
144144
storage: encoder.storage,
145-
codingPath: codingPath + [IndexKey(count)],
145+
codingPath: codingPath + [IndexCodingKey(count)],
146146
userInfo: encoder.userInfo,
147147
autoPopContainers: false,
148148
onValueChange: nil
@@ -158,7 +158,7 @@ final class ArrayEncoder: UnkeyedEncodingContainer {
158158
let index = append(.object(JSON.Object()))
159159
let nestedEncoder = InternalEncoder(
160160
storage: encoder.storage,
161-
codingPath: codingPath + [IndexKey(index)],
161+
codingPath: codingPath + [IndexCodingKey(index)],
162162
userInfo: encoder.userInfo,
163163
autoPopContainers: true
164164
) { [encoder, containerIndex] json in
@@ -174,7 +174,7 @@ final class ArrayEncoder: UnkeyedEncodingContainer {
174174
let index = append(.array(JSON.Array()))
175175
let nestedEncoder = InternalEncoder(
176176
storage: encoder.storage,
177-
codingPath: codingPath + [IndexKey(index)],
177+
codingPath: codingPath + [IndexCodingKey(index)],
178178
userInfo: encoder.userInfo,
179179
autoPopContainers: true
180180
) { [encoder, containerIndex] json in
@@ -189,7 +189,7 @@ final class ArrayEncoder: UnkeyedEncodingContainer {
189189
let index = append(.null)
190190
return InternalEncoder(
191191
storage: encoder.storage,
192-
codingPath: codingPath + [IndexKey(index)],
192+
codingPath: codingPath + [IndexCodingKey(index)],
193193
userInfo: encoder.userInfo,
194194
autoPopContainers: true
195195
) { [encoder, containerIndex] json in

Sources/JBirdCodableSupport/Encoder/ObjectEncoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ final class ObjectEncoder<Key>: KeyedEncodingContainerProtocol where Key: Coding
202202
func superEncoder() -> any Encoder {
203203
InternalEncoder(
204204
storage: encoder.storage,
205-
codingPath: codingPath + [SuperKey()],
205+
codingPath: codingPath + [SuperCodingKey()],
206206
userInfo: encoder.userInfo,
207207
autoPopContainers: true
208208
) { [encoder, containerIndex] json in

Sources/JBirdCodableSupport/IndexKey.swift renamed to Sources/JBirdCodableSupport/IndexCodingKey.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// JBird
2-
// IndexKey.swift
2+
// IndexCodingKey.swift
33
//
44
// MIT License
55
//
@@ -23,12 +23,16 @@
2323
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
// SOFTWARE.
2525

26-
struct IndexKey: CodingKey {
26+
struct IndexCodingKey: CodingKey {
27+
28+
// MARK: - Initializers
2729

2830
init(_ index: Int) {
2931
self.index = index
3032
}
3133

34+
// MARK: - CodingKey
35+
3236
var stringValue: String {
3337
index.description
3438
}
@@ -45,6 +49,8 @@ struct IndexKey: CodingKey {
4549
self.init(intValue)
4650
}
4751

48-
let index: Int
52+
// MARK: - Private
53+
54+
private let index: Int
4955

5056
}

Sources/JBirdCodableSupport/SuperKey.swift renamed to Sources/JBirdCodableSupport/SuperCodingKey.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// JBird
2-
// SuperKey.swift
2+
// SuperCodingKey.swift
33
//
44
// MIT License
55
//
@@ -23,7 +23,7 @@
2323
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
// SOFTWARE.
2525

26-
struct SuperKey: CodingKey {
26+
struct SuperCodingKey: CodingKey {
2727

2828
// MARK: - Initializers
2929

Tests/JBirdCodableSupportTests/IndexKeyTests.swift renamed to Tests/JBirdCodableSupportTests/IndexCodingKeyTests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// JBird
2-
// IndexKeyTests.swift
2+
// IndexCodingKeyTests.swift
33
//
44
// MIT License
55
//
@@ -26,24 +26,23 @@
2626
@testable import JBirdCodableSupport
2727
import Testing
2828

29-
@Suite("Index Key Tests")
30-
struct IndexKeyTests {
29+
@Suite("Index CodingKey Tests")
30+
struct IndexCodingKeyTests {
3131

3232
#if compiler(>=6.2)
3333
@Test("String Init")
3434
func stringInit() async {
3535
await #expect(processExitsWith: .failure) {
36-
_ = IndexKey(stringValue: "foo")
36+
_ = IndexCodingKey(stringValue: "foo")
3737
}
3838
}
3939
#endif
4040

4141
@Test("Int Init")
4242
func intInit() async throws {
43-
let key = try #require(IndexKey(intValue: 12))
43+
let key = try #require(IndexCodingKey(intValue: 12))
4444
#expect(key.intValue == 12)
4545
#expect(key.stringValue == "12")
46-
#expect(key.index == 12)
4746
}
4847

4948
}

Tests/JBirdCodableSupportTests/SuperKeyTests.swift renamed to Tests/JBirdCodableSupportTests/SuperCodingKeyTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// JBird
2-
// SuperKeyTests.swift
2+
// SuperCodingKeyTests.swift
33
//
44
// MIT License
55
//
@@ -26,16 +26,16 @@
2626
@testable import JBirdCodableSupport
2727
import Testing
2828

29-
@Test("Test Super Key")
30-
func superKey() throws {
31-
let key = SuperKey()
29+
@Test("Test Super CodingKey")
30+
func superCodingKey() throws {
31+
let key = SuperCodingKey()
3232
#expect(key.stringValue == "super")
3333
#expect(key.intValue == nil)
34-
let stringInitKey = try #require(SuperKey(stringValue: "super"))
34+
let stringInitKey = try #require(SuperCodingKey(stringValue: "super"))
3535
#expect(stringInitKey.stringValue == "super")
3636
#expect(stringInitKey.intValue == nil)
37-
let invalidStringKey = SuperKey(stringValue: "plop")
37+
let invalidStringKey = SuperCodingKey(stringValue: "plop")
3838
#expect(invalidStringKey == nil)
39-
let invalidIntKey = SuperKey(intValue: 12)
39+
let invalidIntKey = SuperCodingKey(intValue: 12)
4040
#expect(invalidIntKey == nil)
4141
}

0 commit comments

Comments
 (0)