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 config option to disable ERB in YAML #303

Merged
merged 2 commits into from
Apr 10, 2021

Conversation

kmcphillips
Copy link
Contributor

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 or RuntimeError) 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.

Copy link
Member

@cjlarose cjlarose left a 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.

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
Copy link
Member

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.

def initialize(env,
prefix: Config.env_prefix || Config.const_name,
separator: Config.env_separator,
converter: Config.env_converter,
parse_values: Config.env_parse_values)

Copy link
Contributor Author

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.

@kmcphillips
Copy link
Contributor Author

I went back and forth on the config name. evaluate_erb is simpler, but then I realized that it only applies to YAML source, so I set it to evaluate_erb_in_yaml. Thoughts?

@kmcphillips
Copy link
Contributor Author

Or I guess a third option is evaluate_erb in the YAMLSource class, and evaluate_erb_in_yaml in the global config.

@kmcphillips
Copy link
Contributor Author

Ready for another review.

@cjlarose
Copy link
Member

cjlarose commented Apr 9, 2021

Or I guess a third option is evaluate_erb in the YAMLSource class, and evaluate_erb_in_yaml in the global config.

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.

@kmcphillips
Copy link
Contributor Author

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.

Copy link
Member

@cjlarose cjlarose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work!

@cjlarose cjlarose merged commit becfc06 into rubyconfig:master Apr 10, 2021
@kmcphillips
Copy link
Contributor Author

Nice! Thanks!

@cjlarose
Copy link
Member

Published in version 3.1.0!

ippachi pushed a commit to ippachi/config-1 that referenced this pull request Oct 3, 2021
* Allow config option to disable ERB in YAML

* Set ERB config per YAMLSource with fallback to global config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants