Merge pull request #158 from jpsim/fix-157
_KeyedDecodingContainer and _UnkeyedDecodingContainer did not properly decode null
This commit is contained in:
commit
e8dd69f2ad
|
@ -28,6 +28,10 @@
|
|||
[Norio Nomura](https://github.com/norio-nomura)
|
||||
[#146](https://github.com/jpsim/Yams/pull/146)
|
||||
|
||||
* Fix null/~/NULL/Null were parsed as strings, not nil by `YAMLDecoder`.
|
||||
[Norio Nomura](https://github.com/norio-nomura)
|
||||
[#157](https://github.com/jpsim/Yams/issues/157)
|
||||
|
||||
## 1.0.1
|
||||
|
||||
##### Breaking
|
||||
|
|
|
@ -116,7 +116,7 @@ private struct _KeyedDecodingContainer<Key: CodingKey> : KeyedDecodingContainerP
|
|||
func contains(_ key: Key) -> Bool { return mapping[key.stringValue] != nil }
|
||||
|
||||
func decodeNil(forKey key: Key) throws -> Bool {
|
||||
return try node(for: key) == Node("null", Tag(.null))
|
||||
return try decoder(for: key).decodeNil()
|
||||
}
|
||||
|
||||
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable & ScalarConstructible {
|
||||
|
@ -173,12 +173,7 @@ private struct _UnkeyedDecodingContainer: UnkeyedDecodingContainer {
|
|||
|
||||
mutating func decodeNil() throws -> Bool {
|
||||
try throwErrorIfAtEnd(Any?.self)
|
||||
if currentNode == Node("null", Tag(.null)) {
|
||||
currentIndex += 1
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return try currentDecoder { $0.decodeNil() }
|
||||
}
|
||||
|
||||
mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable & ScalarConstructible {
|
||||
|
|
|
@ -316,6 +316,29 @@ class EncoderTests: XCTestCase { // swiftlint:disable:this type_body_length
|
|||
expectEqual(type(of: decoded), Employee.self, "Expected decoded value to be of type Employee; got \(type(of: decoded)) instead.")
|
||||
}
|
||||
|
||||
func test_null_yml() throws {
|
||||
let s = """
|
||||
n1: ~
|
||||
n2: null
|
||||
n3: NULL
|
||||
n4: Null
|
||||
n5:
|
||||
"""
|
||||
struct Test: Decodable {
|
||||
let n1: String?
|
||||
let n2: String?
|
||||
let n3: String?
|
||||
let n4: String?
|
||||
let n5: String?
|
||||
}
|
||||
let t = try YAMLDecoder().decode(Test.self, from: s)
|
||||
XCTAssertNil(t.n1)
|
||||
XCTAssertNil(t.n2)
|
||||
XCTAssertNil(t.n3)
|
||||
XCTAssertNil(t.n4)
|
||||
XCTAssertNil(t.n5)
|
||||
}
|
||||
|
||||
// MARK: - Helper Functions
|
||||
|
||||
private func _testRoundTrip<T>(of value: T,
|
||||
|
@ -1089,7 +1112,8 @@ extension EncoderTests {
|
|||
("testValuesInUnkeyedContainer", testValuesInUnkeyedContainer),
|
||||
("testDictionary", testDictionary),
|
||||
("testNodeTypeMismatch", testNodeTypeMismatch),
|
||||
("testDecodingConcreteTypeParameter", testDecodingConcreteTypeParameter)
|
||||
("testDecodingConcreteTypeParameter", testDecodingConcreteTypeParameter),
|
||||
("test_null_yml", test_null_yml)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue