diff options
| author | Russ Cox <rsc@golang.org> | 2016-10-19 09:11:16 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2016-10-19 18:20:35 +0000 |
| commit | 5378dd77684e2eee5f05aab4b77497bb635fd544 (patch) | |
| tree | 19726011b7167528fd9d290bf396cbc389281a30 /src/text/template/exec_test.go | |
| parent | d2aa8601b5aafa9736f8e49ca713ecc31f9a011f (diff) | |
| download | go-5378dd77684e2eee5f05aab4b77497bb635fd544.tar.xz | |
text/template: add support for reflect.Value args, results in funcs
Add support for passing reflect.Values to and returning reflect.Values from
any registered functions in the FuncMap, much as if they were
interface{} values. Keeping the reflect.Value instead of round-tripping
to interface{} preserves addressability of the value, which is important
for method lookup.
Change index and a few other built-in functions to use reflect.Values,
making a loop using explicit indexing now match the semantics that
range has always had.
Fixes #14916.
Change-Id: Iae1a2fd9bb426886a7fcd9204f30a2d6ad4646ad
Reviewed-on: https://go-review.googlesource.com/31462
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/text/template/exec_test.go')
| -rw-r--r-- | src/text/template/exec_test.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go index 3ef065edcf..7092961850 100644 --- a/src/text/template/exec_test.go +++ b/src/text/template/exec_test.go @@ -1310,3 +1310,26 @@ func TestMaxExecDepth(t *testing.T) { t.Errorf("got error %q; want %q", got, want) } } + +func TestAddrOfIndex(t *testing.T) { + // golang.org/issue/14916. + // Before index worked on reflect.Values, the .String could not be + // found on the (incorrectly unaddressable) V value, + // in contrast to range, which worked fine. + // Also testing that passing a reflect.Value to tmpl.Execute works. + texts := []string{ + `{{range .}}{{.String}}{{end}}`, + `{{with index . 0}}{{.String}}{{end}}`, + } + for _, text := range texts { + tmpl := Must(New("tmpl").Parse(text)) + var buf bytes.Buffer + err := tmpl.Execute(&buf, reflect.ValueOf([]V{{1}})) + if err != nil { + t.Fatal("%s: Execute: %v", text, err) + } + if buf.String() != "<1>" { + t.Fatalf("%s: template output = %q, want %q", text, buf, "<1>") + } + } +} |
