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: template function index should permit integer values of float type #17236

Closed
willfaught opened this issue Sep 26, 2016 · 3 comments
Closed

Comments

@willfaught
Copy link
Contributor

@willfaught willfaught commented Sep 26, 2016

The text/template function index should permit integer values of float type as indexes, since there's no way to convert types in templates themselves.

I hit this particular problem when using unmarshaled JSON as the template data, similar to what's done in the play link below, where an integer in one part refers to an index of a list in another part, but when parsed, the integer is converted to float64.

Note that the function eq is also loose with types.

What did you do?

https://play.golang.org/p/_9K1wu6THk

What did you expect to see?

9

What did you see instead?

template: :1:2: executing "" at <index .a .b>: error calling index: cannot index slice/array with type float64
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Sep 26, 2016

You should probably unmarshal into a concrete type, instead of unmarshalling into interface{}.

I don't think this is a change we want to make. I'm closing this, but I'll let @robpike or @adg reopen if they feel otherwise.

@bradfitz bradfitz closed this Sep 26, 2016
@willfaught
Copy link
Contributor Author

@willfaught willfaught commented Sep 26, 2016

@bradfitz In my case, the JSON can be anything, hence why it unmarshals into interface{}. I suppose this might be an edge case, but it seems like we already went down this road with eq...

@robpike
Copy link
Contributor

@robpike robpike commented Sep 27, 2016

Happy to keep it closed.

@golang golang locked and limited conversation to collaborators Sep 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.