@@ -133,25 +133,7 @@ struct XMLKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
133133 ) )
134134 }
135135
136- let container : XMLKeyedDecodingContainer < NestedKey >
137- if let keyedContainer = value as? KeyedContainer {
138- container = XMLKeyedDecodingContainer < NestedKey > (
139- referencing: decoder,
140- wrapping: keyedContainer
141- )
142- } else if let keyedContainer = value as? KeyedBox {
143- container = XMLKeyedDecodingContainer < NestedKey > (
144- referencing: decoder,
145- wrapping: SharedBox ( keyedContainer)
146- )
147- } else if let singleBox = value as? SingleKeyedBox {
148- let element = ( singleBox. key, singleBox. element)
149- let keyedContainer = KeyedBox ( elements: [ element] , attributes: [ ] )
150- container = XMLKeyedDecodingContainer < NestedKey > (
151- referencing: decoder,
152- wrapping: SharedBox ( keyedContainer)
153- )
154- } else {
136+ guard let container = XMLKeyedDecodingContainer < NestedKey > ( box: value, decoder: decoder) else {
155137 throw DecodingError . typeMismatch (
156138 at: codingPath,
157139 expectation: [ String : Any ] . self,
@@ -192,6 +174,32 @@ struct XMLKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
192174 }
193175}
194176
177+ extension XMLKeyedDecodingContainer {
178+ internal init ? ( box: Box , decoder: XMLDecoderImplementation ) {
179+ switch box {
180+ case let keyedContainer as KeyedContainer :
181+ self . init (
182+ referencing: decoder,
183+ wrapping: keyedContainer
184+ )
185+ case let keyedBox as KeyedBox :
186+ self . init (
187+ referencing: decoder,
188+ wrapping: SharedBox ( keyedBox)
189+ )
190+ case let singleBox as SingleKeyedBox :
191+ let element = ( singleBox. key, singleBox. element)
192+ let keyedContainer = KeyedBox ( elements: [ element] , attributes: [ ] )
193+ self . init (
194+ referencing: decoder,
195+ wrapping: SharedBox ( keyedContainer)
196+ )
197+ default :
198+ return nil
199+ }
200+ }
201+ }
202+
195203/// Private functions
196204extension XMLKeyedDecodingContainer {
197205 private func _errorDescription( of key: CodingKey ) -> String {
@@ -248,7 +256,7 @@ extension XMLKeyedDecodingContainer {
248256
249257 let elements = container
250258 . withShared { keyedBox -> [ KeyedBox . Element ] in
251- keyedBox. elements [ key. stringValue] . map {
259+ return ( key . isInlined ? keyedBox. elements. values : keyedBox . elements [ key. stringValue] ) . map {
252260 if let singleKeyed = $0 as? SingleKeyedBox {
253261 return singleKeyed. element. isNull ? singleKeyed : singleKeyed. element
254262 } else {
@@ -258,7 +266,7 @@ extension XMLKeyedDecodingContainer {
258266 }
259267
260268 let attributes = container. withShared { keyedBox in
261- keyedBox. attributes [ key. stringValue]
269+ key . isInlined ? keyedBox . attributes . values : keyedBox. attributes [ key. stringValue]
262270 }
263271
264272 decoder. codingPath. append ( key)
@@ -271,7 +279,6 @@ extension XMLKeyedDecodingContainer {
271279 _ = decoder. nodeDecodings. removeLast ( )
272280 decoder. codingPath. removeLast ( )
273281 }
274- let box : Box
275282
276283 // You can't decode sequences from attributes, but other strategies
277284 // need special handling for empty sequences.
@@ -292,21 +299,26 @@ extension XMLKeyedDecodingContainer {
292299 return ( ( cdata as? StringBox ) ? . unboxed as? T ) ?? emptyString
293300 }
294301
295- switch strategy ( key) {
296- case . attribute? :
297- box = try getAttributeBox ( for: type, attributes, key)
298- case . element? :
299- box = try getElementBox ( for: type, elements, key)
300- case . elementOrAttribute? :
301- box = try getAttributeOrElementBox ( attributes, elements, key)
302- default :
303- switch type {
304- case is XMLAttributeProtocol . Type :
302+ let box : Box
303+ if key. isInlined {
304+ box = container. typeErasedUnbox ( )
305+ } else {
306+ switch strategy ( key) {
307+ case . attribute? :
305308 box = try getAttributeBox ( for: type, attributes, key)
306- case is XMLElementProtocol . Type :
309+ case . element ? :
307310 box = try getElementBox ( for: type, elements, key)
308- default :
311+ case . elementOrAttribute ? :
309312 box = try getAttributeOrElementBox ( attributes, elements, key)
313+ default :
314+ switch type {
315+ case is XMLAttributeProtocol . Type :
316+ box = try getAttributeBox ( for: type, attributes, key)
317+ case is XMLElementProtocol . Type :
318+ box = try getElementBox ( for: type, elements, key)
319+ default :
320+ box = try getAttributeOrElementBox ( attributes, elements, key)
321+ }
310322 }
311323 }
312324
0 commit comments