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

Allow symbol value via ENV #286

Open
khoan opened this issue Nov 9, 2020 · 5 comments
Open

Allow symbol value via ENV #286

khoan opened this issue Nov 9, 2020 · 5 comments

Comments

@khoan
Copy link

khoan commented Nov 9, 2020

Setup

cd /path/to/rails/app/src
cat config/initializers/config.rb
Config.setup do |config|
  config.const_name = 'Settings'
  config.use_env = true
  config.env_prefix = 'SETTINGS'
  config.env_separator = '__'
  config.env_converter = :downcase
  config.env_parse_values = true
end
cat config/settings/test.yml
provider: :bam
SETTINGS__PROVIDER=:bam RAILS_ENV=test bin/rails c
Settings.provider # => ":bam"   want symbol instead of string
RAILS_ENV=test bin/rails c
Settings.provider # => :bam  got symbol as expected

maybe related to issue#194? Thanks.

@pkuczynski
Copy link
Member

I don't think its related to #194. They actually wanted something opposite to your case.

Env vars are string and I don't think we should force conversion to symbols. However we could implement a logic where strings staring from : would be converted to symbols (see: https://stackoverflow.com/questions/2004491/convert-string-to-symbol-able-in-ruby)

If you are interested, maybe you can fire up a PR?

@pkuczynski pkuczynski changed the title allow symbol value via ENV Allow symbol value via ENV Nov 9, 2020
@pkuczynski
Copy link
Member

@khoan are you interested in firing up PR for this?

@manjunath724
Copy link

@pkuczynski

I always get Symbol :bam for the above configuration. I am executing this on rails-6.1.4 & ruby-3.0.2. Can you please help me replicate? I'd be more than happy to create a PR.

@cjlarose
Copy link
Member

cjlarose commented Oct 5, 2021

@manjunath724 If I understand the feature request correctly, this can be replicated with the following spec:

diff --git a/spec/config_env_spec.rb b/spec/config_env_spec.rb
index 65e7da8..5f91def 100644
--- a/spec/config_env_spec.rb
+++ b/spec/config_env_spec.rb
@@ -77,6 +77,12 @@ describe Config::Options do
           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'
 

Execute

bundle install
bundle exec appraisal install
bundle exec appraisal rails-6.1 rspec spec/config_env_spec.rb

And you'll get this result

Bundler version 2.1.4
Skipping rails-4.2 for Bundler >= 2.x
Bundler version 2.1.4
Skipping rails-4.2 for Bundler >= 2.x
>> BUNDLE_GEMFILE=/Users/chris.larose/workspace/rubyconfig/config/gemfiles/rails_6.1.gemfile bundle exec rspec spec/config_env_spec.rb
RUBY_ENGINE: ruby
RUBY_VERSION: 2.7.0


Gemfile: /Users/chris.larose/workspace/rubyconfig/config/gemfiles/rails_6.1.gemfile
Version:
	rails-6.1.4
	rspec-rails-5.0.1
	sqlite3-1.4.2
	psych-4.0.1


Randomized with seed 33866
....................F....

Failures:

  1) Config::Options when overriding settings via ENV variables is enabled and parsing ENV variable values is enabled should recognize strings starting with : as symbols
     Failure/Error: expect(config.new_var).to eq(:hello)

       expected: :hello
            got: ":hello"

       (compared using ==)

       Diff:
       @@ -1 +1 @@
       -:hello
       +":hello"
     # ./spec/config_env_spec.rb:83:in `block (5 levels) in <top (required)>'

Finished in 0.15873 seconds (files took 0.93499 seconds to load)
25 examples, 1 failure

Failed examples:

rspec ./spec/config_env_spec.rb:80 # Config::Options when overriding settings via ENV variables is enabled and parsing ENV variable values is enabled should recognize strings starting with : as symbols

Randomized with seed 33866

@manjunath724
Copy link

@cjlarose Thanks, I have opened a PR, please take a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants