mirror of
https://github.com/golang/go
synced 2024-11-22 04:34:39 -07:00
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
This commit is contained in:
parent
7b73251d3d
commit
ce274339a1
@ -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},
|
||||
|
||||
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user