Skip to content

Instantly share code, notes, and snippets.

@nlowe
Last active September 9, 2020 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlowe/57aedd465e18ebe9d710616338515515 to your computer and use it in GitHub Desktop.
Save nlowe/57aedd465e18ebe9d710616338515515 to your computer and use it in GitHub Desktop.
Patching / Overriding is poorly explained in the jsonnet documentation. Here's the tl;dr:
// See https://jsonnet.xrt0x.com/j/VigL64fxlx4
//
// TL;DR:
// * `:` -> Set a field, preserving its visibility. If this is the root / base object, the field is public.
// * `::` -> Set a field and also make it private / hidden.
// * `:::` -> Set a field and also make it public / visible.
//
// Prefix any of these with a `+` (so `+:` / `+::` / `+:::`) to patch a field / object instead. What "patch"ing
// means depends on the type of the field. Objects are merged, lists are appended to, and strings are concatenated.
{
a: {
pub1: "pub1",
pub2: "pub2",
pub3: "pub3",
priv1:: "priv1",
priv2:: "priv2",
priv3:: "priv3",
_priv1: self.priv1,
_priv2: self.priv2,
_priv3: self.priv3,
}
} + {
a+: {
// : sets a field, preserving its visiblity
pub1: "override1",
priv1: "overridepriv1"
}
} + {
a+: {
// :: sets a field, overriding its visiblity to private
pub2:: "override2",
priv2:: "overridepriv2"
}
} + {
a+: {
// ::: sets a field, overriding its visibility to public
pub3::: "override3",
priv3::: "overridepriv3"
}
}
{
"a": {
"_priv1": "overridepriv1",
"_priv2": "overridepriv2",
"_priv3": "overridepriv3",
"priv3": "overridepriv3",
"pub1": "override1",
"pub3": "override3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment