6
6
7
7
import Foundation
8
8
9
- internal final class MetadataDecoder : Decoder {
10
- public var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
9
+ final class MetadataDecoder : Decoder {
10
+ public var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
11
11
public let codingPath : [ CodingKey ]
12
12
13
- private let metadata : [ String : String ]
13
+ private let metadata : [ String : String ]
14
14
private let dateFormatter : DateFormatter
15
- private lazy var keyedContainers = [ ObjectIdentifier : Any] ( )
15
+ private lazy var keyedContainers = [ ObjectIdentifier: Any] ( )
16
16
17
- public init ( metadata: [ String : String ] ,
18
- codingPath: [ CodingKey ] = [ ] ,
19
- dateFormatter: DateFormatter ) {
17
+ public init ( metadata: [ String : String ] ,
18
+ codingPath: [ CodingKey ] = [ ] ,
19
+ dateFormatter: DateFormatter )
20
+ {
20
21
self . metadata = metadata
21
22
self . codingPath = codingPath
22
23
self . dateFormatter = dateFormatter
@@ -69,19 +70,20 @@ private extension MetadataDecoder {
69
70
struct KeyedContainer < Key: CodingKey > : KeyedDecodingContainerProtocol {
70
71
var allKeys : [ Key ] { keys. all ( ) }
71
72
72
- let metadata : [ String : String ]
73
+ let metadata : [ String : String ]
73
74
let keys : KeyMap < Key >
74
75
let codingPath : [ CodingKey ]
75
76
let prefix : String
76
77
let dateFormatter : DateFormatter
77
78
78
- init ( metadata: [ String : String ] ,
79
+ init ( metadata: [ String : String ] ,
79
80
codingPath: [ CodingKey ] ,
80
- dateFormatter: DateFormatter ) {
81
+ dateFormatter: DateFormatter )
82
+ {
81
83
self . metadata = metadata
82
- self . keys = KeyMap ( raw: metadata. keys, codingPath: codingPath)
84
+ keys = KeyMap ( raw: metadata. keys, codingPath: codingPath)
83
85
self . codingPath = codingPath
84
- self . prefix = codingPath. asPrefix ( )
86
+ prefix = codingPath. asPrefix ( )
85
87
self . dateFormatter = dateFormatter
86
88
}
87
89
@@ -204,8 +206,8 @@ private extension MetadataDecoder {
204
206
throw DecodingError . keyNotFound ( key, DecodingError . Context (
205
207
codingPath: codingPath. appending ( key) ,
206
208
debugDescription: """
207
- No value found for key ' \( key. stringValue) '.
208
- """
209
+ No value found for key ' \( key. stringValue) '.
210
+ """
209
211
) )
210
212
}
211
213
@@ -220,9 +222,9 @@ private extension MetadataDecoder {
220
222
forKey: key,
221
223
in: self ,
222
224
debugDescription: """
223
- Could not convert ' \( string) ' into a value \
224
- of type ' \( String ( describing: T . self) ) '.
225
- """
225
+ Could not convert ' \( string) ' into a value \
226
+ of type ' \( String ( describing: T . self) ) '.
227
+ """
226
228
)
227
229
}
228
230
@@ -330,9 +332,9 @@ private extension MetadataDecoder {
330
332
DecodingError . Context (
331
333
codingPath: codingPath,
332
334
debugDescription: """
333
- Cannot obtain a keyed container while decoding \
334
- using an unkeyed metadata container.
335
- """
335
+ Cannot obtain a keyed container while decoding \
336
+ using an unkeyed metadata container.
337
+ """
336
338
)
337
339
)
338
340
}
@@ -359,10 +361,9 @@ private extension MetadataDecoder {
359
361
DecodingError . Context (
360
362
codingPath: codingPath,
361
363
debugDescription: """
362
- Index \( currentIndex) is out of bounds.
363
- """
364
- )
365
- )
364
+ Index \( currentIndex) is out of bounds.
365
+ """
366
+ ) )
366
367
}
367
368
368
369
let next = components [ currentIndex]
@@ -381,9 +382,9 @@ private extension MetadataDecoder {
381
382
throw DecodingError . dataCorruptedError (
382
383
in: self ,
383
384
debugDescription: """
384
- Could not convert ' \( string) ' into a value \
385
- of type ' \( String ( describing: T . self) ) '.
386
- """
385
+ Could not convert ' \( string) ' into a value \
386
+ of type ' \( String ( describing: T . self) ) '.
387
+ """
387
388
)
388
389
}
389
390
@@ -392,7 +393,7 @@ private extension MetadataDecoder {
392
393
}
393
394
394
395
struct UnkeyedDecoder : Decoder {
395
- var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
396
+ var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
396
397
397
398
let components : [ Substring ]
398
399
var codingPath : [ CodingKey ]
@@ -491,9 +492,9 @@ private extension MetadataDecoder {
491
492
throw DecodingError . dataCorruptedError (
492
493
in: self ,
493
494
debugDescription: """
494
- Could not convert ' \( value) ' into a value \
495
- of type ' \( String ( describing: T . self) ) '.
496
- """
495
+ Could not convert ' \( value) ' into a value \
496
+ of type ' \( String ( describing: T . self) ) '.
497
+ """
497
498
)
498
499
}
499
500
@@ -502,7 +503,7 @@ private extension MetadataDecoder {
502
503
}
503
504
504
505
struct SingleValueDecoder : Decoder {
505
- var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
506
+ var userInfo : [ CodingUserInfoKey : Any ] { [ : ] }
506
507
507
508
let value : String
508
509
let codingPath : [ CodingKey ]
@@ -536,7 +537,8 @@ private extension MetadataDecoder {
536
537
private var evaluated : Evaluated ?
537
538
538
539
init ( raw: Dictionary < String , String > . Keys ,
539
- codingPath: [ CodingKey ] ) {
540
+ codingPath: [ CodingKey ] )
541
+ {
540
542
self . raw = raw
541
543
self . codingPath = codingPath
542
544
}
@@ -604,7 +606,8 @@ private extension Array where Element == CodingKey {
604
606
private extension URL {
605
607
static func decode( from string: String ,
606
608
forKey key: CodingKey ? ,
607
- at codingPath: [ CodingKey ] ) throws -> Self {
609
+ at codingPath: [ CodingKey ] ) throws -> Self
610
+ {
608
611
guard let url = URL ( string: string) else {
609
612
throw DecodingError . dataCorrupted (
610
613
DecodingError . Context (
@@ -622,7 +625,8 @@ private extension Date {
622
625
static func decode( from string: String ,
623
626
forKey key: CodingKey ? ,
624
627
at codingPath: [ CodingKey ] ,
625
- formatter: DateFormatter ) throws -> Self {
628
+ formatter: DateFormatter ) throws -> Self
629
+ {
626
630
guard let date = formatter. date ( from: string) else {
627
631
let formatDescription = formatter. dateFormat. map {
628
632
" Expected format: \( $0) . "
@@ -632,8 +636,8 @@ private extension Date {
632
636
DecodingError . Context (
633
637
codingPath: key. map ( codingPath. appending) ?? codingPath,
634
638
debugDescription: """
635
- Invalid date string. \( formatDescription ?? " " )
636
- """
639
+ Invalid date string. \( formatDescription ?? " " )
640
+ """
637
641
)
638
642
)
639
643
}
@@ -652,8 +656,8 @@ private extension DecodingError {
652
656
DecodingError . Context (
653
657
codingPath: path,
654
658
debugDescription: """
655
- Cannot obtain a keyed decoding container within this context.
656
- """
659
+ Cannot obtain a keyed decoding container within this context.
660
+ """
657
661
)
658
662
)
659
663
}
@@ -664,8 +668,8 @@ private extension DecodingError {
664
668
DecodingError . Context (
665
669
codingPath: path,
666
670
debugDescription: """
667
- Cannot obtain an unkeyed decoding container within this context.
668
- """
671
+ Cannot obtain an unkeyed decoding container within this context.
672
+ """
669
673
)
670
674
)
671
675
}
@@ -676,8 +680,8 @@ private extension DecodingError {
676
680
DecodingError . Context (
677
681
codingPath: path,
678
682
debugDescription: """
679
- Cannot obtain a single value decoding container within this context.
680
- """
683
+ Cannot obtain a single value decoding container within this context.
684
+ """
681
685
)
682
686
)
683
687
}
@@ -691,36 +695,36 @@ private extension Array {
691
695
}
692
696
}
693
697
694
- internal func makeMetadataDecoder( for metadata: [ String : String ] ) -> MetadataDecoder {
698
+ func makeMetadataDecoder( for metadata: [ String : String ] ) -> MetadataDecoder {
695
699
let dateFormatter = DateFormatter ( )
696
700
dateFormatter. dateFormat = " yyyy-MM-dd "
697
701
dateFormatter. timeZone = . current
698
-
702
+
699
703
return MetadataDecoder (
700
704
metadata: metadata,
701
705
dateFormatter: dateFormatter
702
706
)
703
707
}
704
708
705
- internal func resolveDate( from decoder: MetadataDecoder ) throws -> Date ? {
709
+ func resolveDate( from decoder: MetadataDecoder ) throws -> Date ? {
706
710
return try decoder. decodeIfPresent ( " date " , as: Date . self)
707
711
}
708
712
709
713
private struct AnyCodingKey : CodingKey {
710
714
var stringValue : String
711
715
var intValue : Int ?
712
-
716
+
713
717
init ( _ string: String ) {
714
718
stringValue = string
715
719
}
716
-
720
+
717
721
init ? ( stringValue: String ) {
718
722
self . stringValue = stringValue
719
723
}
720
-
724
+
721
725
init ? ( intValue: Int ) {
722
726
self . intValue = intValue
723
- self . stringValue = String ( intValue)
727
+ stringValue = String ( intValue)
724
728
}
725
729
}
726
730
@@ -730,7 +734,7 @@ private extension Decoder {
730
734
func decodeIfPresent< T: Decodable > ( _ key: String , as type: T . Type = T . self) throws -> T ? {
731
735
return try decodeIfPresent ( AnyCodingKey ( key) , as: type)
732
736
}
733
-
737
+
734
738
/// Decode an optional value for a given key, specified as a `CodingKey`. Throws an error if the
735
739
/// specified key exists but is not able to be decoded as the inferred type.
736
740
func decodeIfPresent< T: Decodable , K: CodingKey > ( _ key: K , as type: T . Type = T . self) throws -> T ? {
0 commit comments