summaryrefslogtreecommitdiff
path: root/src/pkg/template
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-01-15 13:49:31 -0800
committerRuss Cox <rsc@golang.org>2010-01-15 13:49:31 -0800
commit82b5112a0763ce69c9bf9b44672de4ef1a7a7203 (patch)
tree078dec0d16c31a4aa8fe9fd15f1f082b2dd8f56c /src/pkg/template
parente32c9703026be10ab1265d107bd1cbc7af1b3bbd (diff)
downloadgolang-82b5112a0763ce69c9bf9b44672de4ef1a7a7203.tar.gz
template: look inside interface values
R=r CC=golang-dev http://codereview.appspot.com/186169
Diffstat (limited to 'src/pkg/template')
-rw-r--r--src/pkg/template/template.go3
-rw-r--r--src/pkg/template/template_test.go20
2 files changed, 23 insertions, 0 deletions
diff --git a/src/pkg/template/template.go b/src/pkg/template/template.go
index f1257b091..b507c3c9e 100644
--- a/src/pkg/template/template.go
+++ b/src/pkg/template/template.go
@@ -723,6 +723,9 @@ func (t *Template) varValue(name string, st *state) reflect.Value {
}
return t.varValue(name, st.parent)
}
+ if iface, ok := field.(*reflect.InterfaceValue); ok && !iface.IsNil() {
+ field = iface.Elem()
+ }
return field
}
diff --git a/src/pkg/template/template_test.go b/src/pkg/template/template_test.go
index 0ae581c59..fe279a4d1 100644
--- a/src/pkg/template/template_test.go
+++ b/src/pkg/template/template_test.go
@@ -44,6 +44,7 @@ type S struct {
innermap U
stringmap map[string]string
bytes []byte
+ iface interface{}
}
func (s *S) pointerMethod() string { return "ptrmethod!" }
@@ -353,6 +354,24 @@ var tests = []*Test{
out: "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) {
@@ -379,6 +398,7 @@ func TestAll(t *testing.T) {
s.stringmap["stringkey1"] = "stringresult" // the same value so repeated section is order-independent
s.stringmap["stringkey2"] = "stringresult"
s.bytes = strings.Bytes("hello")
+ s.iface = []int{1, 2, 3}
var buf bytes.Buffer
for _, test := range tests {