diff options
| author | Özgür Kesim <oec-go@kesim.org> | 2016-10-21 13:14:57 +0200 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2016-11-18 15:56:02 +0000 |
| commit | 277bcbbdcd26f2d64493e596238e34b47782f98e (patch) | |
| tree | 862c5151f6c57bb3153ccd5d0d02c9576083a754 /src/text/template/exec.go | |
| parent | 348275cda626edf60e1d11a83b2e78d83088ccef (diff) | |
| download | go-277bcbbdcd26f2d64493e596238e34b47782f98e.tar.xz | |
text/template: handle option missingkey=error consistently
The existing implementation of text/template handles the option
"missingkey=error" in an inconsitent manner: If the provided data is
a nil-interface, no error is returned (despite the fact that no key
can be found in it).
This patch makes text/template return an error if "missingkey=error"
is set and the provided data is a not a valid reflect.Value.
Fixes #15356
Change-Id: Ia0a83da48652ecfaf31f18bdbd78cb21dbca1164
Reviewed-on: https://go-review.googlesource.com/31638
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/text/template/exec.go')
| -rw-r--r-- | src/text/template/exec.go | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/src/text/template/exec.go b/src/text/template/exec.go index 49f15faacd..ea964dc2bc 100644 --- a/src/text/template/exec.go +++ b/src/text/template/exec.go @@ -541,6 +541,9 @@ func (s *state) evalFunction(dot reflect.Value, node *parse.IdentifierNode, cmd // value of the pipeline, if any. func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node, args []parse.Node, final, receiver reflect.Value) reflect.Value { if !receiver.IsValid() { + if s.tmpl.option.missingKey == mapError { // Treat invalid value as missing map key. + s.errorf("nil data; no entry for key %q", fieldName) + } return zero } typ := receiver.Type() |
