From 6c12335091f11edc5fe123a47a83be33a225db36 Mon Sep 17 00:00:00 2001 From: Christian Haeusler Date: Tue, 30 Jun 2020 15:39:11 +0200 Subject: [PATCH 1/2] Add helper `makeMergeable` --- CHANGELOG.md | 2 ++ commodore/lib/commodore.libjsonnet | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) 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..dd3201b2f 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; +/* + * \biref makes an object deep mergable + * + * Builds a new object from its input. + * Each key again containing an object, will be suffixed with `+` in the result. + * + * \arg o An arbitrary object. + * \return The transformed object. + */ +local makeMergeable = function(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, } From 831780758a877f37244d49cd67319cedb2e25e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=A4usler?= Date: Tue, 30 Jun 2020 15:47:33 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Simon Gerber --- commodore/lib/commodore.libjsonnet | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/commodore/lib/commodore.libjsonnet b/commodore/lib/commodore.libjsonnet index dd3201b2f..43e0a9c75 100644 --- a/commodore/lib/commodore.libjsonnet +++ b/commodore/lib/commodore.libjsonnet @@ -134,15 +134,15 @@ local getValueOrDefault(dict, field, default) = if std.objectHas(dict, field) then dict[field] else default; /* - * \biref makes an object deep mergable + * \brief makes an object deep mergeable * * Builds a new object from its input. - * Each key again containing an object, will be suffixed with `+` in the result. + * All keys which contain an object will be suffixed with `+` in the result. * * \arg o An arbitrary object. * \return The transformed object. */ -local makeMergeable = function(o) { +local makeMergeable(o) = { [key]+: makeMergeable(o[key]) for key in std.objectFields(o) if std.isObject(o[key])