-
Notifications
You must be signed in to change notification settings - Fork 8
/
AvroDecodable.swift
843 lines (801 loc) · 30.6 KB
/
AvroDecodable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//
// AvroClient/AvroDecodable.swift
//
// Created by Yang Liu on 6/09/18.
// Copyright © 2018 柳洋 and the project authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Foundation
final class AvroDecoder {
private let schema: AvroSchema
fileprivate let infoKey = CodingUserInfoKey(rawValue: "encodeOption")!
public var userInfo: [CodingUserInfoKey : Any] = [CodingUserInfoKey : Any]()
init(schema: AvroSchema) {
self.schema = schema
userInfo[infoKey] = AvroEncodingOption.AvroBinary
}
func setUserInfo(userInfo: [CodingUserInfoKey : Any]) {
self.userInfo = userInfo
}
func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
let encodingOption = userInfo[infoKey] as! AvroEncodingOption
switch encodingOption {
case .AvroBinary:
return try data.withUnsafeBytes{ (pointer: UnsafePointer<UInt8>) in
let decoder = try AvroBinaryDecoder(schema: schema, pointer: pointer, size: data.count)
return try type.init(from: decoder)
}
case .AvroJson:
return try JSONDecoder().decode(type, from: data)
}
}
func decode<K: Decodable, T: Decodable>(_ type: [K:T].Type, from data: Data) throws -> [K:T] {
return try data.withUnsafeBytes{ (pointer: UnsafePointer<UInt8>) in
let decoder = try AvroBinaryDecoder(schema: schema, pointer: pointer, size: data.count)
return try [K:T](decoder: decoder)
}
}
func decode(from data: Data) throws -> Any? {
return try data.withUnsafeBytes{ (pointer: UnsafePointer<UInt8>) in
let decoder = try AvroBinaryDecoder(schema: schema, pointer: pointer, size: data.count)
return try decoder.decode(schema: schema)
}
}
}
final class AvroBinaryDecoder: Decoder {
/// required by Decoder
var codingPath: [CodingKey] {
get {
return myCodingPath
}
}
var userInfo = [CodingUserInfoKey : Any]()
var myCodingPath = [CodingKey]()
/// AvroBinaryDecoder
var primitive: AvroBinaryDecodableProtocol
// schema relate
var schema: AvroSchema
init(schema: AvroSchema, pointer: UnsafePointer<UInt8>, size: Int) throws {
self.schema = schema
self.primitive = AvroPrimitiveDecoder(pointer: pointer, size: size)
}
fileprivate init(other: AvroBinaryDecoder, schema: AvroSchema) throws {
self.schema = schema
self.primitive = other.primitive
}
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
return KeyedDecodingContainer(AvroKeyedDecodingContainer<Key>(decoder: self, schema: schema))
}
func unkeyedContainer() throws -> UnkeyedDecodingContainer {
return try AvroUnkeyedDecodingContainer(decoder: self, schema: schema)
}
func singleValueContainer() throws -> SingleValueDecodingContainer {
return try AvroSingleValueDecodingContainer(decoder: self, schema: schema)
}
func decode<MK: Decodable, T: Decodable>(from: [MK: T].Type) throws -> [MK:T] {
let infoKey = CodingUserInfoKey(rawValue: "decodeOption")!
self.userInfo[infoKey] = self
return try [MK:T](decoder: self)
}
func decode(schema: AvroSchema) throws -> Any? {
switch schema {
case .nullSchema:
return nil
case .booleanSchema:
return try primitive.decode() as Bool
case .intSchema(let intSchema):
if let logicType = intSchema.logicalType, logicType == .date {
let unixdate = Double(try primitive.decode() as Int)
return Date(timeIntervalSince1970: unixdate)
}
return try primitive.decode() as Int32
case .longSchema:
return try primitive.decode() as Int64
case .floatSchema:
return try primitive.decode() as Float
case .doubleSchema:
return try primitive.decode() as Double
case .bytesSchema:
return try primitive.decode() as [UInt8]
case .stringSchema:
return try primitive.decode() as String
case .recordSchema(let recordSchema):
var value: [String: Any] = [String: Any]()
for f in recordSchema.fields {
if let v = try decode(schema: f.type) {
value[f.name] = v
}
}
return value
case .enumSchema(let enumSchema):
let index = try primitive.decode() as Int
if (index < 0 || index > enumSchema.symbols.count) {
print("index of symbols:",index)
throw BinaryDecodingError.indexOutofBoundary
}
return enumSchema.symbols[index]
case .arraySchema(let arraySchema):
var value: [Any] = [Any]()
var blockCount = try primitive.decode() as Int64
while blockCount != 0 {
if blockCount < 0 {
blockCount = -blockCount
let beforeDecodedLength = primitive.available
for _ in 0..<blockCount {
let blockSize = try primitive.decode() as Int64
if blockSize <= 0 {
return value
}
if let v = try decode(schema: arraySchema.items) {
value.append(v)
} else {
//skip unkown block
primitive.advance(Int(blockSize) - (beforeDecodedLength - primitive.available))
}
}
} else {
for _ in 0..<blockCount {
let v = try decode(schema: arraySchema.items)
value.append(v!)
}
}
blockCount = try primitive.decode() as Int64
}
return value
case .mapSchema(let mapSchema):
var value: [String: Any] = [String: Any]()
var blockCount = try primitive.decode() as Int64
while blockCount != 0 {
if blockCount < 0 {
blockCount = -blockCount
let beforeDecodedLength = primitive.available
for _ in 0..<blockCount {
let blockSize = try primitive.decode() as Int64
if blockSize <= 0 {
return value
}
if let k = try? primitive.decode() as String {
value[k] = try decode(schema: mapSchema.values)
} else {
//skip unkown block
primitive.advance(Int(blockSize) - (beforeDecodedLength - primitive.available))
}
}
}else {
for _ in 0..<blockCount{
let k = try primitive.decode() as String
value[k] = try decode(schema: mapSchema.values)
}
}
blockCount = try primitive.decode() as Int64
}
return value
case .unionSchema(let unionSchema):
let index = try primitive.decode() as Int64
if (index < 0 || index > unionSchema.branches.count) {
print("index of union:",index)
throw BinaryDecodingError.indexOutofBoundary
}
return try decode(schema:unionSchema.branches[Int(index)])
case .fixedSchema(let fixedSchema):
if let logicalType = fixedSchema.logicalType, logicalType == .duration {
return try primitive.decode(fixedSize: fixedSchema.size) as [UInt32]
}
return try primitive.decode(fixedSize: fixedSchema.size) as [UInt8]
case .errorSchema(let errorSchema):
var value: [String: Any] = [String: Any]()
for f in errorSchema.fields{
value[f.name] = try decode(schema: f.type)
}
return value
default:
return nil
}
}
}
fileprivate struct AvroKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
class unionIndexCache {
var indexMap: [String:Int] = [:]
func add(key: String, index: Int) {
indexMap[key] = index
}
func getIndex(key: String) -> Int? {
return indexMap[key]
}
}
var allKeys: [K] {
return schemaMap.keys.reduce(into: [K]()) { keys, key in
guard let key = K.init(stringValue: key) else {
return
}
keys.append(key)
}
}
var codingPath: [CodingKey] = []
fileprivate var decoder: AvroBinaryDecoder
var schemaMap: [String: AvroSchema] = [:]
var unionIndex: unionIndexCache
func schema(_ key: K) ->AvroSchema {
let s = schemaMap[key.stringValue]!
if case .unionSchema(let union) = s {
if let index = unionIndex.getIndex(key: key.stringValue) {
return union.branches[index]
}
}
return s
}
func contains(_ key: K) -> Bool {
return schemaMap.keys.contains(key.stringValue)
}
func decodeNil(forKey key: K) throws -> Bool {
let currentSchema = schema(key)
switch currentSchema {
case .nullSchema:
return true
case .unionSchema(let union):
let index = try! decoder.primitive.decode() as Int
guard index < union.branches.count else {
throw BinaryDecodingError.indexOutofBoundary
}
unionIndex.add(key: key.stringValue, index: index)
return union.branches[index].isNull()
default:
return false
}
}
@inlinable
mutating func decode(_ type: Bool.Type, forKey key: K) throws -> Bool {
guard self.schema(key).isBoolean() else {
throw BinaryDecodingError.typeMismatchWithSchemaBool
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: Int.Type, forKey key: K) throws -> Int {
guard self.schema(key).isInt() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt
}
return try Int(decoder.primitive.decode() as Int64)
}
@inlinable
mutating func decode(_ type: Int8.Type, forKey key: K) throws -> Int8 {
guard self.schema(key).isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt8
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: Int16.Type, forKey key: K) throws -> Int16 {
guard self.schema(key).isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt16
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: Int32.Type, forKey key: K) throws -> Int32 {
guard self.schema(key).isInt() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt32
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: Int64.Type, forKey key: K) throws -> Int64 {
guard self.schema(key).isLong() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt64
}
let r: Int64 = try decoder.primitive.decode()
return r
}
@inlinable
mutating func decode(_ type: UInt.Type, forKey key: K) throws -> UInt {
guard self.schema(key).isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: UInt8.Type, forKey key: K) throws -> UInt8 {
guard self.schema(key).isFixed() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt8
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: UInt16.Type, forKey key: K) throws -> UInt16 {
guard self.schema(key).isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt16
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: UInt32.Type, forKey key: K) throws -> UInt32 {
guard self.schema(key).isFixed() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt32
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: UInt64.Type, forKey key: K) throws -> UInt64 {
guard self.schema(key).isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt64
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: Float.Type, forKey key: K) throws -> Float {
guard self.schema(key).isFloat() else {
throw BinaryDecodingError.typeMismatchWithSchemaFloat
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: Double.Type, forKey key: K) throws -> Double {
guard self.schema(key).isDouble() else {
throw BinaryDecodingError.typeMismatchWithSchemaDouble
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: [UInt8].Type, forKey key: K) throws -> [UInt8] {
guard self.schema(key).isBytes() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt8
}
return try decoder.primitive.decode()
}
@inlinable
mutating func decode(_ type: [UInt32].Type, forKey key: K) throws -> [UInt32] {
let sch = self.schema(key)
switch sch {
case .fixedSchema(let fixed):
return try decoder.primitive.decode(fixedSize: fixed.size)
default:
throw BinaryDecodingError.typeMismatchWithSchemaUInt32
}
}
@inlinable
mutating func decode(_ type: String.Type, forKey key: K) throws -> String {
guard self.schema(key).isString() else {
throw BinaryDecodingError.typeMismatchWithSchemaString
}
return try decoder.primitive.decode()
}
@inlinable
func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T : Decodable {
let currentSchema = schema(key)
switch currentSchema {
case .mapSchema,.fixedSchema:
var container = try nestedUnkeyedContainer(forKey: key)
return try container.decode(type)
case .unknownSchema:
throw BinaryEncodingError.invalidSchema
default:
let innerDecoder = try! AvroBinaryDecoder(other: decoder, schema: currentSchema)
return try type.init(from: innerDecoder)
}
}
/*
func decode<MK: Decodable, T:Decodable>(_ type: [MK:T].Type, forKey key: K) throws -> [MK:T] {
let currentSchema = schema(key)
switch currentSchema {
case .mapSchema:
let size = try decoder.primitive.decode() as Int
if size == 0 {
return [MK:T]()
}
let innerDecoder = try! AvroBinaryDecoder(other:decoder,schema:currentSchema)
return try innerDecoder.decode(from: type)
case .fixedSchema:
var container = try nestedUnkeyedContainer(forKey: key)
return try container.decode(type)
case .unknownSchema:
throw BinaryEncodingError.invalidSchema
default:
let innerDecoder = try! AvroBinaryDecoder(other: decoder, schema: currentSchema)
return try type.init(from: innerDecoder)
}
}*/
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
return KeyedDecodingContainer(AvroKeyedDecodingContainer<NestedKey>(decoder: decoder, schema: schema(key)))
}
func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer {
return try AvroUnkeyedDecodingContainer(decoder: decoder, schema: schema(key))
}
func superDecoder() throws -> Decoder {
return decoder
}
func superDecoder(forKey key: K) throws -> Decoder {
return decoder
}
fileprivate init(decoder: AvroBinaryDecoder, schema: AvroSchema) {
self.decoder = decoder
switch(schema) {
case .recordSchema(let record):
for field in record.fields {
self.schemaMap[field.name] = field.type
}
self.schemaMap["fields"] = .fieldsSchema(record.fields)
case .errorSchema(let record):
for field in record.fields {
self.schemaMap[field.name] = field.type
}
self.schemaMap["fields"] = .fieldsSchema(record.fields)
case .fieldsSchema(let fields):
for field in fields {
self.schemaMap[field.name] = field.type
}
case .mapSchema(let map):
self.schemaMap[map.type] = map.values
case .fieldSchema(let field):
self.schemaMap[field.name] = field.type
default: self.schemaMap[schema.getName()!] = schema
}
self.codingPath = decoder.codingPath
self.unionIndex = unionIndexCache()
}
}
fileprivate struct AvroUnkeyedDecodingContainer: UnkeyedDecodingContainer, DecodingHelper {
var codingPath: [CodingKey]
var schema: AvroSchema
fileprivate var keySchema: AvroSchema? = nil
fileprivate var valueSchema: AvroSchema = AvroSchema()
var decoder: AvroBinaryDecoder
fileprivate var haveTail: Bool = false
fileprivate var haveBlock: Bool = false
fileprivate var countValue: Int
fileprivate var count: Int? {
return countValue
}
var isAtEnd: Bool {
if let c = count {
let ret = (currentIndex >= c)
return ret
} else {
return true
}
}
fileprivate var currentIndex: Int = 0
func getCurrentSchema() throws -> AvroSchema {
if count == 0 {
return valueSchema
}
if let k = keySchema {
if (currentIndex % 2) == 0 {
if self.haveBlock {
_ = try decoder.primitive.decode() as Int64
}
return k
}
return valueSchema
}
if self.haveBlock {
if currentIndex < count! {
_ = try decoder.primitive.decode() as Int64
}
}
return valueSchema
}
mutating func advanceIndex() {
currentIndex += 1
if currentIndex == countValue,haveTail {
if var blockCount = try? decoder.primitive.decode() as Int64 {
if blockCount == 0 {
return
}
if blockCount < 0 {
haveBlock = true
blockCount = -blockCount
}
if keySchema != nil {
blockCount <<= 1
}
countValue += Int(blockCount)
}
}
}
mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
advanceIndex()
return KeyedDecodingContainer(AvroKeyedDecodingContainer(decoder: decoder, schema: schema))
}
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
return try AvroUnkeyedDecodingContainer(decoder: decoder, schema: schema)
}
mutating func superDecoder() throws -> Decoder {
advanceIndex()
return decoder
}
@inlinable
mutating func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
defer {advanceIndex()}
let innerDecoder = try AvroBinaryDecoder(other: decoder, schema: getCurrentSchema())
let dd = try T.init(from: innerDecoder)
return dd
}
fileprivate init(decoder: AvroBinaryDecoder, schema: AvroSchema) throws {
self.decoder = decoder
self.codingPath = decoder.codingPath
switch schema {
/// get the size of avro array,bytes and map from the decoding data
case .arraySchema(let array):
let blockCount = try decoder.primitive.decode() as Int64
if blockCount < 0 {
self.countValue = -Int(blockCount)
self.haveBlock = true
} else {
self.countValue = Int(blockCount)
}
self.valueSchema = array.items
self.haveTail = true
self.schema = self.valueSchema
case .bytesSchema:
self.countValue = try Int(decoder.primitive.decode() as Int64)
self.valueSchema = schema
self.schema = self.valueSchema
case .mapSchema(let map):
/// map are key value pairs, so the count is doubled of block count
let blockCount = try decoder.primitive.decode() as Int64
if blockCount < 0 {
self.countValue = -(Int(blockCount)<<1)
self.haveBlock = true
} else {
self.countValue = Int(blockCount)<<1
}
self.schema = .stringSchema
self.keySchema = self.schema
self.valueSchema = map.values
self.haveTail = true
/// get the size of avro fixed from schema
case .fixedSchema(let fixed):
if let logicalType = fixed.logicalType, logicalType == .duration {
self.countValue = 3 /// 3 UInt32 : month/day/year
} else {
self.countValue = fixed.size
}
self.valueSchema = schema
self.schema = self.valueSchema
default:
self.valueSchema = schema
self.schema = self.valueSchema
self.countValue = 1
}
self.currentIndex = 0
}
}
fileprivate struct AvroSingleValueDecodingContainer: SingleValueDecodingContainer, DecodingHelper {
var codingPath: [CodingKey]
var schema: AvroSchema
var decoder: AvroBinaryDecoder
fileprivate init(decoder: AvroBinaryDecoder, schema: AvroSchema) throws {
self.decoder = decoder
self.codingPath = decoder.codingPath
switch schema {
case .recordSchema:
self.schema = schema
case .unionSchema(let union):
let index = try decoder.primitive.decode() as Int
guard index < union.branches.count else {
throw BinaryDecodingError.indexOutofBoundary
}
self.schema = union.branches[index]
default:
self.schema = schema
self.schema = schema.getSerializedSchema().first!
}
}
@inlinable
func decodeIfPresent(_ type: String.Type) throws -> String? {
switch self.schema {
case .stringSchema:
return try decoder.primitive.decode() as String
case .enumSchema(let symbols):
let id = try decoder.primitive.decode() as Int
return symbols.symbols[id]
default:
throw BinaryDecodingError.typeMismatchWithSchemaString
}
}
}
fileprivate protocol DecodingHelper {
var codingPath: [CodingKey] { get }
var decoder: AvroBinaryDecoder { get set}
var schema: AvroSchema {get}
}
extension DecodingHelper {
func decodeNil() -> Bool {
if self.schema.isNull() {
return true
}
return false
}
@inlinable
func decode(_ type: Bool.Type) throws -> Bool {
guard self.schema.isBoolean() else {
throw BinaryDecodingError.typeMismatchWithSchemaBool
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Int.Type) throws -> Int {
guard self.schema.isLong() || self.schema.isInt() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Int8.Type) throws -> Int8 {
guard self.schema.isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt8
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Int16.Type) throws -> Int16 {
guard self.schema.isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt16
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Int32.Type) throws -> Int32 {
guard self.schema.isInt()||self.schema.isContainer() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt32
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Int64.Type) throws -> Int64 {
guard self.schema.isLong() else {
throw BinaryDecodingError.typeMismatchWithSchemaInt64
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: UInt.Type) throws -> UInt {
guard self.schema.isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: UInt8.Type) throws -> UInt8 {
guard self.schema.isByte() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt8
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: UInt16.Type) throws -> UInt16 {
guard self.schema.isInteger() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt16
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: UInt32.Type) throws -> UInt32 {
guard self.schema.isFixed() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt32
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: UInt64.Type) throws -> UInt64 {
guard self.schema.isLong() else {
throw BinaryDecodingError.typeMismatchWithSchemaUInt64
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Float.Type) throws -> Float {
guard self.schema.isFloat() else {
throw BinaryDecodingError.typeMismatchWithSchemaFloat
}
return try decoder.primitive.decode()
}
@inlinable
func decode(_ type: Double.Type) throws -> Double {
switch self.schema {
case .doubleSchema:
return try decoder.primitive.decode()
case .intSchema(let intSchema):
/// Date is Codable in Swift and init(from: decoder), in singleValueContainer
/// the value is decoded as Double and date is from timeIntervalSinceReferenceDate,
/// so it need to be minus an offset to Jan 1 1970 according to Avro spec
if let logicalType = intSchema.logicalType, logicalType == .date {
let unixdate = Double(try decoder.primitive.decode() as Int)
return unixdate - Date.timeIntervalBetween1970AndReferenceDate
}
throw BinaryDecodingError.typeMismatchWithSchemaDouble
default:
throw BinaryDecodingError.typeMismatchWithSchemaDouble
}
}
@inlinable
func decode(_ type: String.Type) throws -> String {
switch self.schema {
case .stringSchema:
return try decoder.primitive.decode() as String
case .enumSchema(let symbols):
let id = try decoder.primitive.decode() as Int
return symbols.symbols[id]
default:
throw BinaryDecodingError.typeMismatchWithSchemaString
}
}
@inlinable
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
let d = try AvroBinaryDecoder(other: decoder, schema: schema)
return try type.init(from: d)
}
}
fileprivate struct UnsupportedAvroType: Error {}
fileprivate struct InvalidResponseFormat: Error {}
protocol AvroDecodable: Decodable {
init(decoder: AvroBinaryDecoder) throws
}
extension KeyedDecodingContainer {
func decode<MK: Decodable, T: Decodable>(
_ type: [MK : T].Type, forKey key: Key) throws -> [MK : T]
{
guard self.contains(key) else {
throw BinaryDecodingError.malformedAvro
}
var c = try nestedUnkeyedContainer(forKey: key)
var values = [MK : T]()
if c.count == 0 {
return values
}
while !c.isAtEnd {
if let k = try? c.decode(type.Key) {
values[k] = try c.decode(type.Value)
}
}
return values
}
func decodeIfPresent<MK: Decodable, T: Decodable>(
_ type: [MK : T].Type, forKey key: Key) throws -> [MK : T]?
{
guard self.contains(key) else {
throw BinaryDecodingError.malformedAvro
}
if try self.decodeNil(forKey: key) {
return nil
}
return try decode(type, forKey: key)
}
}
extension Dictionary: AvroDecodable where Key : Decodable, Value : Decodable {
init(decoder: AvroBinaryDecoder) throws {
self.init()
// Decode as an array of key-value pairs.
var container = try! decoder.unkeyedContainer()
// Count of key-value pairs should be even number
if let count = container.count {
guard count % 2 == 0 else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Expected collection of key-value pairs; encountered odd-length array instead."))
}
}
while !container.isAtEnd {
let key = try container.decode(Key.self)
guard !container.isAtEnd else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Unkeyed container reached end before value in key-value pair."))
}
let value = try container.decode(Value.self)
self[key] = value
}
}
}