1
0
mirror of https://github.com/golang/go synced 2024-11-22 16:04:40 -07:00

template: look inside interface values

R=r
CC=golang-dev
https://golang.org/cl/186169
This commit is contained in:
Russ Cox 2010-01-15 13:49:31 -08:00
parent d6054fcd88
commit 08d3f5e271
2 changed files with 23 additions and 0 deletions

View File

@ -723,6 +723,9 @@ func (t *Template) varValue(name string, st *state) reflect.Value {
} }
return t.varValue(name, st.parent) return t.varValue(name, st.parent)
} }
if iface, ok := field.(*reflect.InterfaceValue); ok && !iface.IsNil() {
field = iface.Elem()
}
return field return field
} }

View File

@ -44,6 +44,7 @@ type S struct {
innermap U innermap U
stringmap map[string]string stringmap map[string]string
bytes []byte bytes []byte
iface interface{}
} }
func (s *S) pointerMethod() string { return "ptrmethod!" } func (s *S) pointerMethod() string { return "ptrmethod!" }
@ -353,6 +354,24 @@ var tests = []*Test{
out: "stringresult\n" + out: "stringresult\n" +
"stringresult\n", "stringresult\n",
}, },
// Interface values
&Test{
in: "{iface}",
out: "[1 2 3]",
},
&Test{
in: "{.repeated section iface}{@}{.alternates with} {.end}",
out: "1 2 3",
},
&Test{
in: "{.section iface}{@}{.end}",
out: "[1 2 3]",
},
} }
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
@ -379,6 +398,7 @@ func TestAll(t *testing.T) {
s.stringmap["stringkey1"] = "stringresult" // the same value so repeated section is order-independent s.stringmap["stringkey1"] = "stringresult" // the same value so repeated section is order-independent
s.stringmap["stringkey2"] = "stringresult" s.stringmap["stringkey2"] = "stringresult"
s.bytes = strings.Bytes("hello") s.bytes = strings.Bytes("hello")
s.iface = []int{1, 2, 3}
var buf bytes.Buffer var buf bytes.Buffer
for _, test := range tests { for _, test := range tests {