Skip to content

Commit ff521ec

Browse files
authored
fix: handle function references in composite bin map
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
1 parent 61b4980 commit ff521ec

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

_test/composite17.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"html/template"
5+
)
6+
7+
var str = `{{ stringOr .Data "test" }}`
8+
9+
func main() {
10+
_, err := template.New("test").
11+
Funcs(template.FuncMap{
12+
"stringOr": stringOr,
13+
}).
14+
Parse(str)
15+
if err != nil {
16+
println(err.Error())
17+
return
18+
}
19+
println("success")
20+
}
21+
22+
func stringOr(v, def string) string {
23+
if v == "" {
24+
return def
25+
}
26+
return v
27+
}
28+
29+
// Output:
30+
// success

interp/run.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2220,7 +2220,12 @@ func compositeBinMap(n *node) {
22202220
convertLiteralValue(c.child[0], typ.Key())
22212221
convertLiteralValue(c.child[1], typ.Elem())
22222222
keys[i] = genValue(c.child[0])
2223-
values[i] = genValue(c.child[1])
2223+
2224+
if c.child[1].typ.cat == funcT {
2225+
values[i] = genFunctionWrapper(c.child[1])
2226+
} else {
2227+
values[i] = genValue(c.child[1])
2228+
}
22242229
}
22252230

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

0 commit comments

Comments
 (0)