Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/template: type checker bug with missing map keys #3850

Closed
remyoudompheng opened this issue Jul 22, 2012 · 4 comments
Closed

text/template: type checker bug with missing map keys #3850

remyoudompheng opened this issue Jul 22, 2012 · 4 comments

Comments

@remyoudompheng
Copy link
Contributor

What steps will reproduce the problem?
1. Compile and run the following program

package main

import (
      "bytes"
      "fmt"
      "math/big"
      "text/template"
)

func main() {
      n, _ := new(big.Int).SetString("123456789", 10)
      display := func(n *big.Int) string { return n.String() }
      tpl := template.Must(template.
            New("test").
            Funcs(template.FuncMap{"display": display}).
            Parse(`{{ index $.Values $.Key | display }}`))

      type data struct {
            Values map[string]*big.Int
            Key    string
      }

      values := map[string]*big.Int{"number": n, "hello": nil}

      for _, key := range []string{"number", "hello", "world"} {
            buf := new(bytes.Buffer)
            err := tpl.Execute(buf, data{values, key})
            fmt.Printf("%q\n%v\n", buf.String(), err)
      }
}

What is the expected output? What do you see instead?

Expected:
"123456789"
<nil>
"<nil>"
<nil>
"<nil>"
<nil>

Got:
"123456789"
<nil>
"<nil>"
<nil>
""
template: test:1: wrong type for value; expected *big.Int; got string

Please use labels and text to provide additional information.
@remyoudompheng
Copy link
Contributor Author

Comment 1:

I expect the following change but it breaks the tests.
diff -r 5e7fd762f356 src/pkg/text/template/funcs.go
--- a/src/pkg/text/template/funcs.go  Tue Jul 17 07:56:25 2012 +0200
+++ b/src/pkg/text/template/funcs.go  Sun Jul 22 12:07:02 2012 +0200
@@ -128,7 +128,7 @@
       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())

@robpike
Copy link
Contributor

robpike commented Jul 23, 2012

Comment 2:

Labels changed: added priority-soon, packagebug, removed priority-triage.

Owner changed to @robpike.

Status changed to Accepted.

@robpike
Copy link
Contributor

robpike commented Jul 23, 2012

Comment 3:

Your fix is correct; there is an incorrect test that should have caught this.

Status changed to Started.

@robpike
Copy link
Contributor

robpike commented Jul 23, 2012

Comment 4:

This issue was closed by revision ce27433.

Status changed to Fixed.

robpike added a commit that referenced this issue May 11, 2015
««« 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

»»»
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants