]> Cypherpunks repositories - gostls13.git/commitdiff
template: look inside interface values
authorRuss Cox <rsc@golang.org>
Fri, 15 Jan 2010 21:49:31 +0000 (13:49 -0800)
committerRuss Cox <rsc@golang.org>
Fri, 15 Jan 2010 21:49:31 +0000 (13:49 -0800)
R=r
CC=golang-dev
https://golang.org/cl/186169

src/pkg/template/template.go
src/pkg/template/template_test.go

index f1257b0915a70f364b5a0b1079d7423dbf3aec61..b507c3c9ecca6468e3d68fd40fe3ec267b47509c 100644 (file)
@@ -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
 }
 
index 0ae581c5931a53f1602b10a3fe36f0ae3838c197..fe279a4d16fcd7796c94b22cbc4a38dc42709f0e 100644 (file)
@@ -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 {