Skip to content

Commit a53672d

Browse files
authored
Merge branch 'main' into enable_MemberImportVisibility_check
2 parents e13688b + be84570 commit a53672d

13 files changed

+292
-43
lines changed

Sources/StructuredFieldValues/Decoder/BareInnerListDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ extension BareInnerListDecoder: UnkeyedDecodingContainer {
6666
self.decoder.pop()
6767
}
6868

69-
if type is Data.Type {
69+
switch type {
70+
case is Data.Type:
7071
let container = try self.decoder.singleValueContainer()
7172
return try container.decode(Data.self) as! T
72-
} else if type is Decimal.Type {
73+
case is Decimal.Type:
7374
let container = try self.decoder.singleValueContainer()
7475
return try container.decode(Decimal.self) as! T
75-
} else if type is Date.Type {
76+
case is Date.Type:
7677
let container = try self.decoder.singleValueContainer()
7778
return try container.decode(Date.self) as! T
78-
} else {
79+
case is DisplayString.Type:
80+
let container = try self.decoder.singleValueContainer()
81+
return try container.decode(DisplayString.self) as! T
82+
default:
7983
return try type.init(from: self.decoder)
8084
}
8185
}

Sources/StructuredFieldValues/Decoder/BareItemDecoder.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ extension BareItemDecoder: SingleValueDecodingContainer {
141141
return Date(timeIntervalSince1970: Double(date))
142142
}
143143

144+
func decode(_: DisplayString.Type) throws -> DisplayString {
145+
guard case .displayString(let string) = self.item else {
146+
throw StructuredHeaderError.invalidTypeForItem
147+
}
148+
149+
return DisplayString(rawValue: string)
150+
}
151+
144152
func decodeNil() -> Bool {
145153
// Items are never nil.
146154
false
@@ -182,6 +190,8 @@ extension BareItemDecoder: SingleValueDecodingContainer {
182190
return try self.decode(Decimal.self) as! T
183191
case is Date.Type:
184192
return try self.decode(Date.self) as! T
193+
case is DisplayString.Type:
194+
return try self.decode(DisplayString.self) as! T
185195
default:
186196
throw StructuredHeaderError.invalidTypeForItem
187197
}

Sources/StructuredFieldValues/Decoder/DictionaryKeyedContainer.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ extension DictionaryKeyedContainer: KeyedDecodingContainerProtocol {
4949
self.decoder.pop()
5050
}
5151

52-
if type is Data.Type {
52+
switch type {
53+
case is Data.Type:
5354
let container = try self.decoder.singleValueContainer()
5455
return try container.decode(Data.self) as! T
55-
} else if type is Decimal.Type {
56+
case is Decimal.Type:
5657
let container = try self.decoder.singleValueContainer()
5758
return try container.decode(Decimal.self) as! T
58-
} else if type is Date.Type {
59+
case is Date.Type:
5960
let container = try self.decoder.singleValueContainer()
6061
return try container.decode(Date.self) as! T
61-
} else {
62+
case is DisplayString.Type:
63+
let container = try self.decoder.singleValueContainer()
64+
return try container.decode(DisplayString.self) as! T
65+
default:
6266
return try type.init(from: self.decoder)
6367
}
6468
}

Sources/StructuredFieldValues/Decoder/KeyedInnerListDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ extension KeyedInnerListDecoder: KeyedDecodingContainerProtocol {
5454
self.decoder.pop()
5555
}
5656

57-
if type is Data.Type {
57+
switch type {
58+
case is Data.Type:
5859
let container = try self.decoder.singleValueContainer()
5960
return try container.decode(Data.self) as! T
60-
} else if type is Decimal.Type {
61+
case is Decimal.Type:
6162
let container = try self.decoder.singleValueContainer()
6263
return try container.decode(Decimal.self) as! T
63-
} else if type is Date.Type {
64+
case is Date.Type:
6465
let container = try self.decoder.singleValueContainer()
6566
return try container.decode(Date.self) as! T
66-
} else {
67+
case is DisplayString.Type:
68+
let container = try self.decoder.singleValueContainer()
69+
return try container.decode(DisplayString.self) as! T
70+
default:
6771
return try type.init(from: self.decoder)
6872
}
6973
}

Sources/StructuredFieldValues/Decoder/KeyedItemDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ extension KeyedItemDecoder: KeyedDecodingContainerProtocol {
5454
self.decoder.pop()
5555
}
5656

57-
if type is Data.Type {
57+
switch type {
58+
case is Data.Type:
5859
let container = try self.decoder.singleValueContainer()
5960
return try container.decode(Data.self) as! T
60-
} else if type is Decimal.Type {
61+
case is Decimal.Type:
6162
let container = try self.decoder.singleValueContainer()
6263
return try container.decode(Decimal.self) as! T
63-
} else if type is Date.Type {
64+
case is Date.Type:
6465
let container = try self.decoder.singleValueContainer()
6566
return try container.decode(Date.self) as! T
66-
} else {
67+
case is DisplayString.Type:
68+
let container = try self.decoder.singleValueContainer()
69+
return try container.decode(DisplayString.self) as! T
70+
default:
6771
return try type.init(from: self.decoder)
6872
}
6973
}

Sources/StructuredFieldValues/Decoder/KeyedTopLevelListDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ extension KeyedTopLevelListDecoder: KeyedDecodingContainerProtocol {
5454
self.decoder.pop()
5555
}
5656

57-
if type is Data.Type {
57+
switch type {
58+
case is Data.Type:
5859
let container = try self.decoder.singleValueContainer()
5960
return try container.decode(Data.self) as! T
60-
} else if type is Decimal.Type {
61+
case is Decimal.Type:
6162
let container = try self.decoder.singleValueContainer()
6263
return try container.decode(Decimal.self) as! T
63-
} else if type is Date.Type {
64+
case is Date.Type:
6465
let container = try self.decoder.singleValueContainer()
6566
return try container.decode(Date.self) as! T
66-
} else {
67+
case is DisplayString.Type:
68+
let container = try self.decoder.singleValueContainer()
69+
return try container.decode(DisplayString.self) as! T
70+
default:
6771
return try type.init(from: self.decoder)
6872
}
6973
}

Sources/StructuredFieldValues/Decoder/ParametersDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ extension ParametersDecoder: KeyedDecodingContainerProtocol {
4949
self.decoder.pop()
5050
}
5151

52-
if type is Data.Type {
52+
switch type {
53+
case is Data.Type:
5354
let container = try self.decoder.singleValueContainer()
5455
return try container.decode(Data.self) as! T
55-
} else if type is Decimal.Type {
56+
case is Decimal.Type:
5657
let container = try self.decoder.singleValueContainer()
5758
return try container.decode(Decimal.self) as! T
58-
} else if type is Date.Type {
59+
case is Date.Type:
5960
let container = try self.decoder.singleValueContainer()
6061
return try container.decode(Date.self) as! T
61-
} else {
62+
case is DisplayString.Type:
63+
let container = try self.decoder.singleValueContainer()
64+
return try container.decode(DisplayString.self) as! T
65+
default:
6266
return try type.init(from: self.decoder)
6367
}
6468
}

Sources/StructuredFieldValues/Decoder/StructuredFieldValueDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,20 @@ extension StructuredFieldValueDecoder {
111111

112112
// An escape hatch here for top-level data: if we don't do this, it'll ask for
113113
// an unkeyed container and get very confused.
114-
if type is Data.Type {
114+
switch type {
115+
case is Data.Type:
115116
let container = try decoder.singleValueContainer()
116117
return try container.decode(Data.self) as! StructuredField
117-
} else if type is Decimal.Type {
118+
case is Decimal.Type:
118119
let container = try decoder.singleValueContainer()
119120
return try container.decode(Decimal.self) as! StructuredField
120-
} else if type is Date.Type {
121+
case is Date.Type:
121122
let container = try decoder.singleValueContainer()
122123
return try container.decode(Date.self) as! StructuredField
123-
} else {
124+
case is DisplayString.Type:
125+
let container = try decoder.singleValueContainer()
126+
return try container.decode(DisplayString.self) as! StructuredField
127+
default:
124128
return try type.init(from: decoder)
125129
}
126130
}

Sources/StructuredFieldValues/Decoder/TopLevelListDecoder.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ extension TopLevelListDecoder: UnkeyedDecodingContainer {
6666
self.decoder.pop()
6767
}
6868

69-
if type is Data.Type {
69+
switch type {
70+
case is Data.Type:
7071
let container = try self.decoder.singleValueContainer()
7172
return try container.decode(Data.self) as! T
72-
} else if type is Decimal.Type {
73+
case is Decimal.Type:
7374
let container = try self.decoder.singleValueContainer()
7475
return try container.decode(Decimal.self) as! T
75-
} else if type is Date.Type {
76+
case is Date.Type:
7677
let container = try self.decoder.singleValueContainer()
7778
return try container.decode(Date.self) as! T
78-
} else {
79+
case is DisplayString.Type:
80+
let container = try self.decoder.singleValueContainer()
81+
return try container.decode(DisplayString.self) as! T
82+
default:
7983
return try type.init(from: self.decoder)
8084
}
8185
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftNIO open source project
4+
//
5+
// Copyright (c) 2020-2024 Apple Inc. and the SwiftNIO project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
/// A type that represents the Display String Structured Type.
16+
public struct DisplayString: RawRepresentable, Codable, Equatable, Hashable {
17+
public typealias RawValue = String
18+
public var rawValue: String
19+
20+
public init(rawValue: String) {
21+
self.rawValue = rawValue
22+
}
23+
}

Sources/StructuredFieldValues/Encoder/StructuredFieldValueEncoder.swift

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,20 @@ class _StructuredFieldEncoder {
150150
fileprivate func encodeItemField<StructuredField: Encodable>(_ data: StructuredField) throws -> [UInt8] {
151151
self.push(key: .init(stringValue: ""), newStorage: .itemHeader)
152152

153-
// There's an awkward special hook here: if the outer type is `Data` or `Decimal`,
154-
// we skip the regular encoding path. This is because otherwise `Data` will
155-
// ask for an unkeyed container and `Decimal` for a keyed one,
156-
// and it all falls apart.
153+
// There's an awkward special hook here: if the outer type is `Data`, `Decimal`, `Date` or
154+
// `DisplayString`, we skip the regular encoding path.
157155
//
158156
// Everything else goes through the normal flow.
159-
if let value = data as? Data {
160-
try self.encode(value)
161-
} else if let value = data as? Decimal {
162-
try self.encode(value)
163-
} else if let value = data as? Date {
164-
try self.encode(value)
165-
} else {
157+
switch data {
158+
case is Data:
159+
try self.encode(data)
160+
case is Decimal:
161+
try self.encode(data)
162+
case is Date:
163+
try self.encode(data)
164+
case is DisplayString:
165+
try self.encode(data)
166+
default:
166167
try data.encode(to: self)
167168
}
168169

@@ -316,6 +317,10 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
316317
try self.currentStackEntry.storage.insertBareItem(.date(date))
317318
}
318319

320+
func encode(_ data: DisplayString) throws {
321+
try self.currentStackEntry.storage.insertBareItem(.displayString(data.rawValue))
322+
}
323+
319324
func encode<T>(_ value: T) throws where T: Encodable {
320325
switch value {
321326
case let value as UInt8:
@@ -352,6 +357,8 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
352357
try self.encode(value)
353358
case let value as Date:
354359
try self.encode(value)
360+
case let value as DisplayString:
361+
try self.encode(value)
355362
default:
356363
throw StructuredHeaderError.invalidTypeForItem
357364
}
@@ -480,6 +487,10 @@ extension _StructuredFieldEncoder {
480487
try self.currentStackEntry.storage.appendBareItem(.date(date))
481488
}
482489

490+
func append(_ value: DisplayString) throws {
491+
try self.currentStackEntry.storage.appendBareItem(.displayString(value.rawValue))
492+
}
493+
483494
func append<T>(_ value: T) throws where T: Encodable {
484495
switch value {
485496
case let value as UInt8:
@@ -516,6 +527,8 @@ extension _StructuredFieldEncoder {
516527
try self.append(value)
517528
case let value as Date:
518529
try self.append(value)
530+
case let value as DisplayString:
531+
try self.append(value)
519532
default:
520533
// Some other codable type.
521534
switch self.currentStackEntry.storage {
@@ -658,6 +671,12 @@ extension _StructuredFieldEncoder {
658671
try self.currentStackEntry.storage.insertBareItem(.date(date), atKey: key)
659672
}
660673

674+
func encode(_ value: DisplayString, forKey key: String) throws {
675+
let key = self.sanitizeKey(key)
676+
let displayString = value.rawValue
677+
try self.currentStackEntry.storage.insertBareItem(.displayString(displayString), atKey: key)
678+
}
679+
661680
func encode<T>(_ value: T, forKey key: String) throws where T: Encodable {
662681
let key = self.sanitizeKey(key)
663682

@@ -696,6 +715,8 @@ extension _StructuredFieldEncoder {
696715
try self.encode(value, forKey: key)
697716
case let value as Date:
698717
try self.encode(value, forKey: key)
718+
case let value as DisplayString:
719+
try self.encode(value, forKey: key)
699720
default:
700721
// Ok, we don't know what this is. This can only happen for a dictionary, or
701722
// for anything with parameters, or for lists, or for inner lists.

0 commit comments

Comments
 (0)