Skip to content

Commit a75d98f

Browse files
committed
Refactor the looking for existing container code and change the error message on the condition
1 parent d61830b commit a75d98f

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

stdlib/public/Darwin/Foundation/JSONEncoder.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,17 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
500500

501501
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
502502
let containerKey = _converted(key).stringValue
503-
let existingContainer = self.container[containerKey]
504-
precondition(existingContainer is NSMutableDictionary?,
505-
"Attempt to request for keyed container with the key that previously unkeyed container already requested.")
506-
let dictionary = existingContainer as? NSMutableDictionary ?? NSMutableDictionary()
507-
self.container[containerKey] = dictionary
503+
let dictionary: NSMutableDictionary
504+
if let existingContainer = self.container[containerKey] {
505+
precondition(
506+
existingContainer is NSMutableDictionary,
507+
"Attempt to re-encode into nested KeyedEncodingContainer<\(Key.self)> for key \"\(containerKey)\" is invalid: non-keyed container already encoded for this key"
508+
)
509+
dictionary = existingContainer as! NSMutableDictionary
510+
} else {
511+
dictionary = NSMutableDictionary()
512+
self.container[containerKey] = dictionary
513+
}
508514

509515
self.codingPath.append(key)
510516
defer { self.codingPath.removeLast() }
@@ -515,11 +521,17 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
515521

516522
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
517523
let containerKey = _converted(key).stringValue
518-
let existingContainer = self.container[containerKey]
519-
precondition(existingContainer is NSMutableArray?,
520-
"Attempt to request for unkeyed container with the key that previously keyed container already requested.")
521-
let array = existingContainer as? NSMutableArray ?? NSMutableArray()
522-
self.container[containerKey] = array
524+
let array: NSMutableArray
525+
if let existingContainer = self.container[containerKey] {
526+
precondition(
527+
existingContainer is NSMutableArray,
528+
"Attempt to re-encode into nested UnkeyedEncodingContainer for key \"\(containerKey)\" is invalid: keyed container/single value already encoded for this key"
529+
)
530+
array = existingContainer as! NSMutableArray
531+
} else {
532+
array = NSMutableArray()
533+
self.container[containerKey] = array
534+
}
523535

524536
self.codingPath.append(key)
525537
defer { self.codingPath.removeLast() }

stdlib/public/Darwin/Foundation/PlistEncoder.swift

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,18 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo
266266

267267
public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
268268
let containerKey = key.stringValue
269-
let existingContainer = self.container[containerKey]
270-
precondition(existingContainer is NSMutableDictionary?,
271-
"Attempt to request for keyed container with the key that previously unkeyed container already requested.")
272-
let dictionary = existingContainer as? NSMutableDictionary ?? NSMutableDictionary()
273-
self.container[containerKey] = dictionary
274-
269+
let dictionary: NSMutableDictionary
270+
if let existingContainer = self.container[containerKey] {
271+
precondition(
272+
existingContainer is NSMutableDictionary,
273+
"Attempt to re-encode into nested KeyedEncodingContainer<\(Key.self)> for key \"\(containerKey)\" is invalid: non-keyed container already encoded for this key"
274+
)
275+
dictionary = existingContainer as! NSMutableDictionary
276+
} else {
277+
dictionary = NSMutableDictionary()
278+
self.container[containerKey] = dictionary
279+
}
280+
275281
self.codingPath.append(key)
276282
defer { self.codingPath.removeLast() }
277283

@@ -281,11 +287,17 @@ fileprivate struct _PlistKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCo
281287

282288
public mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
283289
let containerKey = key.stringValue
284-
let existingContainer = self.container[containerKey]
285-
precondition(existingContainer is NSMutableArray?,
286-
"Attempt to request for unkeyed container with the key that previously keyed container already requested.")
287-
let array = existingContainer as? NSMutableArray ?? NSMutableArray()
288-
self.container[containerKey] = array
290+
let array: NSMutableArray
291+
if let existingContainer = self.container[containerKey] {
292+
precondition(
293+
existingContainer is NSMutableArray,
294+
"Attempt to re-encode into nested UnkeyedEncodingContainer for key \"\(containerKey)\" is invalid: keyed container/single value already encoded for this key"
295+
)
296+
array = existingContainer as! NSMutableArray
297+
} else {
298+
array = NSMutableArray()
299+
self.container[containerKey] = array
300+
}
289301

290302
self.codingPath.append(key)
291303
defer { self.codingPath.removeLast() }

0 commit comments

Comments
 (0)