Skip to content

Commit

Permalink
Fix compatibility with Swift 5.0 (#77)
Browse files Browse the repository at this point in the history
When XMLCoder is compiled in Swift 5.0 mode it causes infinite loops in some tests, apparently due to change in `try?` behaviour introduced in SE-0230 Swift Evolution Proposal. In generic `unbox` handling of the `String` case with `try? unbox(box)` is always inferred as a call to `unbox<String?>` instead of a call to `unbox<String>`, as it was in Swift 4.2 mode. This is now fixed with rearranged type hints in this call. Also applied Swift 5.0 migrator suggestion to change `index` to `firstIndex`.

* Fix compatibility with Swift 5.0
* Cleanup print usage, add Swift v5 to Package.swift
  • Loading branch information
MaxDesiatov committed Feb 22, 2019
1 parent 7a1f965 commit eafcdf1
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Package.swift
Expand Up @@ -27,5 +27,6 @@ let package = Package(
name: "XMLCoderTests",
dependencies: ["XMLCoder"]
),
]
],
swiftLanguageVersions: [.v4_2, .version("5")]
)
2 changes: 1 addition & 1 deletion Sources/XMLCoder/Decoder/XMLDecoder.swift
Expand Up @@ -178,7 +178,7 @@ open class XMLDecoder {
}

// Find the first non-underscore character
guard let firstNonUnderscore = stringKey.index(where: { $0 != "_" }) else {
guard let firstNonUnderscore = stringKey.firstIndex(where: { $0 != "_" }) else {
// Reached the end without finding an _
return stringKey
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift
Expand Up @@ -308,7 +308,6 @@ extension XMLDecoderImplementation {

if type == Date.self || type == NSDate.self {
let date: Date = try unbox(box)

decoded = date as? T
} else if type == Data.self || type == NSData.self {
let data: Data = try unbox(box)
Expand All @@ -321,7 +320,7 @@ extension XMLDecoderImplementation {
decoded = decimal as? T
} else if
type == String.self || type == NSString.self,
let str: String = try? unbox(box), let value = str as? T {
let value = (try unbox(box) as String) as? T {
decoded = value
} else {
storage.push(container: box)
Expand Down
3 changes: 0 additions & 3 deletions Tests/XMLCoderTests/DynamicNodeEncodingTest.swift
Expand Up @@ -125,8 +125,6 @@ final class DynamicNodeEncodingTest: XCTestCase {
let header = XMLHeader(version: 1.0, encoding: "UTF-8")
let encoded = try encoder.encode(library, withRootKey: "library", header: header)
let xmlString = String(data: encoded, encoding: .utf8)
print(xmlString!)
print(libraryXMLTrueFalse)
XCTAssertEqual(xmlString, libraryXMLTrueFalse)
}

Expand Down Expand Up @@ -197,7 +195,6 @@ final class DynamicNodeEncodingTest: XCTestCase {
let data = try encoder.encode(library, withRootKey: "library",
header: XMLHeader(version: 1.0,
encoding: "UTF-8"))
print(String(data: data, encoding: .utf8)!)
let library2 = try decoder.decode(Library.self, from: data)
XCTAssertEqual(library, library2)
}
Expand Down

0 comments on commit eafcdf1

Please sign in to comment.