Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed dump function parameter requirements #320

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Main

##### Breaking
- The `dump` functions now requires `NodeRepresentable` values rather than `Any?` (or any `Sequence`) [#299]

## 4.0.6

##### Breaking
Expand Down
24 changes: 9 additions & 15 deletions Sources/Yams/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import Foundation

/// Produce a YAML string from objects.
///
/// - parameter objects: Sequence of Objects.
/// - parameter objects: Sequence of `NodeRepresentable` values.
/// - parameter canonical: Output should be the "canonical" format as in the YAML specification.
/// - parameter indent: The intendation increment.
/// - parameter indent: The indentation increment.
/// - parameter width: The preferred line width. @c -1 means unlimited.
/// - parameter allowUnicode: Unescaped non-ASCII characters are allowed if true.
/// - parameter lineBreak: Preferred line break.
Expand All @@ -38,14 +38,8 @@ public func dump<Objects>(
explicitEnd: Bool = false,
version: (major: Int, minor: Int)? = nil,
sortKeys: Bool = false) throws -> String
where Objects: Sequence {
func representable(from object: Any) throws -> NodeRepresentable {
if let representable = object as? NodeRepresentable {
return representable
}
throw YamlError.emitter(problem: "\(object) does not conform to NodeRepresentable!")
}
let nodes = try objects.map(representable(from:)).map { try $0.represented() }
where Objects: Sequence, Objects.Element: NodeRepresentable {
let nodes = try objects.map { try $0.represented() }
return try serialize(
nodes: nodes,
canonical: canonical,
Expand All @@ -62,9 +56,9 @@ public func dump<Objects>(

/// Produce a YAML string from an object.
///
/// - parameter object: Object.
/// - parameter object: The object to dump. Should conform to `NodeRepresentable`
/// - parameter canonical: Output should be the "canonical" format as in the YAML specification.
/// - parameter indent: The intendation increment.
/// - parameter indent: The indentation increment.
/// - parameter width: The preferred line width. @c -1 means unlimited.
/// - parameter allowUnicode: Unescaped non-ASCII characters are allowed if true.
/// - parameter lineBreak: Preferred line break.
Expand All @@ -77,7 +71,7 @@ public func dump<Objects>(
///
/// - throws: `YamlError`.
public func dump(
object: Any?,
object: NodeRepresentable,
canonical: Bool = false,
indent: Int = 0,
width: Int = 0,
Expand Down Expand Up @@ -105,7 +99,7 @@ public func dump(
///
/// - parameter nodes: Sequence of `Node`s.
/// - parameter canonical: Output should be the "canonical" format as in the YAML specification.
/// - parameter indent: The intendation increment.
/// - parameter indent: The indentation increment.
/// - parameter width: The preferred line width. @c -1 means unlimited.
/// - parameter allowUnicode: Unescaped non-ASCII characters are allowed if true.
/// - parameter lineBreak: Preferred line break.
Expand Down Expand Up @@ -150,7 +144,7 @@ public func serialize<Nodes>(
///
/// - parameter node: `Node`.
/// - parameter canonical: Output should be the "canonical" format as in the YAML specification.
/// - parameter indent: The intendation increment.
/// - parameter indent: The indentation increment.
/// - parameter width: The preferred line width. @c -1 means unlimited.
/// - parameter allowUnicode: Unescaped non-ASCII characters are allowed if true.
/// - parameter lineBreak: Preferred line break.
Expand Down
18 changes: 18 additions & 0 deletions Sources/Yams/Representer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ extension Array: NodeRepresentable {
}
}

/// Useful when dealing with data serialized with JSONSerialization or PropertyListSerialization
extension NSArray: NodeRepresentable {
/// This value's `Node` representation.
public func represented() throws -> Node {
let nodes = try map(represent)
return Node(nodes, Tag(.seq))
}
}

extension Dictionary: NodeRepresentable {
/// This value's `Node` representation.
public func represented() throws -> Node {
Expand All @@ -61,6 +70,15 @@ extension Dictionary: NodeRepresentable {
}
}

/// Useful when dealing with data serialized with JSONSerialization or PropertyListSerialization
extension NSDictionary: NodeRepresentable {
/// This value's `Node` representation.
public func represented() throws -> Node {
let pairs = try map { (key: try represent($0.0), value: try represent($0.1)) }
return Node(pairs.sorted { $0.key < $1.key }, Tag(.map))
}
}

private func represent(_ value: Any) throws -> Node {
if let representable = value as? NodeRepresentable {
return try representable.represented()
Expand Down