From b27226b7c4127dd1f37f0c8d38b4b5e05380336e Mon Sep 17 00:00:00 2001 From: Tejas Sharma Date: Mon, 18 Nov 2024 18:01:30 -0800 Subject: [PATCH] MR feedback: allow for any node as a mapping key --- Sources/Yams/Parser.swift | 9 ++++----- Sources/Yams/YamlError.swift | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Sources/Yams/Parser.swift b/Sources/Yams/Parser.swift index 48ad3fa1..3759301a 100644 --- a/Sources/Yams/Parser.swift +++ b/Sources/Yams/Parser.swift @@ -365,13 +365,12 @@ private extension Parser { } private func checkDuplicates(mappingKeys: [Node]) throws { - var duplicates: [String: [Node]] = [:] + var duplicates: [Node: [Node]] = [:] for key in mappingKeys { - guard let keyString = key.string else { continue } - if duplicates.keys.contains(keyString) { - duplicates[keyString]?.append(key) + if duplicates.keys.contains(key) { + duplicates[key]?.append(key) } else { - duplicates[keyString] = [key] + duplicates[key] = [key] } } duplicates = duplicates.filter { $1.count > 1 } diff --git a/Sources/Yams/YamlError.swift b/Sources/Yams/YamlError.swift index 00d34697..eeff804a 100644 --- a/Sources/Yams/YamlError.swift +++ b/Sources/Yams/YamlError.swift @@ -79,7 +79,7 @@ public enum YamlError: Error { /// /// - parameter duplicates: A dictionary keyed by the duplicated node value, with all nodes that duplicate the value /// - parameter yaml: YAML String which the problem occured while reading. - case duplicatedKeysInMapping(duplicates: [String: [Node]], yaml: String) + case duplicatedKeysInMapping(duplicates: [Node: [Node]], yaml: String) /// The error context. public struct Context: CustomStringConvertible { @@ -187,13 +187,13 @@ extension YamlError: CustomStringConvertible { } } -private extension Dictionary where Key == String, Value == [Node] { +private extension Dictionary where Key == Node, Value == [Node] { func duplicatedKeyErrorDescription(yaml: String) -> String { var error = "error: parser: expected all keys to be unique but found the following duplicated key(s):" for key in self.keys.sorted() { let duplicatedNodes = self[key]! let marks = duplicatedNodes.compactMap { $0.mark } - error += "\n\(key) (\(marks)):\n\(marks.map { $0.snippet(from: yaml) }.joined(separator: "\n"))" + error += "\n\(key.any) (\(marks)):\n\(marks.map { $0.snippet(from: yaml) }.joined(separator: "\n"))" } return error }