-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
Refactor environment variable handling into new Sources::EnvSource #299
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a509826
Extract env var handling to new EnvSource class
cjlarose b483bad
Allow overriding settings for parsing env sources
cjlarose 09d0c00
Update CHANGELOG
cjlarose 7bfb952
Add AWS Secrets Manager usage to README
cjlarose c77ce2e
Merge branch 'master' into env-source
cjlarose File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module Config | ||
module Sources | ||
# Allows settings to be loaded from a "flat" hash with string keys, like ENV. | ||
class EnvSource | ||
attr_reader :prefix | ||
attr_reader :separator | ||
attr_reader :converter | ||
attr_reader :parse_values | ||
|
||
def initialize(env, | ||
prefix: Config.env_prefix || Config.const_name, | ||
separator: Config.env_separator, | ||
converter: Config.env_converter, | ||
parse_values: Config.env_parse_values) | ||
@env = env | ||
@prefix = prefix.to_s.split(separator) | ||
@separator = separator | ||
@converter = converter | ||
@parse_values = parse_values | ||
end | ||
|
||
def load | ||
return {} if @env.nil? || @env.empty? | ||
|
||
hash = Hash.new | ||
|
||
@env.each do |variable, value| | ||
keys = variable.to_s.split(separator) | ||
|
||
next if keys.shift(prefix.size) != prefix | ||
|
||
keys.map! { |key| | ||
case converter | ||
when :downcase then | ||
key.downcase | ||
when nil then | ||
key | ||
else | ||
raise "Invalid ENV variables name converter: #{converter}" | ||
end | ||
} | ||
|
||
leaf = keys[0...-1].inject(hash) { |h, key| | ||
h[key] ||= {} | ||
} | ||
|
||
unless leaf.is_a?(Hash) | ||
conflicting_key = (prefix + keys[0...-1]).join(separator) | ||
raise "Environment variable #{variable} conflicts with variable #{conflicting_key}" | ||
end | ||
|
||
leaf[keys.last] = parse_values ? __value(value) : value | ||
end | ||
|
||
hash | ||
end | ||
|
||
private | ||
|
||
# Try to convert string to a correct type | ||
def __value(v) | ||
case v | ||
when 'false' | ||
false | ||
when 'true' | ||
true | ||
else | ||
Integer(v) rescue Float(v) rescue v | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'spec_helper' | ||
|
||
module Config::Sources | ||
describe EnvSource do | ||
context 'configuration options' do | ||
before :each do | ||
Config.reset | ||
Config.env_prefix = nil | ||
Config.env_separator = '.' | ||
Config.env_converter = :downcase | ||
Config.env_parse_values = true | ||
end | ||
|
||
context 'default configuration' do | ||
it 'should use global prefix configuration by default' do | ||
Config.env_prefix = 'MY_CONFIG' | ||
|
||
source = EnvSource.new({ 'MY_CONFIG.ACTION_MAILER' => 'enabled' }) | ||
results = source.load | ||
expect(results['action_mailer']).to eq('enabled') | ||
end | ||
|
||
it 'should use global separator configuration by default' do | ||
Config.env_separator = '__' | ||
|
||
source = EnvSource.new({ 'Settings__ACTION_MAILER__ENABLED' => 'yes' }) | ||
results = source.load | ||
expect(results['action_mailer']['enabled']).to eq('yes') | ||
end | ||
|
||
it 'should use global converter configuration by default' do | ||
Config.env_converter = nil | ||
|
||
source = EnvSource.new({ 'Settings.ActionMailer.Enabled' => 'yes' }) | ||
results = source.load | ||
expect(results['ActionMailer']['Enabled']).to eq('yes') | ||
end | ||
|
||
it 'should use global parse_values configuration by default' do | ||
Config.env_parse_values = false | ||
|
||
source = EnvSource.new({ 'Settings.ACTION_MAILER.ENABLED' => 'true' }) | ||
results = source.load | ||
expect(results['action_mailer']['enabled']).to eq('true') | ||
end | ||
end | ||
|
||
context 'configuration overrides' do | ||
it 'should allow overriding prefix configuration' do | ||
source = EnvSource.new({ 'MY_CONFIG.ACTION_MAILER' => 'enabled' }, | ||
prefix: 'MY_CONFIG') | ||
results = source.load | ||
expect(results['action_mailer']).to eq('enabled') | ||
end | ||
|
||
it 'should allow overriding separator configuration' do | ||
source = EnvSource.new({ 'Settings__ACTION_MAILER__ENABLED' => 'yes' }, | ||
separator: '__') | ||
results = source.load | ||
expect(results['action_mailer']['enabled']).to eq('yes') | ||
end | ||
|
||
it 'should allow overriding converter configuration' do | ||
source = EnvSource.new({ 'Settings.ActionMailer.Enabled' => 'yes' }, | ||
converter: nil) | ||
results = source.load | ||
expect(results['ActionMailer']['Enabled']).to eq('yes') | ||
end | ||
|
||
it 'should allow overriding parse_values configuration' do | ||
source = EnvSource.new({ 'Settings.ACTION_MAILER.ENABLED' => 'true' }, | ||
parse_values: false) | ||
results = source.load | ||
expect(results['action_mailer']['enabled']).to eq('true') | ||
end | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pkuczynski
Config::Options#load_env!
andConfig::Options#reload_env!
are not documented at all, but are technicallypublic
methods.As such, depending on whether or not we consider all visible methods to be part of the public API of this gem, this could be considered a breaking change and warrants a major version bump, so I added this under
Breaking Changes
in the changelog.The change for users that use
#load_env!
or#reload_env!
is simple: they should just replace those usages with#reload!
, which is documented.