@@ -40,6 +40,20 @@ public enum PklValueType: UInt8, Decodable, Sendable {
4040
4141public protocol PklSerializableType : Decodable {
4242 static var messageTag : PklValueType { get }
43+
44+ static func decode( _ fields: [ MessagePackValue ] , codingPath: [ any CodingKey ] ) throws -> Self
45+ }
46+
47+ extension PklSerializableType {
48+ static func checkFieldCount( _ fields: [ MessagePackValue ] , codingPath: [ any CodingKey ] , min: Int ) throws {
49+ guard fields. count >= min else {
50+ throw DecodingError . dataCorrupted (
51+ . init(
52+ codingPath: codingPath,
53+ debugDescription: " Expected at least \( min) fields but got \( fields. count) "
54+ ) )
55+ }
56+ }
4357}
4458
4559public protocol PklSerializableValueUnitType : PklSerializableType {
@@ -49,35 +63,41 @@ public protocol PklSerializableValueUnitType: PklSerializableType {
4963 init ( _: ValueType , unit: UnitType )
5064}
5165
52- extension Decodable where Self: PklSerializableValueUnitType {
66+ extension Decodable where Self: PklSerializableType {
5367 public init ( from decoder: Decoder ) throws {
5468 guard let decoder = decoder as? _PklDecoder else {
5569 fatalError ( " \( Self . self) can only be decoded using \( _PklDecoder. self) , but was: \( decoder) " )
5670 }
57- self = try Self . decodeValueUnitType ( from: decoder. value, at: decoder. codingPath)
58- }
59- }
60-
61- extension PklSerializableValueUnitType {
62- static func decodeValueUnitType(
63- from value: MessagePackValue ,
64- at codingPath: [ CodingKey ]
65- ) throws -> Self {
66- guard case . array( let arr) = value else {
71+ let codingPath = decoder. codingPath
72+ guard case . array( let arr) = decoder. value else {
6773 throw DecodingError . dataCorrupted (
6874 . init(
6975 codingPath: codingPath,
70- debugDescription: " Expected array but got \( value. debugDataTypeDescription) "
76+ debugDescription: " Expected array but got \( decoder . value. debugDataTypeDescription) "
7177 ) )
7278 }
7379 let code = try arr [ 0 ] . decode ( PklValueType . self)
80+ guard arr. count > 0 else {
81+ throw DecodingError . dataCorrupted (
82+ . init(
83+ codingPath: codingPath,
84+ debugDescription: " Expected non-empty array "
85+ ) )
86+ }
7487 guard Self . messageTag == code else {
7588 throw DecodingError . dataCorrupted (
7689 . init( codingPath: codingPath, debugDescription: " Cannot decode \( code) into \( Self . self) " ) )
7790 }
7891
79- let value = try arr [ 1 ] . decode ( Self . ValueType. self)
80- let unit = try arr [ 2 ] . decode ( Self . UnitType. self)
92+ self = try Self . decode ( arr, codingPath: codingPath)
93+ }
94+ }
95+
96+ extension Decodable where Self: PklSerializableValueUnitType {
97+ public static func decode( _ fields: [ MessagePackValue ] , codingPath: [ any CodingKey ] ) throws -> Self {
98+ try checkFieldCount ( fields, codingPath: codingPath, min: 3 )
99+ let value = try fields [ 1 ] . decode ( Self . ValueType. self)
100+ let unit = try fields [ 2 ] . decode ( Self . UnitType. self)
81101 return Self ( value, unit: unit)
82102 }
83103}
@@ -237,6 +257,15 @@ extension _PklDecoder {
237257 case . dataSize:
238258 let decoder = try _PklDecoder ( value: propertyValue)
239259 return try PklAny ( value: DataSize ( from: decoder) )
260+ case . pair:
261+ let decoder = try _PklDecoder ( value: propertyValue)
262+ return try PklAny ( value: Pair < AnyHashable ? , AnyHashable ? > ( from: decoder) )
263+ case . regex:
264+ let decoder = try _PklDecoder ( value: propertyValue)
265+ return try PklAny ( value: PklRegex ( from: decoder) )
266+ case . intSeq:
267+ let decoder = try _PklDecoder ( value: propertyValue)
268+ return try PklAny ( value: IntSeq ( from: decoder) )
240269 case . bytes:
241270 guard case . bin( let bytes) = value [ 1 ] else {
242271 throw DecodingError . dataCorrupted (
0 commit comments