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: (and html/template) execution option for given errors for missing map keys #6288

Closed
gopherbot opened this issue Aug 30, 2013 · 16 comments
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@gopherbot
Copy link

by bjruth:

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.

http://play.golang.org/p/zVTrueV2dd

Note the '<no value>' value for lastName.

What is the expected output?

I expect fields used in the template that are missing in the map should return an error
(missing key) rather than render '<no value>'

Please provide any additional information below.

Discussion on golang-nuts:
https://groups.google.com/forum/#!topic/golang-nuts/VPhthY-ocsg

An ideal solution would be a Template.ExecuteStrict method that returns an error rather
than replacing missing keys/fields with '<no value>' in the output (as it does
now). This ensures nothing breaks with programs using Template.Execute in Go 1.x
@robpike
Copy link
Contributor

robpike commented Aug 30, 2013

Comment 1:

It's too close to make the 1.2 deadline but should happen for 1.3.

Labels changed: added priority-later, feature, removed priority-triage.

Owner changed to @robpike.

Status changed to Accepted.

@robpike
Copy link
Contributor

robpike commented Aug 30, 2013

Comment 2:

Not sure what the design should be. It could also be a function on the template that
enables this feature.

@gopherbot
Copy link
Author

Comment 3 by bjruth:

One suggestion from the thread was for the template engine to support the Getter
interface on data. So {{.foo}} would resolve to data.Get("foo"). If a error is returned,
it would be returned.

@gopherbot
Copy link
Author

Comment 4 by bjruth:

That last statement was not useful :) If and error is returned from data.Get("foo"), it
will be returned on Execute.

@gopherbot
Copy link
Author

Comment 5 by bjruth:

A more complete explanation of my first comment: Execute can fall back to using the
Getter interface if data is a struct and does not contain the field. So {{.Foo}} would
resolve to data.Foo if defined and fall back to data.Get("Foo") if implemented. This
enables custom handling in the Get method and the ability to return an error like method
calls. This _could_ remove the need to implement a separate ExecuteStrict for stricter
handling although I believe the default behavior should (eventually) be to return an
error. The Getter support could just be another feature of the template engine.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 6:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 7:

Labels changed: removed feature.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 8:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 9:

Labels changed: added repo-main.

@gopherbot
Copy link
Author

Comment 10 by jd@tekii.com.ar:

There is a possible workaround this, and that is to define your parameter as an
interface and then size the length to see that you are not getting any, and that means
that you got no result from  your map.
http://play.golang.org/p/Kj7iqnqu1x

@robpike
Copy link
Contributor

robpike commented Mar 6, 2014

Comment 11:

Labels changed: added release-go1.3maybe, removed priority-later, release-none.

@rsc
Copy link
Contributor

rsc commented Apr 3, 2014

Comment 12:

Labels changed: added release-none, suggested, removed release-go1.3maybe.

@gopherbot
Copy link
Author

Comment 13 by einthusan@wojka.com:

Hi, I am having the same issue. I guess this didn't make it for Go 1.4 either... really
need a way for template to fail upon missing map keys.

@gopherbot gopherbot added accepted Suggested Issues that may be good for new contributors looking for work to do. labels Dec 6, 2014
@j7b
Copy link

j7b commented Mar 18, 2015

You can implement pretty decent fail and default value behaviors with functions:

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

@robpike robpike closed this as completed in 4e5ac45 Apr 3, 2015
@awly
Copy link
Contributor

awly commented Apr 4, 2015

@robpike What's the rationale of this string-based approach as opposed to Options field in template.Template?
If new options are added you will have to update text/template and html/template anyway.
Struct-based implementation would add compile-time safety and feels cleaner.

@robpike
Copy link
Contributor

robpike commented Apr 4, 2015

It's a simpler API this way and html/template will not need to echo every type and constant.

@mikioh mikioh added this to the Go1.5 milestone Apr 5, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
@rsc rsc unassigned robpike Jun 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

6 participants