diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d1f7bed..77b90ffb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ ### Bug Fixes -_None_ +* The context enrich function won't overwrite existing values in the `env` and `param` variables. + [David Jennes](https://github.com/djbe) + [#29](https://github.com/SwiftGen/SwiftGenKit/issues/29) ### Breaking Changes diff --git a/Sources/Context.swift b/Sources/Context.swift index b26d34ad..8791e04c 100644 --- a/Sources/Context.swift +++ b/Sources/Context.swift @@ -9,8 +9,8 @@ import Foundation public enum StencilContext { - public static let environment = "env" - public static let parameters = "param" + public static let environmentKey = "env" + public static let parametersKey = "param" /** Enriches a stencil context with parsed parameters and environment variables @@ -24,9 +24,19 @@ public enum StencilContext { environment: [String: String] = ProcessInfo().environment) throws -> [String: Any] { var context = context - context[StencilContext.environment] = environment - context[StencilContext.parameters] = try Parameters.parse(items: parameters) + context[environmentKey] = merge(context[environmentKey], with: environment) + context[parametersKey] = merge(context[parametersKey], with: try Parameters.parse(items: parameters)) return context } + + private static func merge(_ lhs: Any?, with rhs: [String: Any]) -> [String: Any] { + var result = lhs as? [String: Any] ?? [:] + + for (key, value) in rhs { + result[key] = value + } + + return result + } } diff --git a/Tests/StencilSwiftKitTests/ContextTests.swift b/Tests/StencilSwiftKitTests/ContextTests.swift index 82b21953..a80cf65c 100644 --- a/Tests/StencilSwiftKitTests/ContextTests.swift +++ b/Tests/StencilSwiftKitTests/ContextTests.swift @@ -18,13 +18,13 @@ class ContextTests: XCTestCase { environment: ["PATH": "foo:bar:baz"]) XCTAssertEqual(result.count, 2, "2 items have been added") - guard let env = result[StencilContext.environment] as? [String: Any] else { + guard let env = result[StencilContext.environmentKey] as? [String: Any] else { XCTFail("`env` should be a dictionary") return } XCTAssertEqual(env["PATH"] as? String, "foo:bar:baz") - guard let params = result[StencilContext.parameters] as? [String: Any] else { + guard let params = result[StencilContext.parametersKey] as? [String: Any] else { XCTFail("`param` should be a dictionary") return } @@ -41,13 +41,13 @@ class ContextTests: XCTestCase { XCTAssertEqual(result["foo"] as? String, "bar") XCTAssertEqual(result["hello"] as? Bool, true) - guard let env = result[StencilContext.environment] as? [String: Any] else { + guard let env = result[StencilContext.environmentKey] as? [String: Any] else { XCTFail("`env` should be a dictionary") return } XCTAssertEqual(env["PATH"] as? String, "foo:bar:baz") - guard let params = result[StencilContext.parameters] as? [String: Any] else { + guard let params = result[StencilContext.parametersKey] as? [String: Any] else { XCTFail("`param` should be a dictionary") return } @@ -62,13 +62,13 @@ class ContextTests: XCTestCase { environment: ["PATH": "foo:bar:baz"]) XCTAssertEqual(result.count, 2, "2 items have been added") - guard let env = result[StencilContext.environment] as? [String: Any] else { + guard let env = result[StencilContext.environmentKey] as? [String: Any] else { XCTFail("`env` should be a dictionary") return } XCTAssertEqual(env["PATH"] as? String, "foo:bar:baz") - guard let params = result[StencilContext.parameters] as? [String: Any] else { + guard let params = result[StencilContext.parametersKey] as? [String: Any] else { XCTFail("`param` should be a dictionary") return }