-
Notifications
You must be signed in to change notification settings - Fork 440
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
Syntax for optional array concatenation #234
Comments
@chrisleck had similar feedback in the context of maps and the same solution should be used for both cases |
+1 to some form of undefined or null that is removed at render time in arrays/maps. I just ran into this problem last week in an array definition where one of the elements was optional based on an optional param to a function generating it. Having the optional param default to "None" or "Null" or "undefined" and then not having it show up in the subsequent array would be magical. |
I would like this. For reference, here's a snippet that we use to work around not having this for the object case. function(cfg)
local if_enabled(addon, manifest) = if cfg.phase3[addon] then manifest else {};
local join(arr) = std.foldl(function(a, b) a + b, arr, {});
if_enabled("run_addons",
join([
if_enabled("kube_proxy", (import "kube-proxy/kube-proxy.jsonnet")(cfg)),
if_enabled("dashboard", (import "dashboard/dashboard.jsonnet")(cfg)),
if_enabled("heapster", (import "heapster/heapster.jsonnet")(cfg)),
if_enabled("kube_dns", (import "kube-dns/kube-dns.jsonnet")(cfg)),
])) From here |
We ended up adding a utility function:
Which lets us do both and serves most of our use cases. I'm gonna close this for now. |
Re-opening because there ought to be a better solution for this |
@sparkprime I suggested in #ksonnet on Slack adding an unambiguous type of Adding I also suggested borrowing from Ruby and adding infix Similarly, in method chains ( Although |
My preferred way of doing this is to extend the python composition syntax to allow things like [ 1, 2 if true, 3 if false ] == [ 1, 2 ] I.e., rather than having 2 kinds of object literal (composition and non-composition) we instead have a single one that encompasses everything and squares it all out. You're allowed to put a "forspec" on any fields. I agree std.prune isn't the best way to do this, it was just easy. |
@sparkprime Please borrow |
This function removes empty values in a list as also used in the custom
|
Hi, I just wanted to chime in and mention another use-case for an I'm using Jsonnet to generate AWS IAM policies. The basic setup looks like this:
If I want to add more policy statements based on conditions, I would (intuitively) do it so:
However, in case that Currently, I'm using the following workaround:
Having an |
Hello from 2021.
Is there a cleaner way of doing this yet? |
Yeah, e.g.:
|
We've had pretty good luck with the aforementioned join() method. We've been using it quite extensively for quite a while. |
At Box we've often had a need to optionally include elements in an array, e.g.
While the current approach is effective it has two downsides:
Either some guidance on how best to handle cases like this, or an extension to the syntax would be nice. I talked to @sparkprime yesterday in slack and he spun up a suggestion to talk about:
Which is much more readable and deals with #1 and #2.
We began talking about potential implementations for this, and explored the idea of an undefined type/value which stays in the list until render time (and comparison) at which point it is removed. That idea has a lot of merit and would even allow functions to return undefined thereby pushing certain branching logic down, but there are obviously a lot of interesting corner cases.
Dave also mentioned that if such a proposal went through he'd probably want to move from
{ [if false then "foo"]: "bar" } to { foo: "bar" if false }.
The text was updated successfully, but these errors were encountered: