Skip to content

Commit

Permalink
Handle empty dictionaries (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim authored Jun 3, 2023
1 parent 948991e commit a34f950
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

##### Bug Fixes

* None.
* Empty dictionaries can be now represented, regardless of its key or element
type information.
[JP Simard](https://github.com/jpsim)
[#393](https://github.com/jpsim/Yams/issues/393)

## 5.0.5

Expand Down
2 changes: 2 additions & 0 deletions Sources/Yams/Representer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ extension Dictionary: NodeRepresentable {
private func represent(_ value: Any) throws -> Node {
if let representable = value as? NodeRepresentable {
return try representable.represented()
} else if (value as? NSDictionary)?.count == 0 {
return .mapping(Node.Mapping([]))
}
throw YamlError.representer(problem: "Failed to represent \(value)")
}
Expand Down
7 changes: 6 additions & 1 deletion Tests/YamsTests/RepresenterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class RepresenterTests: XCTestCase {
let intToInt = [1: 2]
XCTAssertEqual(try Node(intToInt), [1: 2])
}

func testEmptyDictionary() throws {
XCTAssertEqual(try Yams.dump(object: NSDictionary()), "{}\n")
}
}

extension RepresenterTests {
Expand All @@ -149,7 +153,8 @@ extension RepresenterTests {
("testUUID", testUUID),
("testOptional", testOptional),
("testArray", testArray),
("testDictionary", testDictionary)
("testDictionary", testDictionary),
("testEmptyDictionary", testEmptyDictionary)
]
}
}

0 comments on commit a34f950

Please sign in to comment.