Skip to content

Commit ac16b20

Browse files
committed
Codable cleanup
1 parent c45cdb7 commit ac16b20

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

stdlib/public/core/ClosedRange.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,25 +491,23 @@ public typealias CountableClosedRange<Bound: Strideable> = ClosedRange<Bound>
491491

492492
extension ClosedRange: Codable where Bound: Codable {
493493
private enum CodingKeys: String, CodingKey {
494-
case from
495-
case to
494+
case lowerBound = "from"
495+
case upperBound = "to"
496496
}
497497

498-
@inlinable
499498
public init(from decoder: Decoder) throws {
500499
let container = try decoder.container(keyedBy: CodingKeys.self)
501-
let lowerBound = try container.decode(Bound.self, forKey: .from)
502-
let upperBound = try container.decode(Bound.self, forKey: .to)
500+
let lowerBound = try container.decode(Bound.self, forKey: .lowerBound)
501+
let upperBound = try container.decode(Bound.self, forKey: .upperBound)
503502
guard lowerBound <= upperBound else {
504-
throw DecodingError.dataCorruptedError(forKey: CodingKeys.to, in: container, debugDescription: "upperBound (to) cannot be lower than lowerBound (from)")
503+
throw DecodingError.dataCorruptedError(forKey: CodingKeys.upperBound, in: container, debugDescription: "upperBound (to) cannot be lower than lowerBound (from)")
505504
}
506505
self = lowerBound...upperBound
507506
}
508507

509-
@inlinable
510508
public func encode(to encoder: Encoder) throws {
511509
var container = encoder.container(keyedBy: CodingKeys.self)
512-
try container.encode(self.lowerBound, forKey: .from)
513-
try container.encode(self.upperBound, forKey: .to)
510+
try container.encode(self.lowerBound, forKey: .lowerBound)
511+
try container.encode(self.upperBound, forKey: .upperBound)
514512
}
515513
}

stdlib/public/core/Range.swift

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -407,26 +407,24 @@ extension Range: Hashable where Bound: Hashable {
407407

408408
extension Range: Codable where Bound: Codable {
409409
private enum CodingKeys: String, CodingKey {
410-
case from
411-
case upTo
410+
case lowerBound = "from"
411+
case upperBound = "upTo"
412412
}
413413

414-
@inlinable
415414
public init(from decoder: Decoder) throws {
416415
let container = try decoder.container(keyedBy: CodingKeys.self)
417-
let lowerBound = try container.decode(Bound.self, forKey: .from)
418-
let upperBound = try container.decode(Bound.self, forKey: .upTo)
416+
let lowerBound = try container.decode(Bound.self, forKey: .lowerBound)
417+
let upperBound = try container.decode(Bound.self, forKey: .upperBound)
419418
guard lowerBound <= upperBound else {
420-
throw DecodingError.dataCorruptedError(forKey: CodingKeys.upTo, in: container, debugDescription: "upperBound (upTo) cannot be lower than lowerBound (from)")
419+
throw DecodingError.dataCorruptedError(forKey: CodingKeys.upperBound, in: container, debugDescription: "upperBound (upTo) cannot be lower than lowerBound (from)")
421420
}
422421
self = lowerBound..<upperBound
423422
}
424423

425-
@inlinable
426424
public func encode(to encoder: Encoder) throws {
427425
var container = encoder.container(keyedBy: CodingKeys.self)
428-
try container.encode(self.lowerBound, forKey: .from)
429-
try container.encode(self.upperBound, forKey: .upTo)
426+
try container.encode(self.lowerBound, forKey: .lowerBound)
427+
try container.encode(self.upperBound, forKey: .upperBound)
430428
}
431429
}
432430

@@ -474,19 +472,17 @@ extension PartialRangeUpTo: RangeExpression {
474472

475473
extension PartialRangeUpTo: Codable where Bound: Codable {
476474
private enum CodingKeys: String, CodingKey {
477-
case beginningUpTo
475+
case upperBound = "beginningUpTo"
478476
}
479477

480-
@inlinable
481478
public init(from decoder: Decoder) throws {
482479
let container = try decoder.container(keyedBy: CodingKeys.self)
483-
self = ..<try container.decode(Bound.self, forKey: .beginningUpTo)
480+
self = try (..<container.decode(Bound.self, forKey: .upperBound))
484481
}
485482

486-
@inlinable
487483
public func encode(to encoder: Encoder) throws {
488484
var container = encoder.container(keyedBy: CodingKeys.self)
489-
try container.encode(self.upperBound, forKey: .beginningUpTo)
485+
try container.encode(self.upperBound, forKey: .upperBound)
490486
}
491487
}
492488

@@ -533,19 +529,17 @@ extension PartialRangeThrough: RangeExpression {
533529

534530
extension PartialRangeThrough: Codable where Bound: Codable {
535531
private enum CodingKeys: String, CodingKey {
536-
case beginningTo
532+
case upperBound = "beginningTo"
537533
}
538534

539-
@inlinable
540535
public init(from decoder: Decoder) throws {
541536
let container = try decoder.container(keyedBy: CodingKeys.self)
542-
self = ...try container.decode(Bound.self, forKey: .beginningTo)
537+
self = try (...container.decode(Bound.self, forKey: .upperBound))
543538
}
544539

545-
@inlinable
546540
public func encode(to encoder: Encoder) throws {
547541
var container = encoder.container(keyedBy: CodingKeys.self)
548-
try container.encode(self.upperBound, forKey: .beginningTo)
542+
try container.encode(self.upperBound, forKey: .upperBound)
549543
}
550544
}
551545

@@ -687,19 +681,17 @@ extension PartialRangeFrom: Sequence
687681

688682
extension PartialRangeFrom: Codable where Bound: Codable {
689683
private enum CodingKeys: String, CodingKey {
690-
case toEndFrom
684+
case lowerBound = "toEndFrom"
691685
}
692686

693-
@inlinable
694687
public init(from decoder: Decoder) throws {
695688
let container = try decoder.container(keyedBy: CodingKeys.self)
696-
self = try container.decode(Bound.self, forKey: .toEndFrom)...
689+
self = try container.decode(Bound.self, forKey: .lowerBound)...
697690
}
698691

699-
@inlinable
700692
public func encode(to encoder: Encoder) throws {
701693
var container = encoder.container(keyedBy: CodingKeys.self)
702-
try container.encode(self.lowerBound, forKey: .toEndFrom)
694+
try container.encode(self.lowerBound, forKey: .lowerBound)
703695
}
704696
}
705697

0 commit comments

Comments
 (0)