diff --git a/Sources/ProjectSpec/Spec.swift b/Sources/ProjectSpec/Spec.swift index abff04d8..8852558a 100644 --- a/Sources/ProjectSpec/Spec.swift +++ b/Sources/ProjectSpec/Spec.swift @@ -88,41 +88,6 @@ extension Spec { } } -extension Array where Element == PathProperty { - func resolvingPaths(in jsonDictionary: JSONDictionary, relativeTo path: Path) -> JSONDictionary { - var result = jsonDictionary - - for pathProperty in self { - switch pathProperty { - case .string(let key): - if let source = result[key] as? String { - result[key] = (path + source).string - } else if let source = result[key] as? [String] { - result[key] = source.map { (path + $0).string } - } else if let source = result[key] as? [String: String] { - result[key] = source.mapValues { (path + $0).string } - } - case .dictionary(let pathProperties): - for (key, dictionary) in result { - if let source = dictionary as? JSONDictionary { - result[key] = pathProperties.resolvingPaths(in: source, relativeTo: path) - } - } - case .object(let key, let pathProperties): - if let source = result[key] as? JSONDictionary { - result[key] = pathProperties.resolvingPaths(in: source, relativeTo: path) - } else if let source = result[key] as? [JSONDictionary] { - result[key] = source.map { pathProperties.resolvingPaths(in: $0, relativeTo: path) } - } else if let source = result[key] as? [String: JSONDictionary] { - result[key] = source.mapValues { pathProperties.resolvingPaths(in: $0, relativeTo: path) } - } - } - } - - return result - } -} - extension Dictionary where Key == String, Value: Any { func merged(onto other: Dictionary) -> Dictionary { diff --git a/Sources/ProjectSpec/SpecLoader.swift b/Sources/ProjectSpec/SpecLoader.swift index d8fbd374..06f2523b 100644 --- a/Sources/ProjectSpec/SpecLoader.swift +++ b/Sources/ProjectSpec/SpecLoader.swift @@ -26,3 +26,38 @@ enum PathProperty { case dictionary([PathProperty]) case object(String, [PathProperty]) } + +extension Array where Element == PathProperty { + func resolvingPaths(in jsonDictionary: JSONDictionary, relativeTo path: Path) -> JSONDictionary { + var result = jsonDictionary + + for pathProperty in self { + switch pathProperty { + case .string(let key): + if let source = result[key] as? String { + result[key] = (path + source).string + } else if let source = result[key] as? [String] { + result[key] = source.map { (path + $0).string } + } else if let source = result[key] as? [String: String] { + result[key] = source.mapValues { (path + $0).string } + } + case .dictionary(let pathProperties): + for (key, dictionary) in result { + if let source = dictionary as? JSONDictionary { + result[key] = pathProperties.resolvingPaths(in: source, relativeTo: path) + } + } + case .object(let key, let pathProperties): + if let source = result[key] as? JSONDictionary { + result[key] = pathProperties.resolvingPaths(in: source, relativeTo: path) + } else if let source = result[key] as? [JSONDictionary] { + result[key] = source.map { pathProperties.resolvingPaths(in: $0, relativeTo: path) } + } else if let source = result[key] as? [String: JSONDictionary] { + result[key] = source.mapValues { pathProperties.resolvingPaths(in: $0, relativeTo: path) } + } + } + } + + return result + } +}