Fix default template fallback for missing vars
This commit is contained in:
parent
41d74b0ac6
commit
defcd18f44
2 changed files with 13 additions and 1 deletions
|
|
@ -2279,7 +2279,7 @@ func (e *Executor) applyFilter(value, filter string) string {
|
|||
|
||||
// Handle default filter
|
||||
if corexHasPrefix(filter, "default(") {
|
||||
if value == "" || value == "{{ "+filter+" }}" {
|
||||
if value == "" || isUnresolvedTemplateValue(value) {
|
||||
// Extract default value
|
||||
re := regexp.MustCompile(`default\(([^)]*)\)`)
|
||||
if match := re.FindStringSubmatch(filter); len(match) > 1 {
|
||||
|
|
@ -2318,6 +2318,10 @@ func (e *Executor) applyFilter(value, filter string) string {
|
|||
return value
|
||||
}
|
||||
|
||||
func isUnresolvedTemplateValue(value string) bool {
|
||||
return corexHasPrefix(value, "{{ ") && corexHasSuffix(value, " }}")
|
||||
}
|
||||
|
||||
// handleLookup handles lookup() expressions.
|
||||
func (e *Executor) handleLookup(expr string) string {
|
||||
// Parse lookup('type', 'arg')
|
||||
|
|
|
|||
|
|
@ -518,6 +518,14 @@ func TestModulesInfra_TemplateString_Good_FilterInTemplate(t *testing.T) {
|
|||
assert.Equal(t, "hello", result)
|
||||
}
|
||||
|
||||
func TestModulesInfra_TemplateString_Good_DefaultFilterMissingVar(t *testing.T) {
|
||||
e := NewExecutor("/tmp")
|
||||
|
||||
result := e.templateString("{{ missing_var | default('fallback') }}", "", nil)
|
||||
|
||||
assert.Equal(t, "fallback", result)
|
||||
}
|
||||
|
||||
func TestModulesInfra_TemplateString_Good_DefaultFilterEmptyVar(t *testing.T) {
|
||||
e := NewExecutor("/tmp")
|
||||
e.vars["empty_var"] = ""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue