mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
74 lines
2.0 KiB
Swift
74 lines
2.0 KiB
Swift
import Foundation
|
|
import Yams
|
|
|
|
// MARK: - YamlParsingError
|
|
|
|
internal enum YamlParserError: Error, Equatable {
|
|
case yamlParsing(String)
|
|
}
|
|
|
|
internal func == (lhs: YamlParserError, rhs: YamlParserError) -> Bool {
|
|
switch (lhs, rhs) {
|
|
case let (.yamlParsing(lhs), .yamlParsing(rhs)):
|
|
return lhs == rhs
|
|
}
|
|
}
|
|
|
|
// MARK: - YamlParser
|
|
|
|
public struct YamlParser {
|
|
public static func parse(_ yaml: String,
|
|
env: [String: String] = ProcessInfo.processInfo.environment) throws -> [String: Any] {
|
|
do {
|
|
return try Yams.load(yaml: yaml, .default,
|
|
.swiftlintContructor(env: env)) as? [String: Any] ?? [:]
|
|
} catch {
|
|
throw YamlParserError.yamlParsing("\(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension Constructor {
|
|
static func swiftlintContructor(env: [String: String]) -> Constructor {
|
|
return Constructor(customScalarMap(env: env))
|
|
}
|
|
|
|
static func customScalarMap(env: [String: String]) -> ScalarMap {
|
|
var map = defaultScalarMap
|
|
map[.str] = String.constructExpandingEnvVars(env: env)
|
|
map[.bool] = Bool.constructUsingOnlyTrueAndFalse
|
|
|
|
return map
|
|
}
|
|
}
|
|
|
|
private extension String {
|
|
static func constructExpandingEnvVars(env: [String: String]) -> (_ scalar: Node.Scalar) -> String? {
|
|
return { (scalar: Node.Scalar) -> String? in
|
|
return scalar.string.expandingEnvVars(env: env)
|
|
}
|
|
}
|
|
|
|
func expandingEnvVars(env: [String: String]) -> String {
|
|
var result = self
|
|
for (key, value) in env {
|
|
result = result.replacingOccurrences(of: "${\(key)}", with: value)
|
|
}
|
|
|
|
return result
|
|
}
|
|
}
|
|
|
|
private extension Bool {
|
|
static func constructUsingOnlyTrueAndFalse(from scalar: Node.Scalar) -> Bool? {
|
|
switch scalar.string.lowercased() {
|
|
case "true":
|
|
return true
|
|
case "false":
|
|
return false
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|