From 34d7af512be9045006b37bf1b97478e5a0675aa5 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Sun, 5 Mar 2017 15:55:51 +0100 Subject: [PATCH 1/2] Ensure that the context enrich function doesn't overwrite existing variables --- CHANGELOG.md | 4 +++- Sources/Context.swift | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) 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..f1859178 100644 --- a/Sources/Context.swift +++ b/Sources/Context.swift @@ -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[self.environment] = merge(context[self.environment], with: environment) + context[self.parameters] = merge(context[self.parameters], 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 + } } From 03cf0715a7b87356e2aa6df8d32b3431b8abefdd Mon Sep 17 00:00:00 2001 From: David Jennes Date: Sat, 25 Mar 2017 16:20:24 +0100 Subject: [PATCH 2/2] Rename constants to be clearer --- Sources/Context.swift | 8 ++++---- Tests/StencilSwiftKitTests/ContextTests.swift | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/Context.swift b/Sources/Context.swift index f1859178..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,8 +24,8 @@ public enum StencilContext { environment: [String: String] = ProcessInfo().environment) throws -> [String: Any] { var context = context - context[self.environment] = merge(context[self.environment], with: environment) - context[self.parameters] = merge(context[self.parameters], with: 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 } 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 }