forked from apple/swift-collections
/
MinimalEncoder.swift
716 lines (638 loc) · 24.8 KB
/
MinimalEncoder.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// A Plist-style encoder that attempts to enforce a strict interpretation of
/// the `Encodable` protocol requirements. The encoded value is of an enum type
/// that exactly reflects the values encoded, allowing easy matching of
/// actual vs expected outputs.
///
/// This encoder verifies that `encoder(to:)` implementations do not keep
/// containers alive beyond their expected lifetime.
///
/// FIXME: Fix error reporting. We ought to throw errors instead of trapping
/// on validation problems, and we should include context information to help
/// isolating the problem.
public class MinimalEncoder {
public enum Value: Hashable {
case null
case bool(Bool)
case int(Int)
case int8(Int8)
case int16(Int16)
case int32(Int32)
case int64(Int64)
case uint(UInt)
case uint8(UInt8)
case uint16(UInt16)
case uint32(UInt32)
case uint64(UInt64)
case float(Float)
case double(Double)
case string(String)
case array([Value])
case dictionary([String: Value])
}
var userInfo: [CodingUserInfoKey: Any]
public init(userInfo: [CodingUserInfoKey: Any] = [:]) {
self.userInfo = userInfo
}
public func encode<T: Encodable>(_ value: T) throws -> Value {
let encoder = Encoder(base: self, parent: nil, key: nil)
return try encoder._encode(value, key: nil)
}
public static func encode<T: Encodable>(_ value: T) throws -> Value {
let base = MinimalEncoder()
return try base.encode(value)
}
}
extension MinimalEncoder {
struct _Key: CodingKey {
var stringValue: String
var intValue: Int?
static let `super` = Self("super")
init(_ string: String) {
self.stringValue = string
self.intValue = nil
}
init(_ index: Int) {
self.stringValue = "Index \(index)"
self.intValue = index
}
init?(stringValue: String) {
self.init(stringValue)
}
init?(intValue: Int) {
self.init(intValue)
}
}
}
extension MinimalEncoder.Value: CustomStringConvertible {
func _description(prefix: String, indent: String) -> String {
switch self {
case .null: return ".null"
case .bool(let v): return ".bool(\(v))"
case .int(let v): return ".int(\(v))"
case .int8(let v): return ".int8(\(v))"
case .int16(let v): return ".int16(\(v))"
case .int32(let v): return ".int32(\(v))"
case .int64(let v): return ".int64(\(v))"
case .uint(let v): return ".uint(\(v))"
case .uint8(let v): return ".uint8(\(v))"
case .uint16(let v): return ".uint16(\(v))"
case .uint32(let v): return ".uint32(\(v))"
case .uint64(let v): return ".uint64(\(v))"
case .float(let v): return ".float(\(v))"
case .double(let v): return ".double(\(v))"
case .string(let v): return ".string(\(String(reflecting: v)))"
case .array(let value):
if value.count == 0 { return ".array([])" }
var result = ".array([\n"
for v in value {
result += prefix
result += indent
result += v._description(prefix: prefix + indent, indent: indent)
result += ",\n"
}
result += prefix + "])"
return result
case .dictionary(let value):
if value.count == 0 { return ".dictionary([:])" }
var result = ".dictionary([\n"
for (k, v) in value.sorted(by: { $0.key < $1.key }) {
result += prefix
result += indent
result += String(reflecting: k)
result += ": "
result += v._description(prefix: prefix + indent, indent: indent)
result += ",\n"
}
result += prefix + "])"
return result
}
}
public var description: String {
self._description(prefix: "", indent: " ")
}
}
extension MinimalDecoder.Value {
public var typeDescription: String {
switch self {
case .null: return ".null"
case .bool(_): return ".bool"
case .int(_): return ".int"
case .int8(_): return ".int8"
case .int16(_): return ".int16"
case .int32(_): return ".int32"
case .int64(_): return ".int64"
case .uint(_): return ".uint"
case .uint8(_): return ".uint8"
case .uint16(_): return ".uint16"
case .uint32(_): return ".uint32"
case .uint64(_): return ".uint64"
case .float(_): return ".float"
case .double(_): return ".double"
case .string(_): return ".string"
case .array(_): return ".array"
case .dictionary(_): return ".dictionary"
}
}
}
protocol _MinimalEncoderContainer: AnyObject {
var base: MinimalEncoder { get }
var parent: _MinimalEncoderContainer? { get }
var key: CodingKey? { get }
var isValid: Bool { get }
func finalize() -> MinimalEncoder.Value?
}
extension _MinimalEncoderContainer {
var codingPath: [CodingKey] {
expectTrue(isValid, "Container isn't valid", trapping: true)
var path: [CodingKey] = []
var encoder: _MinimalEncoderContainer? = self
while let e = encoder {
if let key = e.key { path.append(key) }
encoder = e.parent
}
path.reverse()
return path
}
func _encode<T: Encodable>(_ value: T, key: CodingKey?) throws -> MinimalEncoder.Value {
expectTrue(isValid, "Container isn't valid", trapping: true)
var encoder = MinimalEncoder.Encoder(base: base, parent: self, key: key)
try value.encode(to: encoder)
expectTrue(isKnownUniquelyReferenced(&encoder), "\(T.self).encode(to:) retained its encoder", trapping: true)
guard let result = encoder.finalize() else {
preconditionFailure("\(T.self).encode(to:) failed to encode anything")
}
return result
}
}
extension MinimalEncoder {
final class Encoder {
unowned let base: MinimalEncoder
unowned let parent: _MinimalEncoderContainer?
let key: CodingKey?
var container: _MinimalEncoderContainer?
var isValid = true
init(base: MinimalEncoder, parent: _MinimalEncoderContainer?, key: CodingKey?) {
self.base = base
self.parent = parent
self.key = key
}
}
}
extension MinimalEncoder.Encoder: _MinimalEncoderContainer {
func finalize() -> MinimalEncoder.Value? {
expectTrue(isValid, "Container isn't valid", trapping: true)
let value = container?.finalize()
isValid = false
return value
}
}
extension MinimalEncoder.Encoder: Encoder {
var userInfo: [CodingUserInfoKey: Any] { base.userInfo }
func container<Key: CodingKey>(
keyedBy type: Key.Type
) -> KeyedEncodingContainer<Key> {
expectTrue(isValid, "Encoder isn't valid", trapping: true)
expectNil(container, "Cannot extract multiple containers", trapping: true)
let keyed = MinimalEncoder.KeyedContainer<Key>(base: base, parent: self, key: nil)
self.container = keyed
return .init(keyed)
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
expectTrue(isValid, "Encoder isn't valid", trapping: true)
expectNil(container, "Cannot extract multiple containers", trapping: true)
let unkeyed = MinimalEncoder.UnkeyedContainer(base: base, parent: self, key: nil)
self.container = unkeyed
return unkeyed
}
func singleValueContainer() -> SingleValueEncodingContainer {
expectTrue(isValid, "Encoder isn't valid", trapping: true)
expectNil(container, "Cannot extract multiple containers", trapping: true)
let single = MinimalEncoder.SingleValueContainer(base: base, parent: self, key: nil)
self.container = single
return single
}
}
extension MinimalEncoder {
final class KeyedContainer<Key: CodingKey> {
unowned let base: MinimalEncoder
unowned let parent: _MinimalEncoderContainer?
var key: CodingKey?
var values: [String: Value] = [:]
var isValid = true
var pendingContainer: AnyObject?
init(base: MinimalEncoder, parent: _MinimalEncoderContainer?, key: CodingKey?) {
self.base = base
self.parent = parent
self.key = key
}
func commitPendingContainer() {
expectTrue(self.isValid, "Container isn't valid", trapping: true)
guard var c = pendingContainer else { return }
self.pendingContainer = nil
expectTrue(isKnownUniquelyReferenced(&c), "Container was retained", trapping: true)
let container = c as! _MinimalEncoderContainer
let key = container.key!
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
guard let v = container.finalize() else { preconditionFailure("Container remained empty")}
values[key.stringValue] = v
}
}
}
extension MinimalEncoder.KeyedContainer: _MinimalEncoderContainer {
func finalize() -> MinimalEncoder.Value? {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
isValid = false
return .dictionary(values)
}
}
extension MinimalEncoder.KeyedContainer: KeyedEncodingContainerProtocol {
func encodeNil(forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .null
}
func encode(_ value: Bool, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .bool(value)
}
func encode(_ value: Int, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .int(value)
}
func encode(_ value: Int8, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .int8(value)
}
func encode(_ value: Int16, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .int16(value)
}
func encode(_ value: Int32, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .int32(value)
}
func encode(_ value: Int64, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .int64(value)
}
func encode(_ value: UInt, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .uint(value)
}
func encode(_ value: UInt8, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .uint8(value)
}
func encode(_ value: UInt16, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .uint16(value)
}
func encode(_ value: UInt32, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .uint32(value)
}
func encode(_ value: UInt64, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .uint64(value)
}
func encode(_ value: Float, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .float(value)
}
func encode(_ value: Double, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .double(value)
}
func encode(_ value: String, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = .string(value)
}
func encode<T: Encodable>(_ value: T, forKey key: Key) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
values[key.stringValue] = try _encode(value, key: key)
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type,
forKey key: Key
) -> KeyedEncodingContainer<NestedKey> {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
let keyed = MinimalEncoder.KeyedContainer<NestedKey>(base: base, parent: self, key: key)
pendingContainer = keyed
return .init(keyed)
}
func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key.stringValue) has already been encoded", trapping: true)
let unkeyed = MinimalEncoder.UnkeyedContainer(base: base, parent: self, key: key)
pendingContainer = unkeyed
return unkeyed
}
func superEncoder() -> Encoder {
let key = MinimalEncoder._Key.super
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key) has already been encoded", trapping: true)
let sup = MinimalEncoder.Encoder(base: base, parent: self, key: key)
pendingContainer = sup
return sup
}
func superEncoder(forKey key: Key) -> Encoder {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
expectNil(values[key.stringValue], "Key \(key) has already been encoded", trapping: true)
let sup = MinimalEncoder.Encoder(base: base, parent: self, key: key)
pendingContainer = sup
return sup
}
}
extension MinimalEncoder {
final class UnkeyedContainer {
unowned let base: MinimalEncoder
unowned let parent: _MinimalEncoderContainer?
let key: CodingKey?
var values: [Value] = []
var isValid = true
var pendingContainer: AnyObject?
init(base: MinimalEncoder, parent: _MinimalEncoderContainer?, key: CodingKey?) {
self.base = base
self.parent = parent
self.key = key
}
func commitPendingContainer() {
expectTrue(self.isValid, "Container isn't valid", trapping: true)
guard var c = pendingContainer else { return }
self.pendingContainer = nil
expectTrue(isKnownUniquelyReferenced(&c), "Container was retained", trapping: true)
let container = c as! _MinimalEncoderContainer
guard let v = container.finalize() else { preconditionFailure("Container remained empty")}
values.append(v)
}
}
}
extension MinimalEncoder.UnkeyedContainer: _MinimalEncoderContainer {
func finalize() -> MinimalEncoder.Value? {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
isValid = false
return .array(values)
}
}
extension MinimalEncoder.UnkeyedContainer: UnkeyedEncodingContainer {
var count: Int {
values.count
}
func encodeNil() throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.null)
}
func encode(_ value: Bool) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.bool(value))
}
func encode(_ value: Int) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.int(value))
}
func encode(_ value: Int8) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.int8(value))
}
func encode(_ value: Int16) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.int16(value))
}
func encode(_ value: Int32) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.int32(value))
}
func encode(_ value: Int64) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.int64(value))
}
func encode(_ value: UInt) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.uint(value))
}
func encode(_ value: UInt8) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.uint8(value))
}
func encode(_ value: UInt16) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.uint16(value))
}
func encode(_ value: UInt32) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.uint32(value))
}
func encode(_ value: UInt64) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.uint64(value))
}
func encode(_ value: Float) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.float(value))
}
func encode(_ value: Double) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.double(value))
}
func encode(_ value: String) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(.string(value))
}
func encode<T: Encodable>(_ value: T) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
values.append(try _encode(value, key: key))
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type
) -> KeyedEncodingContainer<NestedKey> {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
let keyed = MinimalEncoder.KeyedContainer<NestedKey>(
base: base,
parent: self,
key: MinimalEncoder._Key(count))
pendingContainer = keyed
return .init(keyed)
}
func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
let unkeyed = MinimalEncoder.UnkeyedContainer(base: base, parent: self, key: MinimalEncoder._Key(count))
pendingContainer = unkeyed
return unkeyed
}
func superEncoder() -> Encoder {
expectTrue(isValid, "Container isn't valid", trapping: true)
commitPendingContainer()
let sup = MinimalEncoder.Encoder(base: base, parent: self, key: MinimalEncoder._Key(count))
pendingContainer = sup
return sup
}
}
extension MinimalEncoder {
final class SingleValueContainer {
unowned let base: MinimalEncoder
unowned let parent: _MinimalEncoderContainer?
let key: CodingKey?
var value: Value?
var isValid = true
init(base: MinimalEncoder, parent: _MinimalEncoderContainer?, key: CodingKey?) {
self.base = base
self.parent = parent
self.key = key
}
}
}
extension MinimalEncoder.SingleValueContainer: _MinimalEncoderContainer {
func finalize() -> MinimalEncoder.Value? {
expectTrue(isValid, "Container isn't valid", trapping: true)
isValid = false
return value
}
}
extension MinimalEncoder.SingleValueContainer: SingleValueEncodingContainer {
func encodeNil() throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .null
}
func encode(_ value: Bool) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .bool(value)
}
func encode(_ value: Int) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .int(value)
}
func encode(_ value: Int8) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .int8(value)
}
func encode(_ value: Int16) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .int16(value)
}
func encode(_ value: Int32) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .int32(value)
}
func encode(_ value: Int64) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .int64(value)
}
func encode(_ value: UInt) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .uint(value)
}
func encode(_ value: UInt8) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .uint8(value)
}
func encode(_ value: UInt16) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .uint16(value)
}
func encode(_ value: UInt32) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .uint32(value)
}
func encode(_ value: UInt64) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .uint64(value)
}
func encode(_ value: Float) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .float(value)
}
func encode(_ value: Double) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .double(value)
}
func encode(_ value: String) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = .string(value)
}
func encode<T: Encodable>(_ value: T) throws {
expectTrue(isValid, "Container isn't valid", trapping: true)
expectNil(self.value, "Cannot encode multiple values into a single-value container", trapping: true)
self.value = try _encode(value, key: nil)
}
}