diff --git a/CHANGELOG.md b/CHANGELOG.md index 344bb4576..909f5fc32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added * `getValueOrDefault` helper ([#125]) +* `makeMergeable` helper ([#126]) ## [v0.1.6] @@ -89,3 +90,4 @@ Initial implementation [#103]: https://github.com/projectsyn/commodore/pull/103 [#106]: https://github.com/projectsyn/commodore/pull/106 [#125]: https://github.com/projectsyn/commodore/pull/125 +[#126]: https://github.com/projectsyn/commodore/pull/126 diff --git a/commodore/lib/commodore.libjsonnet b/commodore/lib/commodore.libjsonnet index ea129d93f..43e0a9c75 100644 --- a/commodore/lib/commodore.libjsonnet +++ b/commodore/lib/commodore.libjsonnet @@ -133,6 +133,25 @@ local envList(map) = [ local getValueOrDefault(dict, field, default) = if std.objectHas(dict, field) then dict[field] else default; +/* + * \brief makes an object deep mergeable + * + * Builds a new object from its input. + * All keys which contain an object will be suffixed with `+` in the result. + * + * \arg o An arbitrary object. + * \return The transformed object. + */ +local makeMergeable(o) = { + [key]+: makeMergeable(o[key]) + for key in std.objectFields(o) + if std.isObject(o[key]) +} + { + [key]: o[key] + for key in std.objectFields(o) + if !std.isObject(o[key]) +}; + { inventory: std.native('inventory'), yaml_load: std.native('yaml_load'), @@ -144,4 +163,5 @@ local getValueOrDefault(dict, field, default) = proxyVars: proxyVars, envList: envList, getValueOrDefault: getValueOrDefault, + makeMergeable: makeMergeable, }