From d6336e1389747d2db5f00cdd000b59b61b7d31c9 Mon Sep 17 00:00:00 2001 From: Manjunath Date: Wed, 6 Oct 2021 23:17:30 +0530 Subject: [PATCH 1/4] Allow symbol value via ENV --- README.md | 4 +++- lib/config/sources/env_source.rb | 9 ++++----- spec/config_env_spec.rb | 5 +++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fea1309d..badaad59 100644 --- a/README.md +++ b/README.md @@ -453,7 +453,7 @@ You can customize how environment variables are processed: * `env_converter` (default: `:downcase`) - how to process variables names: * `nil` - no change * `:downcase` - convert to lower case -* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Boolean`, `Integer`, `Float`, `String`) +* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Boolean`, `Integer`, `Float`, `String` & `Symbol`) For instance, given the following environment: @@ -461,6 +461,7 @@ For instance, given the following environment: SETTINGS__SECTION__SERVER_SIZE=1 SETTINGS__SECTION__SERVER=google.com SETTINGS__SECTION__SSL_ENABLED=false +SETTINGS__SECTION__SERVER_ACCESS=:remote ``` And the following configuration: @@ -481,6 +482,7 @@ The following settings will be available: Settings.section.server_size # => 1 Settings.section.server # => 'google.com' Settings.section.ssl_enabled # => false +Settings.section.server_access # => :remote ``` ### Working with AWS Secrets Manager diff --git a/lib/config/sources/env_source.rb b/lib/config/sources/env_source.rb index df59e35e..fdbd8e4f 100644 --- a/lib/config/sources/env_source.rb +++ b/lib/config/sources/env_source.rb @@ -59,11 +59,10 @@ def load # Try to convert string to a correct type def __value(v) - case v - when 'false' - false - when 'true' - true + if %w(true false).include? v + eval(v) + elsif v.squish.start_with?(':') + v.parameterize.underscore.to_sym else Integer(v) rescue Float(v) rescue v end diff --git a/spec/config_env_spec.rb b/spec/config_env_spec.rb index 65e7da8b..4656b2df 100644 --- a/spec/config_env_spec.rb +++ b/spec/config_env_spec.rb @@ -77,6 +77,11 @@ expect(config.new_var.is_a? Float).to eq(true) end + it 'should recognize strings starting with : as symbols' do + ENV['Settings.new_var'] = ':hello' + expect(config.new_var).to eq(:hello) + end + it 'should leave strings intact' do ENV['Settings.new_var'] = 'foobar' From aa76ea95c2cacbb4a413eabc9bf08cf435c6c248 Mon Sep 17 00:00:00 2001 From: Manjunath Date: Thu, 7 Oct 2021 11:09:33 +0530 Subject: [PATCH 2/4] Let strings starting with :: intact --- README.md | 6 ++++-- lib/config/sources/env_source.rb | 2 +- spec/config_env_spec.rb | 9 +++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index badaad59..9f2db896 100644 --- a/README.md +++ b/README.md @@ -461,7 +461,8 @@ For instance, given the following environment: SETTINGS__SECTION__SERVER_SIZE=1 SETTINGS__SECTION__SERVER=google.com SETTINGS__SECTION__SSL_ENABLED=false -SETTINGS__SECTION__SERVER_ACCESS=:remote +SETTINGS__SECTION__SERVER_TYPE=:remote +SETTINGS__SECTION__SERVER_ACCESS=::ENABLED ``` And the following configuration: @@ -482,7 +483,8 @@ The following settings will be available: Settings.section.server_size # => 1 Settings.section.server # => 'google.com' Settings.section.ssl_enabled # => false -Settings.section.server_access # => :remote +Settings.section.server_type # => :remote +Settings.section.server_access # => '::ENABLED' ``` ### Working with AWS Secrets Manager diff --git a/lib/config/sources/env_source.rb b/lib/config/sources/env_source.rb index fdbd8e4f..687224fd 100644 --- a/lib/config/sources/env_source.rb +++ b/lib/config/sources/env_source.rb @@ -61,7 +61,7 @@ def load def __value(v) if %w(true false).include? v eval(v) - elsif v.squish.start_with?(':') + elsif v.squish =~ /^:[^:]/ v.parameterize.underscore.to_sym else Integer(v) rescue Float(v) rescue v diff --git a/spec/config_env_spec.rb b/spec/config_env_spec.rb index 4656b2df..7b160de2 100644 --- a/spec/config_env_spec.rb +++ b/spec/config_env_spec.rb @@ -78,8 +78,13 @@ end it 'should recognize strings starting with : as symbols' do - ENV['Settings.new_var'] = ':hello' - expect(config.new_var).to eq(:hello) + ENV['Settings.new_var'] = ':remote' + expect(config.new_var).to eq(:remote) + end + + it 'should leave strings starting with :: intact' do + ENV['Settings.new_var'] = '::ENABLED' + expect(config.new_var).to eq('::ENABLED') end it 'should leave strings intact' do From 01d47967e75eb8ad87f3df3f09340b080c31f669 Mon Sep 17 00:00:00 2001 From: Manjunath Date: Thu, 7 Oct 2021 22:58:40 +0530 Subject: [PATCH 3/4] Replace .squish with .strip --- lib/config/sources/env_source.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config/sources/env_source.rb b/lib/config/sources/env_source.rb index 687224fd..ca81feb4 100644 --- a/lib/config/sources/env_source.rb +++ b/lib/config/sources/env_source.rb @@ -61,7 +61,7 @@ def load def __value(v) if %w(true false).include? v eval(v) - elsif v.squish =~ /^:[^:]/ + elsif v.strip =~ /^:[^:]/ v.parameterize.underscore.to_sym else Integer(v) rescue Float(v) rescue v From efb69a6f12a9d4d7244d9e0606263716aa3f15c1 Mon Sep 17 00:00:00 2001 From: Manjunath Date: Sun, 10 Oct 2021 19:27:45 +0530 Subject: [PATCH 4/4] Covert string to symbol --- lib/config/sources/env_source.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/config/sources/env_source.rb b/lib/config/sources/env_source.rb index ca81feb4..c03d6cf1 100644 --- a/lib/config/sources/env_source.rb +++ b/lib/config/sources/env_source.rb @@ -62,11 +62,22 @@ def __value(v) if %w(true false).include? v eval(v) elsif v.strip =~ /^:[^:]/ - v.parameterize.underscore.to_sym + convert_str_to_symbol(v) else Integer(v) rescue Float(v) rescue v end end + + # Remove all special characters from a string before converting into a symbol + def convert_str_to_symbol(str) + str. + gsub(/[^a-z0-9\-_]+/i, "-"). + gsub(/-{2,}/, "-"). + gsub(/^-|-$/i, ""). + tr("-", "_"). + downcase. + to_sym + end end end end