Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that the context enrich function doesn't overwrite existing variables #29

Merged
merged 2 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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