From ba6448d08429439d9bc7b74ef307b69dcd901335 Mon Sep 17 00:00:00 2001 From: Alexis Bridoux Date: Sat, 15 May 2021 21:54:07 +0200 Subject: [PATCH 1/2] Changed dump function parameter requirements The dump functions now require a NodeRepresentable value to be passed rather than Any. This makes the caller responsible for ensuring the provided value can be represented by a node, while default implementations for common types is provided. Also fixed some typos. --- CHANGELOG.md | 5 +++++ Sources/Yams/Emitter.swift | 24 +++++++++--------------- Sources/Yams/Representer.swift | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b95e89..7aa75e78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 4.0.7 + +##### Breaking +- The `dump` functions now requires `NodeRepresentable` values rather than `Any?` (or any `Sequence`) [#299] + ## 4.0.6 ##### Breaking diff --git a/Sources/Yams/Emitter.swift b/Sources/Yams/Emitter.swift index e7c4f5f6..bb83adaa 100644 --- a/Sources/Yams/Emitter.swift +++ b/Sources/Yams/Emitter.swift @@ -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. @@ -38,14 +38,8 @@ public func dump( 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, @@ -62,9 +56,9 @@ public func dump( /// 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. @@ -77,7 +71,7 @@ public func dump( /// /// - throws: `YamlError`. public func dump( - object: Any?, + object: NodeRepresentable, canonical: Bool = false, indent: Int = 0, width: Int = 0, @@ -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. @@ -150,7 +144,7 @@ public func serialize( /// /// - 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. diff --git a/Sources/Yams/Representer.swift b/Sources/Yams/Representer.swift index b4ff73c1..31a351a1 100644 --- a/Sources/Yams/Representer.swift +++ b/Sources/Yams/Representer.swift @@ -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 { @@ -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() From fe6abb255927f8ecef9043c0954b90dd1b7917e4 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Mon, 24 May 2021 06:34:13 -0400 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7aa75e78..1bc55785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 4.0.7 +## Main ##### Breaking - The `dump` functions now requires `NodeRepresentable` values rather than `Any?` (or any `Sequence`) [#299]