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
  • Loading branch information
djbe committed Mar 10, 2017
1 parent 73b0114 commit 34d7af5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 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
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))

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

0 comments on commit 34d7af5

Please sign in to comment.