Skip to content

Commit

Permalink
fix: handle function references in composite bin map
Browse files Browse the repository at this point in the history
When passing a function reference as an interface in a composite binary map, the case should be handled to not take the value of the the node.

Related to #886
  • Loading branch information
nrwiersma authored Jan 28, 2021
1 parent 61b4980 commit ff521ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
30 changes: 30 additions & 0 deletions _test/composite17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"html/template"
)

var str = `{{ stringOr .Data "test" }}`

func main() {
_, err := template.New("test").
Funcs(template.FuncMap{
"stringOr": stringOr,
}).
Parse(str)
if err != nil {
println(err.Error())
return
}
println("success")
}

func stringOr(v, def string) string {
if v == "" {
return def
}
return v
}

// Output:
// success
7 changes: 6 additions & 1 deletion interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2220,7 +2220,12 @@ func compositeBinMap(n *node) {
convertLiteralValue(c.child[0], typ.Key())
convertLiteralValue(c.child[1], typ.Elem())
keys[i] = genValue(c.child[0])
values[i] = genValue(c.child[1])

if c.child[1].typ.cat == funcT {
values[i] = genFunctionWrapper(c.child[1])
} else {
values[i] = genValue(c.child[1])
}
}

n.exec = func(f *frame) bltn {
Expand Down

0 comments on commit ff521ec

Please sign in to comment.