-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 deep-merging of options? #1180
Comments
This definitely sounds useful for complex tasks. A tricky thing I think will be how tricky merging objects deeply is. We can provide a simple mechanism, but it probably won't be complicated enough for some. I think we should provide an interface that can be given a function similar to a comparator. I'm not sure if there is a name for something like this already.
An object listing depth could be normalized to this function storing root key and depth in userdata to check if it should merge deeper. This whole issue seems like it could be rather complex to figure out and also explain whatever is decided but useful. |
What are the use-cases? I've personally yet to see any need or demand for this though. |
I don't have any personal use-cases, but I know a discussion was happening around gruntjs/grunt-contrib-concat#59, and there might be others. If this is a common request, perhaps it should be built-in. |
The nested options help us keep configuration clean, keep the READMEs clean, etc.
or
or
|
👍 |
+1 This would be great. As a user of grunt tasks, it feels frustrating that to override a 2nd level property you need to override the whole option. Probably grunt wasn't designed with such complexity in mind, but the fact is many tasks have N deep configuration. Also, as a grunt task developer I always try to avoid nesting configuration, because of lack of support for deep merge, but if feels weird to expose task with a configuration like this:
In the above case, I have the need to override only the |
I would also love deep merging of the options by default. Could do it on my own using |
+1, my use case: Gruntfile{
options: {
hooks: {
init () {
fs.readFile('files/logo.txt', {
encoding: 'utf-8'
},
(error, logo) => {
console.log(logo);
})
}
}
}
} Actual tasklet options = this.options({
hooks: {
/*
* init,
* done,
* fail
*/
}
});
let { hooks } = options;
if (hooks.init) {
hooks.init();
} Expected tasklet options = this.options({
hooks: {
init () {},
fail () {},
done () {}
}
});
let { hooks } = options;
hooks.init(); |
Easy to workaround but without recursively merging, this.options() is completely useless for me. Surprised to see that this issue is over 2 years old. |
Some task authors have expressed a desire for
this.options()
to (recursively) deep merge task options. Right now, options are shallow merged (one level deep).(Currently, target-level config options override task-level config options, which override built-in task options. Nobody is asking for this to change, yay)
Why aren't options deep merged? Because options are atomic. While individual options must be merged into a single object, their values should be left unchanged.
To better understand this, we must understand what an option is. An option is a single value used to configure a task. Tasks can be configured with zero or more options. Thus, the merged options object may contain, as its properties, zero or more options, each with a corresponding value.
When one thinks about options in this way, the idea of deep merging all options seems a bit heavy handed. So I'll propose an alternative, although I'm not sure what it should look like yet.
What if task authors could specify, inside the task where they specify the built-in default values, a per-option setting that enables deep merging for that option? The setting could be a number representing the max depth. And it would default to
0
orfalse
for a shallow merge.Thoughts?
The text was updated successfully, but these errors were encountered: