carvel-dev / ytt

YAML templating tool that works on YAML structure instead of text
https://carvel.dev/ytt
Apache License 2.0
1.68k stars 137 forks source link

how to best short-circuit entire template evaluation #500

Open jayunit100 opened 3 years ago

jayunit100 commented 3 years ago

possible workaround, from conv w dimitry, using a .lib.yaml.... Describe the problem/challenge you have

Right now, we have to sometimes nest 1000s of lines of yaml in a

#@ if
...
#@end

block, which can be hard to reason about (i.e. what is this #@end referring to?)...

https://github.com/vmware-tanzu/tanzu-framework/blob/main/pkg/v1/providers/infrastructure-vsphere/v0.7.10/ytt/overlay.yaml

Describe the solution you'd like

Something like

#@ if not data.values.IS_WINDOWS_WORKLOAD_CLUSTER:
    #@ break;
#@end

so that we can short circuit (or not) the processing of a file in some way.

pivotaljohn commented 3 years ago

In general, the ability to write guard clauses can make it easier to reason over a function (lowercase 'f'). 👍🏻 It's effectively a kind of precondition which alleviates the reader from being concerned about that condition for the rest of the execution.

Turning towards implementation concerns...

More context:

Options:

Concerns:

cppforlife commented 3 years ago

return is only allowed within a function

that's still one of the options. we can rewrite ast to support return at top level. question is it a good idea?

pivotaljohn commented 3 years ago

I'm on the fence, ATM.

On the one hand — as I noted above — guard clauses can made a larger script much more easier to reason about.

On the other hand, this mechanism can be used to terminate execution anywhere in the script. Imagine there's a return in the middle of the example Jay gave... ugh.

cppforlife commented 3 years ago

im also on the conservative side for such change, since it may come with its own set of sharp edges.