Skip to content

Commit

Permalink
Ensure that the context enrich function doesn't overwrite existing va…
Browse files Browse the repository at this point in the history
…riables (#29)
  • Loading branch information
djbe authored Mar 25, 2017
1 parent 73b0114 commit 71c7a25
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 14 additions & 4 deletions Sources/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
12 changes: 6 additions & 6 deletions Tests/StencilSwiftKitTests/ContextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 71c7a25

Please sign in to comment.