Skip to content

Commit

Permalink
Add smart quoting to String.represented()
Browse files Browse the repository at this point in the history
Apply `.singleQuoted` on representing `Node.Scalar` from `String` if `Resolver.default` resolves that to other than `.str`.
Fixes #197
  • Loading branch information
norio-nomura committed Jul 6, 2019
1 parent 1bc7a6b commit c8f31af
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Sources/Yams/Representer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ extension Dictionary: NodeRepresentable {
}

private func represent(_ value: Any) throws -> Node {
if let string = value as? String {
return Node(string)
} else if let representable = value as? NodeRepresentable {
if let representable = value as? NodeRepresentable {
return try representable.represented()
}
throw YamlError.representer(problem: "Failed to represent \(value)")
Expand Down Expand Up @@ -230,7 +228,8 @@ extension URL: ScalarRepresentable {
extension String: ScalarRepresentable {
/// This value's `Node.scalar` representation.
public func represented() -> Node.Scalar {
return .init(self)
let node = Node.Scalar.init(self, .init(.implicit, .default))
return node.resolvedTag.name == .str ? node : .init(self, .init(.str), .singleQuoted)
}
}

Expand Down

0 comments on commit c8f31af

Please sign in to comment.