-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Optional namespace for add_source!
#288
Comments
Adding sources at runtime should be rather done this way: https://github.com/rubyconfig/config#adding-sources-at-runtime However |
I think this feature is kind of cool. I could see people using it for dev/test/prod type breakdowns as well, where they want all 3 to appear in the final payload (as opposed to only one being merged based on env). If this feature is added, it just has to not conflict with the existing loading mechanisms. |
I don't understand why there's an Array for the namespace, unless you're expecting it to nest multiple levels or something? -Config::Sources::YAMLSource.new("/Users/userguy/repos/scratch/example.yml", ['new-level'])
+Config::Sources::YAMLSource.new("/Users/userguy/repos/scratch/example.yml", 'new-level') |
Yep, that's precisely it. Providing multiple items to the array nests the result progressively deeper @Fryguy . That's what that whole reverse-inject combo takes care of doing. |
@Fryguy -- With that said, you're not wrong that it's a little unnatural, lol. It certainly wouldn't be all that hard to slip in the whole |
It's already possible to pass an arbitrary nested_settings = Config::Sources::YAMLSource.new("/Users/userguy/repos/scratch/example.yml")
Settings.add_source!({ new_level: nested_settings.load }) |
@cjlarose That's a good point...I completely forgot that add_source can take arbitrary hashes, so yes I agree with you. EDIT: However, will reload! work with that? |
Reload would not work with the proposed namespace either I guess? |
In the proposal the namespace is part of the source, so it could be reloaded. |
@cjlarose -- I did try that exact thing, but I remember experiencing some undesirable behaviour on reloads, as chunks of the configuration would just disappear. That's why I went to the source to make my modification so that this would persist through reloads. If you wanted to modify the behaviour of hash sources and nested objects inside to achieve the same effect, I wouldn't have a strong opinion about it (not really my place to have a strong opinion about a project you all maintain, anyway) but it seems from my uneducated vantage point like it would be more complicated to plumb in reliably. |
If others agree, I am fine with accepting a PR with this feature... |
@wwboynton Makes sense. |
@wwboynton would you still like to fire up PR for this? |
Hey, all. I'm sorry! When I wrote this issue, I was unemployed. Less than a week after, I ended up with a new job and therefore zero free time. But this weekend I was working on a project, used this gem, encountered this same thing again, and instantly remembered this whole conversation, so I've taken a moment and cracked open a fork with a PR coming soon. I'm happy to make any changes necessary for code style (i.e. keyword method argument, ternary as opposed to multi-line, whatever) or functionality, I don't pretend to know my way all around this codebase and I'm happy to defer to your judgement, I just know this is functionality that I need and others might benefit from it, too. |
It would be nice to be able to invoke
Settings.add_source!
with an optional parent prefix such that new sources could be loaded in programmatically into different levels of the Settings structure. This would allow for multiple YAML files of similar function to be grouped under the same parent without having to specify that namespace in every single YAML file.In my case, I have a lot of user-defined YAML files together in a directory which are split up for organization/legibility, but which cohabitate in the same parent node of the
Settings
object when all loaded together. It's confusing to ask my users to specify a few levels of parent nodes at the top of every single file in the directory just so that theSettings
object will be correctly organized when it gets to my code. It would be nice to write code which will load these YAML files into theSettings
object in a way that mimics their directory structure on the filesystem.Modifying the
yaml_source.rb
source to accommodate this might look something like this:Running that like so:
...produces output like this:
If you'd like me to take a crack at doing this and submitting a PR, let me know and I'm happy to do it. Maybe I am just very tired and completely missed an existing way to do this in the documentation, or am overthinking this and forgetting something fundamental about YAML that will solve my problem. It has been known to happen, in which case please close this issue and accept my apologies for wasting your time!
The text was updated successfully, but these errors were encountered: