Skip to content

Commit

Permalink
MR feedback: allow for any node as a mapping key
Browse files Browse the repository at this point in the history
  • Loading branch information
tejassharma96 committed Nov 19, 2024
1 parent bc0d5b4 commit b27226b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions Sources/Yams/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions Sources/Yams/YamlError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b27226b

Please sign in to comment.