Description
Moving away from NSXMLParser (Objective-c) in my app to Decodable makes this library very handy. Thank you for forking and maintaining it.
I have a Goodreads XML response which I'm decoding with XMLCoder. That works great.
goodreads.xml.txt
Goodreads.swift.txt
GoodreadsTests.swift.txt
However, there is a level of indirection after following the books example which is bothering me a bit.
In the Book class I need to add this:
var authors: AuthorList
instead of this:
var authors: [Author]
where AuthorList is defined as:
struct AuthorList: Decodable {
var authors: [Author]
enum CodingKeys: String, CodingKey {
case authors = "author"
}
}
If I don't do that, I get a parsing error:
keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "book", intValue: nil), CodingKeys(stringValue: "authors", intValue: nil), _XMLKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"id\", intValue: nil) (\"id\").", underlyingError: nil))
/Users/tony/Downloads/XMLCoder-master/Tests/XMLCoderTests/GoodreadsTests.swift:24: error: -[XMLCoderTests.GoodreadsTests testGoodreadsBundle] : XCTAssertNotNil failed - error retrieving book
When using var authors: [Author]
I added a case to codingKeys in Book:
case authors = "author"
to no avail.
Of course the hack forces me to do this in the test case:
let authors = book?.authors.authors.map { $0 }
instead of this:
let authors = book?.authors.map { $0 }
Any hints to remove this extra level of indirection would be very much appreciated.
Thanks a lot.