]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1] text/template: fix bug in map indexing
authorRob Pike <r@golang.org>
Fri, 21 Sep 2012 19:54:11 +0000 (05:54 +1000)
committerRob Pike <r@golang.org>
Fri, 21 Sep 2012 19:54:11 +0000 (05:54 +1000)
««« backport 0748cd92ed76
text/template: fix bug in map indexing
If the key is not present, return value of the type of the element
not the type of the key. Also fix a test that should have caught this case.

Fixes #3850.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/6405078

»»»

src/pkg/text/template/exec_test.go
src/pkg/text/template/funcs.go

index f4ae50f0ee9dcaa7183ab3f81926a864d01a4a35..64149533b3e9939f90ee83cc778ef285ec3962e1 100644 (file)
@@ -387,7 +387,7 @@ var execTests = []execTest{
        {"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false},
        {"map[one]", "{{index .MSI `one`}}", "1", tVal, true},
        {"map[two]", "{{index .MSI `two`}}", "2", tVal, true},
-       {"map[NO]", "{{index .MSI `XXX`}}", "", tVal, true},
+       {"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true},
        {"map[WRONG]", "{{index .MSI 10}}", "", tVal, false},
        {"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true},
 
index 90fb9c52c080784796113f1851ebb30206819062..e6fa0fb5f2a24f00836c15ef697fb2beef147dbf 100644 (file)
@@ -128,7 +128,7 @@ func index(item interface{}, indices ...interface{}) (interface{}, error) {
                        if x := v.MapIndex(index); x.IsValid() {
                                v = x
                        } else {
-                               v = reflect.Zero(v.Type().Key())
+                               v = reflect.Zero(v.Type().Elem())
                        }
                default:
                        return nil, fmt.Errorf("can't index item of type %s", index.Type())