@@ -114,10 +114,20 @@ public struct OpenAPIValueContainer: Codable, Hashable, Sendable {
114
114
self . init ( validatedValue: item)
115
115
} else if let item = try ? container. decode ( String . self) {
116
116
self . init ( validatedValue: item)
117
- } else if let item = try ? container. decode ( [ OpenAPIValueContainer ] . self) {
118
- self . init ( validatedValue: item. map ( \. value) )
119
- } else if let item = try ? container. decode ( [ String : OpenAPIValueContainer ] . self) {
120
- self . init ( validatedValue: item. mapValues ( \. value) )
117
+ } else if var container = try ? decoder. unkeyedContainer ( ) {
118
+ var items : [ ( any Sendable ) ? ] = [ ]
119
+ if let count = container. count { items. reserveCapacity ( count) }
120
+ while !container. isAtEnd {
121
+ let item = try container. decode ( OpenAPIValueContainer . self)
122
+ items. append ( item. value)
123
+ }
124
+ self . init ( validatedValue: items)
125
+ } else if let container = try ? decoder. container ( keyedBy: StringKey . self) {
126
+ let keyValuePairs = try container. allKeys. map { key -> ( String , ( any Sendable ) ? ) in
127
+ let item = try container. decode ( OpenAPIValueContainer . self, forKey: key)
128
+ return ( key. stringValue, item. value)
129
+ }
130
+ self . init ( validatedValue: Dictionary ( uniqueKeysWithValues: keyValuePairs) )
121
131
} else {
122
132
throw DecodingError . dataCorruptedError (
123
133
in: container,
@@ -133,36 +143,53 @@ public struct OpenAPIValueContainer: Codable, Hashable, Sendable {
133
143
/// - Parameter encoder: The encoder to which the value should be encoded.
134
144
/// - Throws: An error if the encoding process encounters issues or if the value is invalid.
135
145
public func encode( to encoder: any Encoder ) throws {
136
- var container = encoder. singleValueContainer ( )
137
146
guard let value = value else {
147
+ var container = encoder. singleValueContainer ( )
138
148
try container. encodeNil ( )
139
149
return
140
150
}
141
151
#if canImport(Foundation)
142
152
if value is NSNull {
153
+ var container = encoder. singleValueContainer ( )
143
154
try container. encodeNil ( )
144
155
return
145
156
}
146
157
#if canImport(CoreFoundation)
147
158
if let nsNumber = value as? NSNumber {
159
+ var container = encoder. singleValueContainer ( )
148
160
try encode ( nsNumber, to: & container)
149
161
return
150
162
}
151
163
#endif
152
164
#endif
153
165
switch value {
154
- case let value as Bool : try container. encode ( value)
155
- case let value as Int : try container. encode ( value)
156
- case let value as Double : try container. encode ( value)
157
- case let value as String : try container. encode ( value)
166
+ case let value as Bool :
167
+ var container = encoder. singleValueContainer ( )
168
+ try container. encode ( value)
169
+ case let value as Int :
170
+ var container = encoder. singleValueContainer ( )
171
+ try container. encode ( value)
172
+ case let value as Double :
173
+ var container = encoder. singleValueContainer ( )
174
+ try container. encode ( value)
175
+ case let value as String :
176
+ var container = encoder. singleValueContainer ( )
177
+ try container. encode ( value)
158
178
case let value as [ ( any Sendable ) ? ] :
159
- try container. encode ( value. map ( OpenAPIValueContainer . init ( validatedValue: ) ) )
179
+ var container = encoder. unkeyedContainer ( )
180
+ for item in value {
181
+ let containerItem = OpenAPIValueContainer ( validatedValue: item)
182
+ try container. encode ( containerItem)
183
+ }
160
184
case let value as [ String : ( any Sendable ) ? ] :
161
- try container. encode ( value. mapValues ( OpenAPIValueContainer . init ( validatedValue: ) ) )
185
+ var container = encoder. container ( keyedBy: StringKey . self)
186
+ for (itemKey, itemValue) in value {
187
+ try container. encode ( OpenAPIValueContainer ( validatedValue: itemValue) , forKey: StringKey ( itemKey) )
188
+ }
162
189
default :
163
190
throw EncodingError . invalidValue (
164
191
value,
165
- . init( codingPath: container . codingPath, debugDescription: " OpenAPIValueContainer cannot be encoded " )
192
+ . init( codingPath: encoder . codingPath, debugDescription: " OpenAPIValueContainer cannot be encoded " )
166
193
)
167
194
}
168
195
}
@@ -357,36 +384,29 @@ public struct OpenAPIObjectContainer: Codable, Hashable, Sendable {
357
384
358
385
// MARK: Decodable
359
386
360
- /// Creates an `OpenAPIValueContainer` by decoding it from a single-value container in a given decoder.
361
- ///
362
- /// - Parameter decoder: The decoder used to decode the container.
363
- /// - Throws: An error if the decoding process encounters an issue or if the data does not match the expected format.
387
+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
364
388
public init ( from decoder: any Decoder ) throws {
365
- let container = try decoder. singleValueContainer ( )
366
- let item = try container. decode ( [ String : OpenAPIValueContainer ] . self)
367
- self . init ( validatedValue: item. mapValues ( \. value) )
389
+ let container = try decoder. container ( keyedBy: StringKey . self)
390
+ let keyValuePairs = try container. allKeys. map { key -> ( String , ( any Sendable ) ? ) in
391
+ let item = try container. decode ( OpenAPIValueContainer . self, forKey: key)
392
+ return ( key. stringValue, item. value)
393
+ }
394
+ self . init ( validatedValue: Dictionary ( uniqueKeysWithValues: keyValuePairs) )
368
395
}
369
396
370
397
// MARK: Encodable
371
398
372
- /// Encodes the `OpenAPIValueContainer` into a format that can be stored or transmitted via the given encoder.
373
- ///
374
- /// - Parameter encoder: The encoder used to perform the encoding.
375
- /// - Throws: An error if the encoding process encounters an issue or if the data does not match the expected format.
399
+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
376
400
public func encode( to encoder: any Encoder ) throws {
377
- var container = encoder. singleValueContainer ( )
378
- try container. encode ( value. mapValues ( OpenAPIValueContainer . init ( validatedValue: ) ) )
401
+ var container = encoder. container ( keyedBy: StringKey . self)
402
+ for (itemKey, itemValue) in value {
403
+ try container. encode ( OpenAPIValueContainer ( validatedValue: itemValue) , forKey: StringKey ( itemKey) )
404
+ }
379
405
}
380
406
381
407
// MARK: Equatable
382
408
383
- /// Compares two `OpenAPIObjectContainer` instances for equality by comparing their inner key-value dictionaries.
384
- ///
385
- /// - Parameters:
386
- /// - lhs: The left-hand side `OpenAPIObjectContainer` to compare.
387
- /// - rhs: The right-hand side `OpenAPIObjectContainer` to compare.
388
- ///
389
- /// - Returns: `true` if the `OpenAPIObjectContainer` instances are equal, `false` otherwise.
409
+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
390
410
public static func == ( lhs: OpenAPIObjectContainer , rhs: OpenAPIObjectContainer ) -> Bool {
391
411
let lv = lhs. value
392
412
let rv = rhs. value
@@ -401,9 +421,7 @@ public struct OpenAPIObjectContainer: Codable, Hashable, Sendable {
401
421
402
422
// MARK: Hashable
403
423
404
- /// Hashes the `OpenAPIObjectContainer` instance into the provided `Hasher`.
405
- ///
406
- /// - Parameter hasher: The `Hasher` into which the hash value is combined.
424
+ // swift-format-ignore: AllPublicDeclarationsHaveDocumentation
407
425
public func hash( into hasher: inout Hasher ) {
408
426
for (key, itemValue) in value {
409
427
hasher. combine ( key)
@@ -474,9 +492,14 @@ public struct OpenAPIArrayContainer: Codable, Hashable, Sendable {
474
492
/// - Parameter decoder: The decoder to use for decoding the array of values.
475
493
/// - Throws: An error if the decoding process fails or if the decoded values cannot be validated.
476
494
public init ( from decoder: any Decoder ) throws {
477
- let container = try decoder. singleValueContainer ( )
478
- let item = try container. decode ( [ OpenAPIValueContainer ] . self)
479
- self . init ( validatedValue: item. map ( \. value) )
495
+ var container = try decoder. unkeyedContainer ( )
496
+ var items : [ ( any Sendable ) ? ] = [ ]
497
+ if let count = container. count { items. reserveCapacity ( count) }
498
+ while !container. isAtEnd {
499
+ let item = try container. decode ( OpenAPIValueContainer . self)
500
+ items. append ( item. value)
501
+ }
502
+ self . init ( validatedValue: items)
480
503
}
481
504
482
505
// MARK: Encodable
@@ -486,8 +509,11 @@ public struct OpenAPIArrayContainer: Codable, Hashable, Sendable {
486
509
/// - Parameter encoder: The encoder to use for encoding the array of values.
487
510
/// - Throws: An error if the encoding process fails.
488
511
public func encode( to encoder: any Encoder ) throws {
489
- var container = encoder. singleValueContainer ( )
490
- try container. encode ( value. map ( OpenAPIValueContainer . init ( validatedValue: ) ) )
512
+ var container = encoder. unkeyedContainer ( )
513
+ for item in value {
514
+ let containerItem = OpenAPIValueContainer ( validatedValue: item)
515
+ try container. encode ( containerItem)
516
+ }
491
517
}
492
518
493
519
// MARK: Equatable
0 commit comments