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 1 commit
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
14 changes: 12 additions & 2 deletions Sources/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rename those two static let constants using environmentKey and parametersKey.

Because the first time i read and reviewed your code change I didn't understand why you used instance properties there, and almost thought that self.environment were referring to the [String: String] dictionary of the context, containing the env variables (ok, doing the review from GitHub didn't help, as the limited git diff scope failed to remind me that this was actually inside a static func…). Using context[self.paramtersKey] should help clarify that.


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
}
}