-
-
Notifications
You must be signed in to change notification settings - Fork 229
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 config option to disable ERB in YAML #303
Conversation
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.
Thanks for your contribution! This is great!
I added a suggestion to make it a little nicer if you're up to it. Otherwise, I'm happy to merge as-is and leave the suggestion for future work.
lib/config/sources/yaml_source.rb
Outdated
result = YAML.load(ERB.new(IO.read(@path)).result) if @path and File.exist?(@path) | ||
if @path and File.exist?(@path) | ||
file_contents = IO.read(@path) | ||
file_contents = ERB.new(file_contents).result if Config.evaluate_erb_in_yaml |
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.
Minor suggestion that would be really nice: give users the option to opt into- or out of this behavior on a per-file basis if they're using the YAMLSource
API directly. For example,
Config.add_source!(Config::Sources::YAMLSource.new(path1)) # defaults to global Config.evaluate_erb_in_yaml
Config.add_source!(Config::Sources::YAMLSource.new(path1, evaluate_erb: false)) # overrides global setting
Config.add_source!(Config::Sources::YAMLSource.new(path1, evaluate_erb: true)) # overrides global setting
That way it's not all-or-nothing. We do something similar for EnvSource
here: configuration is derived from the global values if left unspecified, but can always be overridden.
config/lib/config/sources/env_source.rb
Lines 10 to 14 in 618c839
def initialize(env, | |
prefix: Config.env_prefix || Config.const_name, | |
separator: Config.env_separator, | |
converter: Config.env_converter, | |
parse_values: Config.env_parse_values) |
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.
Oh neat. Ok I'll spend some time with this. Feedback makes sense.
I went back and forth on the config name. |
Or I guess a third option is |
Ready for another review. |
I like this best and it looks like this is what you went with. Code looks great! Thanks for making the changes. I'll do some local testing here in the next couple of days, merge, and publish a new version. |
Ok! Let me know if you want changes. I'm happy with it I think, but every codebase is different. Thanks for accepting the contribution. |
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.
Awesome work!
Nice! Thanks! |
Published in version 3.1.0! |
* Allow config option to disable ERB in YAML * Set ERB config per YAMLSource with fallback to global config
Problem
There is currently no way to disable ERB evaluation in YAML files at load time.
This is an issue if the config file has a string that contains ERB to be evaluated later, or something that looks like ERB. It simply raises an error (such as
SyntaxError
orRuntimeError
) at application load time.Solution
Add the
Config.evaluate_erb_in_yaml
option, and use it to conditionally evaluate ERB or simply pass the string through.This is fully backwards compatible as the value defaults to
true
.