diff options
author | Micah Stetson <micah.stetson@gmail.com> | 2010-02-26 18:39:43 +1100 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2010-02-26 18:39:43 +1100 |
commit | 8c5404746ff2d14a1d3eacf78dfd25940411156e (patch) | |
tree | 7b2306c051e6c2363734febd10d24f5bff61060c | |
parent | 8d4fb690d623c7e33078f841b485edd57a210743 (diff) | |
download | go-8c5404746ff2d14a1d3eacf78dfd25940411156e.tar.gz go-8c5404746ff2d14a1d3eacf78dfd25940411156e.zip |
Fix a couple of bugs referencing data values in template.
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
-rw-r--r-- | src/pkg/template/template.go | 5 | ||||
-rw-r--r-- | src/pkg/template/template_test.go | 13 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/pkg/template/template.go b/src/pkg/template/template.go index 40b9f640b7..1fa55dc8d9 100644 --- a/src/pkg/template/template.go +++ b/src/pkg/template/template.go @@ -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: diff --git a/src/pkg/template/template_test.go b/src/pkg/template/template_test.go index a7c34ebeea..31cf318cfc 100644 --- a/src/pkg/template/template_test.go +++ b/src/pkg/template/template_test.go @@ -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 @@ -341,6 +343,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", out: "stringresult\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) |