-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Decoding of Arrays of Empty Elements (#152)
## Overview Fixes #123 and extends #145 to accommodate decoding of arrays of empty strings or mixed arrays of non-empty and empty strings. ## Example We may now decode ```xml <container> <empty/> <empty/> <empty/> </container> ``` into the following type ```swift struct EmptyArray: Equatable, Codable { enum CodingKeys: String, CodingKey { case empties = "empty" } let empties: [Empty] } ``` where ```swift struct Empty: Equatable, Codable { } ``` We can also decode a value of the following type ```swift struct EmptyWrapper { let empty: Empty } ``` from ```xml <wrapper> <empty/> </wrapper> ``` Further, following from #145 we can decode arrays of empty strings ```xml <container> <string-type/> <string-type>My neighbors are empty<string-type> <string-type/> </container> ``` as ```swift struct EmptyArray: Equatable, Codable { enum CodingKeys: String, CodingKey { case stringType = "string-type" } let stringType: [String] } ``` and variants. ## Source Compatibility In cases where we decode an array of type `[String?]`, an empty element is now decoded as `""` rather than `nil`, the justification being that `String` can itself take empty values. We use the convention that `nil` signifies the absence of an element (if 0 or 1 of the element are allowed) as in the updated [BreakfastTest](https://github.com/bwetherfield/XMLCoder/blob/0d20614e47df98d1a10174e992c585edf629c9b9/Tests/XMLCoderTests/BreakfastTest.swift) and in the following error-throwing [test case](https://github.com/MaxDesiatov/XMLCoder/blob/2855777ff868e8a4c1d944c7da0ddb49a8b3893e/Tests/XMLCoderTests/Minimal/NullTests.swift#L56-L68). * Add nested choice unkeyed container decoding test * Fix nested unkeyed container implementation for nested keyed box * Clean up unwrapping syntax * Treat corner case of empty string value intrinsic * Remove xcodeproj junk * Add some logging to assess where we're at * Add cases for empty string as null element decoding * Swiftformat * Transform precondition to where clause in switch statement * Remove print statements * Add failing test for a nested array of empty-string value intrinsic elements * Do a little cleanup of single keyed box passing around * Refactor XMLKeyedDecodingContainer.decodeConcrete elements massaging * Remove xcscheme junk * Add fix for empty arrays, wrapped empties etc * Clean up * Revert singleKeyed dive change * Accommodate singleKeyed reading options * Alter Border Test * Add test case that returns [nil] (exists a non-optional property) * Eliminate possibly empty Int from Breakfast test * Fix formatting * Fix forcecast * Fix formatting * Update LinuxMain * Fix tests such that null elements read as empty strings * Fix linux main * Add nested array of empty strings decoding in the explicit style * Add mixed case empty and non-empty string cases * Reinstate missing test * Add test for decoding a null element into an optional type
- Loading branch information
1 parent
0eba4c6
commit 3a7fd96
Showing
13 changed files
with
315 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// EmptyArrayTest.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Benjamin Wetherfield on 10/1/19. | ||
// | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
struct Empty: Equatable, Codable {} | ||
|
||
struct EmptyArray: Equatable, Codable { | ||
enum CodingKeys: String, CodingKey { case empties = "empty" } | ||
let empties: [Empty] | ||
} | ||
|
||
struct EmptyWrapper: Equatable, Codable { | ||
let empty: Empty | ||
} | ||
|
||
struct OptionalEmptyWrapper: Equatable, Codable { | ||
let empty: Empty? | ||
} | ||
|
||
private let xml = """ | ||
<container> | ||
<empty/> | ||
<empty/> | ||
<empty/> | ||
</container> | ||
""" | ||
|
||
private let xmlArray = """ | ||
<container> | ||
<empty/> | ||
<empty/> | ||
<empty/> | ||
</container> | ||
""" | ||
|
||
private let xmlContainsEmpty = """ | ||
<container> | ||
<empty/> | ||
</container> | ||
""" | ||
|
||
class EmptyArrayTest: XCTestCase { | ||
func testEmptyArrayDecode() throws { | ||
let decoded = try XMLDecoder().decode([Empty].self, from: xml.data(using: .utf8)!) | ||
XCTAssertEqual(decoded, [Empty(), Empty(), Empty()]) | ||
} | ||
|
||
func testWrappedEmptyArrayDecode() throws { | ||
let decoded = try XMLDecoder().decode(EmptyArray.self, from: xmlArray.data(using: .utf8)!) | ||
XCTAssertEqual(decoded, EmptyArray(empties: [Empty(), Empty(), Empty()])) | ||
} | ||
|
||
func testWrappedEmptyDecode() throws { | ||
let decoded = try XMLDecoder().decode(EmptyWrapper.self, from: xmlContainsEmpty.data(using: .utf8)!) | ||
XCTAssertEqual(decoded, EmptyWrapper(empty: Empty())) | ||
} | ||
|
||
func testWrappedOptionalEmptyDecode() throws { | ||
let decoded = try XMLDecoder().decode(OptionalEmptyWrapper.self, from: xmlContainsEmpty.data(using: .utf8)!) | ||
XCTAssertEqual(decoded, OptionalEmptyWrapper(empty: Empty())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.