]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: fix bug in map indexing
authorRob Pike <r@golang.org>
Mon, 23 Jul 2012 23:19:12 +0000 (16:19 -0700)
committerRob Pike <r@golang.org>
Mon, 23 Jul 2012 23:19:12 +0000 (16:19 -0700)
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 c8a30139773b49a2ff6fdf544b5240ec345d3894..4efe2d1b3820bc9cdeaa1ae7972574885f8e987c 100644 (file)
@@ -390,7 +390,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())