From 4556cfb81b417cd2722a7d412eb89d8f37b687f0 Mon Sep 17 00:00:00 2001 From: Norio Nomura Date: Tue, 29 Jan 2019 12:03:53 +0900 Subject: [PATCH 1/3] Add `test_null_yml()` to `EncoderTests` from #157 --- Tests/YamsTests/EncoderTests.swift | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Tests/YamsTests/EncoderTests.swift b/Tests/YamsTests/EncoderTests.swift index 49cd9f97..24715b41 100644 --- a/Tests/YamsTests/EncoderTests.swift +++ b/Tests/YamsTests/EncoderTests.swift @@ -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(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) ] } } From a64cee1ac51a9dbafc3e36add663e8b2da5089ef Mon Sep 17 00:00:00 2001 From: Norio Nomura Date: Tue, 29 Jan 2019 12:09:49 +0900 Subject: [PATCH 2/3] Delegate `decodeNil()` to `_Decoder` in `(_Keyed|_Unkeyed)DecodingContainer` --- Sources/Yams/Decoder.swift | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Sources/Yams/Decoder.swift b/Sources/Yams/Decoder.swift index 3c4d3182..802d7291 100644 --- a/Sources/Yams/Decoder.swift +++ b/Sources/Yams/Decoder.swift @@ -116,7 +116,7 @@ private struct _KeyedDecodingContainer : 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(_ 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(_ type: T.Type) throws -> T where T: Decodable & ScalarConstructible { From 34160742402ec71897348b4aed4e3f08c7156914 Mon Sep 17 00:00:00 2001 From: Norio Nomura Date: Tue, 29 Jan 2019 12:13:19 +0900 Subject: [PATCH 3/3] Add a bug fix entry to CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b70225f..af4bdcc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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