Skip to content

Commit

Permalink
Merge pull request #167 from jpsim/fix-157
Browse files Browse the repository at this point in the history
[yams-1.0-branch] _KeyedDecodingContainer and _UnkeyedDecodingContainer did not properly decode null
  • Loading branch information
norio-nomura authored Jan 30, 2019
2 parents 4e21d22 + fb05fa4 commit 231bdd1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
Expand Down
9 changes: 2 additions & 7 deletions Sources/Yams/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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 {
Expand Down Expand Up @@ -166,12 +166,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 {
Expand Down
26 changes: 25 additions & 1 deletion Tests/YamsTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1089,7 +1112,8 @@ extension EncoderTests {
("testValuesInUnkeyedContainer", testValuesInUnkeyedContainer),
("testDictionary", testDictionary),
("testNodeTypeMismatch", testNodeTypeMismatch),
("testDecodingConcreteTypeParameter", testDecodingConcreteTypeParameter)
("testDecodingConcreteTypeParameter", testDecodingConcreteTypeParameter),
("test_null_yml", test_null_yml)
]
}
}
Expand Down

0 comments on commit 231bdd1

Please sign in to comment.