Skip to content

Commit

Permalink
Fix: Namespaces Passed As Hashes Not Being Transformed Properly
Browse files Browse the repository at this point in the history
Why This Change Is Necessary
========================================================================

When a namespace was being passed in as a Hash (as is the default for
our Rails integration), the namespaces weren't being translated to an
array of namespaces, which is what the rest of the library expects.

This was working correctly for the CLI commands because they rely on the
fact that the system will pass through default values, but inside of the
server or the console, this was not working correctly.

Additionally, if a lambda or proc is passed in as a value, it was not
being `call`ed and therefore the value was not resolving.

How These Changes Address the Issue
========================================================================

We now convert hashes passed to the ContextResolver into an array of the
hash's values as well as call procs and lambdas if they are passed in.

Side Effects Caused By This Change
========================================================================

None known.

------------------------------------------------------------------------
Actions:
  * References #58
  • Loading branch information
jfelchner committed May 29, 2019
1 parent 4d383a7 commit bc074c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/chamber/context_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def resolve
options[:basepath] ||= options[:rootpath]
end

options[:namespaces] = resolve_namespaces(options[:namespaces])
options[:encryption_keys] = Keys::Encryption.resolve(filenames: options[:encryption_keys],
namespaces: options[:namespaces],
rootpath: options[:rootpath])
Expand All @@ -52,6 +53,13 @@ def self.resolve(options = {})

protected

def resolve_namespaces(other)
(other.respond_to?(:values) ? other.values : other)
.map do |namespace|
namespace.respond_to?(:call) ? namespace.call : namespace
end
end

def resolve_preset
if in_a_rails_project?
'rails'
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/chamber/context_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ module Commands
expect(options[:decryption_keys]).to eql({})
end

it 'unfurls namespace hashes if they are passed in' do
options = ContextResolver.resolve(namespaces: {
environment: 'foo',
hostname: 'bar',
})

expect(options[:namespaces]).to eql %w{foo bar}
end

it 'processing lambdas and procs if they are passed in as namespaces' do
options = ContextResolver.resolve(namespaces: {
environment: -> { 'foo' },
hostname: -> { 'bar' },
})

expect(options[:namespaces]).to eql %w{foo bar}
end

it 'sets the information to a Rails preset even if it is not pointing to a Rails app' do
options = ContextResolver.resolve(rootpath: './app',
preset: 'rails')
Expand Down

0 comments on commit bc074c8

Please sign in to comment.