-
Notifications
You must be signed in to change notification settings - Fork 36
Add function to safely check variable existence in templates #148
Conversation
Is there a use-case other than defaulting? The issue with the current |
Nothing else than defaulting comes to mind, no. You're thinking of overriding sprig's Meaning something like: --- a/templater/templater.go
+++ b/templater/templater.go
@@ -203,7 +203,7 @@ func templateFuncs(c *context.Context, rs *context.ResourceSet) template.FuncMap
return string(data), nil
}
- m["definedOrEmpty"] = func(varName string) string {
+ m["default"] = func(varName string) string {
if val, ok := rs.Values[varName]; ok {
return fmt.Sprint(val)
} |
Forgot to mention that my understanding of the underlying problem here, is not failing on The sprig docs explicitly gives the impression that |
Yep!
You're right, that's what I meant - having a slow brain day in the aftermath of Underwater Pub's last opera night yesterday ;-) |
Alrighty, so overriding the The only challenge I'm seeing is for this new A couple of examples to illustrate what I'm getting at. Here sprig's
Now if we were to override this
How would we know that |
Using |
Very good point indeed! I'll give it a go later and see how it turns out.
…On Fri, 29 Jun 2018 at 17:19, Vincent Ambo ***@***.***> wrote:
Using default on a string literal wouldn't make sense because it's always
non-nil, right? So we can just say that default's second argument *always*
refers to a variable.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#148 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABLLEy7CFeeRFpHjTTeeWTu1y2UPiVpDks5uBkWBgaJpZM4U6b4_>
.
|
That worked like a charm, great suggestion 👍
For sure easier to grasp than my initial suggestion, and covers my use-case perfectly. Took some inspiration from sprig's Pushed a second commit making it easier to see the actual changes made compared to the first approach -- probably squashable before merging tho. |
FYI, I'm on vacation atm - hence a little bit of delay :) |
…tive These changes overrides the `default` function provided by sprig with an alternative to retrieve variable values from variables that might not have been declared at all. Referencing a variable in a template that is not declared, will lead to the underlying templating functionality raising an error, causing kontemplate to exit. The override alternative to `default` accepts a second string argument with the variable name. If the variable in question has not been declared the first argument's value would be returned, just as the original `default` function does.
bd8e0c1
to
5eb0702
Compare
5eb0702
to
13f43e0
Compare
Thanks! |
This change is included in version 1.7.0 :) |
Hooray, thanks for cutting a new version! 🎉 |
I think I'm encountering the issue that this PR discusses / fixes, and I'm not sure how to use it correctly ... With a map with subitems, (say,
If
|
I would try passing in your last argument as a string and without the dot prefix: {{ default "missing" "config" }} Does that work by any chance? |
@jbell-shibumi The |
Howdy @tazjin!
These changes adds the
definedOrEmpty
template function which allows templates to get values from possibly non-existent variables in a safe manner.If no variable exists with the name provided, an empty string will be returned. This makes it a good fit for combining with sprig's
default
function:Would either result in the
version
variable's value, or if it has not been declared, would use the result ofgitHEAD
function instead.Any thoughts?
P.S. I'm not opinionated about the function name.
Refs #140 (comment)