]> Cypherpunks repositories - gostls13.git/commitdiff
Fix a couple of bugs referencing data values in template.
authorMicah Stetson <micah.stetson@gmail.com>
Fri, 26 Feb 2010 07:39:43 +0000 (18:39 +1100)
committerRob Pike <r@golang.org>
Fri, 26 Feb 2010 07:39:43 +0000 (18:39 +1100)
Adds tests and fixes for two cases that fail with the current release.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/217115

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

index 40b9f640b793b1865fb3f30732fdb36ce72d47bf..1fa55dc8d9fcc9802e1be4d3bd85a54a086a1062 100644 (file)
@@ -623,6 +623,9 @@ func (st *state) findVar(s string) reflect.Value {
                if data == nil {
                        return nil
                }
+               if intf, ok := data.(*reflect.InterfaceValue); ok {
+                       data = intf.Elem()
+               }
 
                switch typ := data.Type().(type) {
                case *reflect.StructType:
@@ -706,6 +709,8 @@ func empty(v reflect.Value) bool {
                return v.Get() == ""
        case *reflect.StructValue:
                return false
+       case *reflect.MapValue:
+               return false
        case *reflect.ArrayValue:
                return v.Len() == 0
        case *reflect.SliceValue:
index a7c34ebeeabcb3c7d53bbb08e8791b55fdbd1643..31cf318cfc56366c8b638972d8fd69030ddf7265 100644 (file)
@@ -9,6 +9,7 @@ import (
        "container/vector"
        "fmt"
        "io"
+       "json"
        "testing"
 )
 
@@ -40,6 +41,7 @@ type S struct {
        true          bool
        false         bool
        mp            map[string]string
+       json          interface{}
        innermap      U
        stringmap     map[string]string
        bytes         []byte
@@ -340,6 +342,16 @@ var tests = []*Test{
 
                out: "55\n",
        },
+       &Test{
+               in: "{.section innermap}{.section mp}{innerkey}{.end}{.end}\n",
+
+               out: "55\n",
+       },
+       &Test{
+               in: "{.section json}{.repeated section maps}{a}{b}{.end}{.end}\n",
+
+               out: "1234\n",
+       },
        &Test{
                in: "{stringmap.stringkey1}\n",
 
@@ -391,6 +403,7 @@ func TestAll(t *testing.T) {
        s.false = false
        s.mp = make(map[string]string)
        s.mp["mapkey"] = "Ahoy!"
+       s.json, _ = json.Decode("{\"maps\":[{\"a\":1,\"b\":2},{\"a\":3,\"b\":4}]}")
        s.innermap.mp = make(map[string]int)
        s.innermap.mp["innerkey"] = 55
        s.stringmap = make(map[string]string)