@@ -24,24 +24,34 @@ extension _PklDecoder {
2424
2525 var currentIndex : Int
2626
27- var members : [ MessagePackValue ]
27+ // either [members] or [bytes] are set
28+ var members : [ MessagePackValue ] ?
29+
30+ var bytes : [ UInt8 ] ?
2831
2932 var isAtEnd : Bool {
30- self . currentIndex >= self . members. count
33+ let length = self . members? . count ?? self . bytes!. count
34+ return self . currentIndex >= length
3135 }
3236
3337 init ( value: [ MessagePackValue ] , codingPath: [ CodingKey ] ) throws {
3438 func error( _ debugDescription: String ) -> DecodingError {
3539 . dataCorrupted( . init( codingPath: codingPath, debugDescription: debugDescription) )
3640 }
41+
3742 guard value. count > 0 else {
3843 throw error ( " Expected at least a type marker, but got 0 values " )
3944 }
4045 guard let pklType = try ? value [ 0 ] . decode ( PklValueType . self) else {
4146 throw error ( " Failed to decode type marker from ' \( value [ 0 ] ) ' " )
4247 }
4348
44- if value. count == 2 {
49+ if pklType == . bytes {
50+ guard case . bin( let bytes) = value [ 1 ] else {
51+ throw error ( " Expected binary but got \( value [ 1 ] . debugDataTypeDescription) " )
52+ }
53+ self . bytes = bytes
54+ } else if value. count == 2 {
4555 guard [ . list, . listing, . set] . contains ( pklType) else {
4656 throw error ( " Expected either list, listing, or set, but got \( pklType) " )
4757 }
@@ -58,16 +68,29 @@ extension _PklDecoder {
5868 }
5969
6070 func decodeNil( ) throws -> Bool {
71+ if self . bytes != nil {
72+ return false
73+ }
74+ let members = self . members!
6175 defer { currentIndex += 1 }
62- switch self . members [ self . currentIndex] {
76+ switch members [ self . currentIndex] {
6377 case . nil : return true
6478 default : return false
6579 }
6680 }
6781
82+ private func decodeBytes< T> ( _: T . Type , _ bytes: [ UInt8 ] ) throws -> T where T: Decodable {
83+ let value = bytes [ self . currentIndex]
84+ return value as! T
85+ }
86+
6887 func decode< T> ( _ type: T . Type ) throws -> T where T: Decodable {
6988 defer { currentIndex += 1 }
70- let value = self . members [ self . currentIndex]
89+ if let bytes = self . bytes {
90+ return try self . decodeBytes ( type, bytes)
91+ }
92+
93+ let value = self . members![ self . currentIndex]
7194
7295 // special case for polymorphic types
7396 if type == PklAny . self,
@@ -80,8 +103,11 @@ extension _PklDecoder {
80103 func nestedContainer< NestedKey> ( keyedBy type: NestedKey . Type ) throws -> KeyedDecodingContainer <
81104 NestedKey
82105 > where NestedKey: CodingKey {
106+ if self . bytes != nil {
107+ throw self . error ( " Cannot decode byte into \( type) because the decoded value is a byte array. " )
108+ }
83109 defer { currentIndex += 1 }
84- let value = self . members [ self . currentIndex]
110+ let value = self . members! [ self . currentIndex]
85111 var nestedCodingPath = self . codingPath
86112 if let key = NestedKey ( intValue: currentIndex) {
87113 nestedCodingPath. append ( key)
@@ -92,8 +118,11 @@ extension _PklDecoder {
92118 }
93119
94120 func nestedUnkeyedContainer( ) throws -> UnkeyedDecodingContainer {
121+ if self . bytes != nil {
122+ throw self . error ( " Cannot create nested UnkeyedDecodingContainer; the decoded value is a byte array. " )
123+ }
95124 defer { currentIndex += 1 }
96- let value = self . members [ self . currentIndex]
125+ let value = self . members! [ self . currentIndex]
97126 var nestedCodingPath = self . codingPath
98127 if let key = _PklKey ( intValue: currentIndex) {
99128 nestedCodingPath. append ( key)
@@ -108,5 +137,9 @@ extension _PklDecoder {
108137 func superDecoder( ) throws -> Decoder {
109138 fatalError ( " TODO " )
110139 }
140+
141+ private func error( _ debugDescription: String ) -> DecodingError {
142+ . dataCorrupted( . init( codingPath: self . codingPath, debugDescription: debugDescription) )
143+ }
111144 }
112145}
0 commit comments