-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: missingkey=zero ignored for map[string]interface{} #24963
Comments
My current workaround. It seems that if treated as an error, the right behaviour ensues.
|
I've been poking around a bit. The good news is that this edge case doesn't have any tests, so I don't think that the current behavior is locked into place. The only However, I am not convinced whether this is a good idea. I can understand that the code you provided behaves in a somewhat confusing way, but I imagine that forcing Take, for example, pipes. In those, a value is passed from one command to the next. In that scenario, passing So I suspect that applying your change could break pieces of code with Something else that comes to mind is that you could use a workaround for this very specific edge case - that is, the edge case of needing zero values to be "stringified". I have written a simple example here: https://play.golang.org/p/f6hs0UiLX-I
Note that the example fails on 1.10 due to a bug with passing nil values to template funcs. It is fixed in master and will ship with 1.11, though: #18716 In summary, I don't think there's anything we can do here. Paging @robpike @rsc @ianlancetaylor to see what others think. |
How about providing an option to allow the user to define their own behaviour through a function call? This would allow non-zero result substitution etc. |
You mean an alternative to Such a suggestion should be a proposal; see https://github.com/golang/proposal. |
I think that decision is best left up to the designer of the template engine. To me, it seems like an obvious feature that is missing which is to set a default value when a key is missing and not just substitute a zero value. This is done with flags in the case where a command line option is not provided there is the ability to set a default value. |
Well, I'm precisely asking because I don't see an addition to The only thing that potentially comes to mind is
That would fix your initial example, I believe. /cc @robpike, who is likely very opinionated about text/template's design. |
missingkey=zero for an interface{} is a nil interface{}, which formats as <no value>. Sorry, but that's how the zero formats. The option is doing what it's documented to do. |
@rsc any thoughts on my |
@rsc - Personally I think setting the output as "" for a nil interface{} which eventually ends up as a piece of text as the result is not a good representation of no value. But an empty string "" (empty string) is. But since you guys don't want to change it, people will forever be having to code around this. |
…templates Seems like we are affected by golang/go#24963. That is, even though we internally use the template option `missingkey=zero`, in some cases it still prints `<no value>` instead of zero values, which has been confusing the state yaml parsing. This fixes the issue by naively replacing all the remaining occurrences of `<no value>` in the rendered text, while printing debug logs to ease debugging in the future when there is unexpected side-effects introduced by this native method. Fixes #553
…templates Seems like we are affected by golang/go#24963. That is, even though we internally use the template option `missingkey=zero`, in some cases it still prints `<no value>` instead of zero values, which has been confusing the state yaml parsing. This fixes the issue by naively replacing all the remaining occurrences of `<no value>` in the rendered text, while printing debug logs to ease debugging in the future when there is unexpected side-effects introduced by this native method. Fixes #553
…templates Seems like we are affected by golang/go#24963. That is, even though we internally use the template option `missingkey=zero`, in some cases it still prints `<no value>` instead of zero values, which has been confusing the state yaml parsing. This fixes the issue by naively replacing all the remaining occurrences of `<no value>` in the rendered text, while printing debug logs to ease debugging in the future when there is unexpected side-effects introduced by this native method. Fixes #553
…templates (#645) Seems like we are affected by golang/go#24963. That is, even though we internally use the template option `missingkey=zero`, in some cases it still prints `<no value>` instead of zero values, which has been confusing the state yaml parsing. This fixes the issue by naively replacing all the remaining occurrences of `<no value>` in the rendered text, while printing debug logs to ease debugging in the future when there is unexpected side-effects introduced by this native method. Fixes #553
If the missingkey=zero option is used in text/template expansion, then the result will be
<no value>
where the input data is interface{} and not string or text.In the case of a map[string]int, the value of a literal zero will be inserted. This makes sense.
For map[string]string, then an empty string is appropriate.
For map[string]interface{} it puts
<no value>
. Which in my opinion makes no sense. The template itself is all text. It would be most appropriate to use an empty string if it's not an integer. Or to allow defining what value to put in it's place if the key is missingWhat version of Go are you using (
go version
)?go version go1.10.1 linux/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?Ubuntu 14.04
What did you do?
What did you expect to see?
Template text: APP_VERSION={{.AppVersion}}
Expanded: APP_VERSION=
What did you see instead?
Template text: APP_VERSION={{.AppVersion}}
Expanded: APP_VERSION=<no value>
The text was updated successfully, but these errors were encountered: