Fix default template fallback for missing vars

This commit is contained in:
Virgil 2026-04-01 22:47:37 +00:00
parent 41d74b0ac6
commit defcd18f44
2 changed files with 13 additions and 1 deletions

View file

@ -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')

View file

@ -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"] = ""