aboutsummaryrefslogtreecommitdiff
path: root/src/text/template/exec.go
AgeCommit message (Collapse)Author
2025-02-10text/template: handle UnsafePointer in isTruepgxiaolianzi
Change-Id: I4d0b5919d109f768ba04ab519e8f948a5749a752 GitHub-Last-Rev: 6f27f1193c21bb10e3b81660b4271f2c1f33be1e GitHub-Pull-Request: golang/go#70520 Reviewed-on: https://go-review.googlesource.com/c/go/+/631076 Run-TryBot: Rob Pike <r@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2024-11-18text/template: don't crash piping to call with no argumentsIan Lance Taylor
Fixes #70341 Change-Id: I792b15d5e8d08c3762659fbcdfb3d620b59071ca Reviewed-on: https://go-review.googlesource.com/c/go/+/628096 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Zxilly Chou <zhouxinyu1001@gmail.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2024-11-08encoding/json, text/template: use reflect.Value.Equal instead of ==Emmanuel T Odeke
This change applies a fix for a reflect.Value incorrect comparison using "==" or reflect.DeepEqual. This change is a precursor to the change that'll bring in the static analyzer "reflectvaluecompare", by ensuring that all tests pass beforehand. Updates #43993 Change-Id: I6c47eb0a1de6353ac7495cb8cb49b318b7ebba56 Reviewed-on: https://go-review.googlesource.com/c/go/+/626116 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
2024-10-02text/template: use correct verbs for reflect.ValueJes Cok
Fixes #69708. Change-Id: I7a0347aff75cc8dad3f6c36966b1d03822a78dc9 GitHub-Last-Rev: fcc15513e48ceae68b846e1dff69b459defb2614 GitHub-Pull-Request: golang/go#69713 Reviewed-on: https://go-review.googlesource.com/c/go/+/616695 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-09-26text/template: support range-over-intqiulaidongfeng
Fixes #66107 Change-Id: I19b466e3fb17557cf4f198b7fd8c13e774d854b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/615095 Auto-Submit: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Commit-Queue: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-09-23text/template: support range-over-funcqiulaidongfeng
For #66107 Change-Id: I2fcd04bebe80346dbd244ab7ea09cbe6010b9d8e GitHub-Last-Rev: 5ebf615db5889a04738c555c651e07c1fd287748 GitHub-Pull-Request: golang/go#68329 Reviewed-on: https://go-review.googlesource.com/c/go/+/596956 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-05-24text/template: add detailed info for goodFunc checkZxilly
goodFunc now returns a error describe the exact error it met. builtin call function can print the name of the callee function if the goodFunc check failed. For input {{call .InvalidReturnCountFunc}} before: can't evaluate field InvalidReturnTypeFunc in type *template.T after: invalid function signature for .InvalidReturnTypeFunc: second argument should be error; is bool Change-Id: I9aa53424ac9a2bffbdbeac889390f41218817575 GitHub-Last-Rev: 7c1e0dbd08884a38d92a42530104884a9ca52b44 GitHub-Pull-Request: golang/go#65509 Reviewed-on: https://go-review.googlesource.com/c/go/+/561115 Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2024-05-13fmt, internal/fmtsort: refactor SortedMap to use slice of structs for map ↵aimuz
sorting This change refactors the SortedMap type in the fmtsort package from using two parallel slices for keys and values to a single slice of structs. This improves code clarity and reduces the complexity of handling map entries. Affected files and their respective functions have been updated to work with the new structure, including adjustments in fmt/print.go and text/template/exec.go to iterate over the new map representation. goos: darwin goarch: arm64 pkg: fmt cpu: Apple M2 Max │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ SprintfPadding-12 21.29n ± 5% 20.89n ± 8% ~ (p=0.393 n=10) SprintfEmpty-12 2.986n ± 4% 2.997n ± 10% ~ (p=0.697 n=10) SprintfString-12 8.327n ± 9% 8.493n ± 12% ~ (p=0.579 n=10) SprintfTruncateString-12 15.93n ± 10% 15.56n ± 10% ~ (p=0.853 n=10) SprintfTruncateBytes-12 14.56n ± 12% 14.13n ± 11% ~ (p=0.796 n=10) SprintfSlowParsingPath-12 9.026n ± 15% 9.511n ± 14% ~ (p=0.646 n=10) SprintfQuoteString-12 40.88n ± 3% 40.73n ± 1% ~ (p=0.782 n=10) SprintfInt-12 6.279n ± 7% 6.130n ± 6% ~ (p=0.218 n=10) SprintfIntInt-12 11.08n ± 10% 11.37n ± 10% ~ (p=0.424 n=10) SprintfPrefixedInt-12 31.24n ± 3% 31.21n ± 2% ~ (p=0.912 n=10) SprintfFloat-12 13.96n ± 7% 13.99n ± 15% ~ (p=0.986 n=10) SprintfComplex-12 49.16n ± 7% 50.57n ± 6% ~ (p=0.436 n=10) SprintfBoolean-12 7.578n ± 15% 7.267n ± 11% ~ (p=0.529 n=10) SprintfHexString-12 36.14n ± 2% 35.74n ± 1% ~ (p=0.118 n=10) SprintfHexBytes-12 48.74n ± 1% 48.34n ± 4% ~ (p=0.128 n=10) SprintfBytes-12 60.16n ± 3% 61.36n ± 5% ~ (p=0.218 n=10) SprintfStringer-12 39.02n ± 10% 39.31n ± 9% ~ (p=0.739 n=10) SprintfStructure-12 161.2n ± 1% 133.9n ± 4% -16.90% (p=0.000 n=10) ManyArgs-12 31.87n ± 17% 33.00n ± 12% ~ (p=0.165 n=10) FprintInt-12 32.32n ± 0% 33.13n ± 1% +2.49% (p=0.000 n=10) FprintfBytes-12 47.31n ± 0% 47.99n ± 1% +1.44% (p=0.000 n=10) FprintIntNoAlloc-12 32.05n ± 1% 33.12n ± 0% +3.34% (p=0.000 n=10) ScanInts-12 130.5µ ± 1% 131.3µ ± 0% +0.57% (p=0.000 n=10) ScanRecursiveInt-12 40.83m ± 1% 40.65m ± 2% ~ (p=0.353 n=10) ScanRecursiveIntReaderWrapper-12 40.77m ± 2% 40.83m ± 2% ~ (p=0.971 n=10) geomean 100.6n 100.3n -0.32% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ SprintfPadding-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfEmpty-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfString-12 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateString-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateBytes-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfSlowParsingPath-12 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfQuoteString-12 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfIntInt-12 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfPrefixedInt-12 64.00 ± 0% 64.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfFloat-12 8.000 ± 0% 8.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfComplex-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfBoolean-12 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexString-12 80.00 ± 0% 80.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexBytes-12 104.0 ± 0% 104.0 ± 0% ~ (p=1.000 n=10) ¹ SprintfBytes-12 88.00 ± 0% 88.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfStringer-12 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfStructure-12 216.0 ± 0% 168.0 ± 0% -22.22% (p=0.000 n=10) ManyArgs-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintfBytes-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ FprintIntNoAlloc-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ScanInts-12 14.87Ki ± 0% 14.87Ki ± 0% ~ (p=1.000 n=10) ¹ ScanRecursiveInt-12 16.37Ki ± 0% 16.34Ki ± 9% ~ (p=0.950 n=10) ScanRecursiveIntReaderWrapper-12 16.43Ki ± 8% 16.35Ki ± 0% ~ (p=0.052 n=10) geomean ² -1.03% ² ¹ all samples are equal ² summaries must be >0 to compute geomean │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ SprintfPadding-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfEmpty-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateBytes-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfSlowParsingPath-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfQuoteString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfIntInt-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfPrefixedInt-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfFloat-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfComplex-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfBoolean-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexBytes-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfBytes-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfStringer-12 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfStructure-12 8.000 ± 0% 6.000 ± 0% -25.00% (p=0.000 n=10) ManyArgs-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintfBytes-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ FprintIntNoAlloc-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ScanInts-12 1.590k ± 0% 1.590k ± 0% ~ (p=1.000 n=10) ¹ ScanRecursiveInt-12 1.592k ± 0% 1.592k ± 0% ~ (p=0.303 n=10) ScanRecursiveIntReaderWrapper-12 1.594k ± 0% 1.594k ± 0% ~ (p=0.582 n=10) geomean ² -1.14% ² ¹ all samples are equal ² summaries must be >0 to compute geomean Change-Id: I2e850d827d2fd7d6618db60f7071977af5639032 GitHub-Last-Rev: 5a4afcf045331c6864902e848ededc1562d5fa53 GitHub-Pull-Request: golang/go#67256 Reviewed-on: https://go-review.googlesource.com/c/go/+/584155 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: qiu laidongfeng2 <2645477756@qq.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-03-18text/template: simplify unwrapping reflect.Interface valueJoe Taber
When text/template is evaluating a pipeline command and encounters an `interface{}`, it "digs down one level to the thing inside". Currently it does this with `value = reflect.ValueOf(value.Interface())`, which is unnecessary since it could just use `value = value.Elem()`. This commit changes it to use the latter. Why it was written that way is mysterious because the proposed change appears to be strictly better, but given the blame date (13 years ago) it may have been written while reflect was still in development before `Elem()` was added. Change-Id: I6c4f6283e78de07732c4120ce11f26f113fa46e4 GitHub-Last-Rev: bdfc6973ab227f951f244fda4d803da55fb49e71 GitHub-Pull-Request: golang/go#66373 Reviewed-on: https://go-review.googlesource.com/c/go/+/572355 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
2024-02-26text: add available godoc linkcui fliter
Change-Id: Idbc110cfc4fd6bbbc8b79807ac14abf7b30e0e65 Reviewed-on: https://go-review.googlesource.com/c/go/+/539855 Run-TryBot: shuang cui <imcusg@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: qiulaidongfeng <2645477756@qq.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org>
2023-08-01text/template, html/template: use reflect.TypeFor for known typesIan Lance Taylor
For #60088 Change-Id: Ibc3983ca5cfe396087ddfa96c43cfe32ca47129a Reviewed-on: https://go-review.googlesource.com/c/go/+/514640 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-06-15text/template: set variables correctly in range assignmentIan Lance Taylor
I unintentionally flipped them in CL 446795. For #56490 Fixes #60801 Change-Id: I57586bec052e1b2cc61513870ce24dd6ce17e56b Reviewed-on: https://go-review.googlesource.com/c/go/+/503575 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com>
2022-11-02text/template: correct assignment, not declaration, in rangeIan Lance Taylor
We were mishandling {{range $i = .}}, treating it as though it were {{range $i := .}}. That happened to work if $i were the most recently declared variable, but not otherwise. Fixes #56490 Change-Id: I222a009d671d86c06a980a54388e05f12101c00b Reviewed-on: https://go-review.googlesource.com/c/go/+/446795 Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2022-08-22text/template: compare reflect.Value instances differentlyKeith Randall
To avoid false positives from the reflectvaluecompare checker #43993 Use v.IsValid() instead of var zero reflect.Value v != zero Also avoid comparing directly with the singleton reflect.Value representing a missing value. Detect the missing value by type instead. Change-Id: I3a00d63cf61c077e7c7ae816474aa1f032be325b Reviewed-on: https://go-review.googlesource.com/c/go/+/308769 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Ian Lance Taylor <iant@google.com>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-10-29text/template: use reflect.Value.FieldByIndexErrRob Pike
to avoid a crash on a nil pointer as an embedded field. Fixes #48215 Change-Id: I214faa6e3cf08cdec1c01035e4bbca0900c6a408 Reviewed-on: https://go-review.googlesource.com/c/go/+/357963 Trust: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-10-26all: use reflect.{Pointer,PointerTo}Cuong Manh Le
Updates #47651 Updates #48665 Change-Id: I69a87b45a5cad7a07fbd855040cd9935cf874554 Reviewed-on: https://go-review.googlesource.com/c/go/+/358454 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-10-05text/template: only unwrap final and/or valueIan Lance Taylor
In the last CL I missed the fact that except for the final value the code already unwraps the argument. For #31103 Change-Id: Ic9099aeb50c6b3322fc14a90ac8026c1d8cb1698 Reviewed-on: https://go-review.googlesource.com/c/go/+/354091 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-10-01text/template: undo reflect.Value wrapping for short-circuit and/orIan Lance Taylor
For #31103 Change-Id: I9c0aa64f95f564de31a4c178e3930584d41316bb Reviewed-on: https://go-review.googlesource.com/c/go/+/353610 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-09-29text/template: check final value for short-circuit and/orIan Lance Taylor
There was a bug in the short-circuit code for and/or added in CL 321490: it ignored the value passed in by an earlier pipeline. For #31103 Change-Id: Ic31f4d7cedfe563ef968cbb712ecfb2413c42eb5 Reviewed-on: https://go-review.googlesource.com/c/go/+/353130 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-09-23html/template, text/template: implement break and continue for range loopsRuss Cox
Break and continue for range loops was accepted as a proposal in June 2017. It was implemented in CL 66410 (Oct 2017) but then rolled back in CL 92155 (Feb 2018) because html/template changes had not been implemented. This CL reimplements break and continue in text/template and then adds support for them in html/template as well. Fixes #20531. Change-Id: I05330482a976f1c078b4b49c2287bd9031bb7616 Reviewed-on: https://go-review.googlesource.com/c/go/+/321491 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2021-09-21text/template: implement short-circuit and, orRuss Cox
Making the builtin and and or functions use short-circuit evaluation was accepted as a proposal in April 2019, but we never got around to implementing it. Do that. Fixes #31103. Change-Id: Ia43d4a9a6b0ab814f2dd3471ebaca3e7bb1505cf Reviewed-on: https://go-review.googlesource.com/c/go/+/321490 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2021-05-04text/template: add lock for Template.tmpl to fix data raceIan Lance Taylor
This adds a new lock protecting "tmpl". This is a copy of https://golang.org/cl/257817 by Andreas Fleig, updated for current tip, and updated to start running the html/template TestEscapeRace test. Thanks to @bep for providing the test case. Fixes #39807 Change-Id: Ic8874484290283a49116812eeaffb8608346dc70 Reviewed-on: https://go-review.googlesource.com/c/go/+/316669 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2021-03-05text/template: wrap errors returned by template functions instead of ↵Adrien Delorme
stringifying them Fixes #34201 Change-Id: Ic2e2967e4b01167345cf38bd006cabb206a64377 GitHub-Last-Rev: 5d0c4856550614484a8dbfb68c37aa1abcfcc529 GitHub-Pull-Request: golang/go#42398 Reviewed-on: https://go-review.googlesource.com/c/go/+/267838 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Trust: Daniel Martí <mvdan@mvdan.cc> Trust: Pontus Leitzler <leitzler@gmail.com> Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Go Bot <gobot@golang.org>
2021-02-25reflect: add Method.IsExported and StructField.IsExported methodsJoe Tsai
The IsExported method is a more intuitive helper for checking whether the method or field is exported than checking whether PkgPath is empty. In the same CL, modify the standard library to make use of this helper. Fixes #41563 Change-Id: Iaacfb3b74449501f98e2707aa32095a32bd3c3c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/266197 Trust: Joe Tsai <joetsai@digital-static.net> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-12-15text/template: error on range over send channelMeng Zhuo
template range require channel contains RecvDir if recv on send only channel will raise an panic. Fixes #43065 Change-Id: Ie0ea70ce60e074bf8c9f2378e07ef1d4c41dc38f Reviewed-on: https://go-review.googlesource.com/c/go/+/276532 Trust: Meng Zhuo <mzh@golangcn.org> Run-TryBot: Meng Zhuo <mzh@golangcn.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
2020-08-28text/template: add CommentNode to template parse treeAriel Mashraki
Fixes #34652 Change-Id: Icf6e3eda593fed826736f34f95a9d66f5450cc98 Reviewed-on: https://go-review.googlesource.com/c/go/+/229398 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-11-12text/template: add error check for parenthesized first argument in pipelineRob Pike
An error check was missing: If the first argument of a pipeline is parenthesized, and the pipeline has further arguments, then syntactically the pipeline is a function invocation and there must be a "call". Tricky rare corner case, but easily caught. Add the error check and some tests to verify behavior. Fixes #31810. Change-Id: Ica80b7c11284e4ea9e8cc94a01dbbc9a67e42079 Reviewed-on: https://go-review.googlesource.com/c/go/+/206124 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-09-25text/template: don't evaluate '.' as a float64Daniel Martí
When using a '.' constant literal as a reflect.Value variadic argument, idealConstant would incorrectly result in a float64. This is because rune literals can be represented as a float64, and contain a period, which tricked the logic into thinking the literal must have been a floating point number. This also happened with other characters that can be part of a floating point number, such as 'e' or 'P'. To fix these edge cases, exit the case sooner if the literal was a rune, since that should always go to the int case instead. Finally, add test cases that verify that they behave properly. These would error before, since eq would receive a mix of int and float64, which aren't comparable. Fixes #34483. Change-Id: Icfcb7803bfa0cf317a1d1adacacad3d69a57eb42 Reviewed-on: https://go-review.googlesource.com/c/go/+/196808 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Tom Payne <tom@airmap.com> Reviewed-by: Rob Pike <r@golang.org>
2019-08-27text/template: replace bytes.Buffer with strings.BuilderDong-hee Na
After Go.1.10+ strings.Builder is known as more efficient in concatenating and building strings than bytes.Buffer. In this CL, there is a minor logic fix for getting advantage of strings.builder. name old time/op new time/op delta DefinedTemplate-8 543ns ± 3% 512ns ± 2% -5.73% (p=0.000 n=8+8) name old alloc/op new alloc/op delta DefinedTemplate-8 192B ± 0% 160B ± 0% -16.67% (p=0.000 n=8+8) name old allocs/op new allocs/op delta DefinedTemplate-8 5.00 ± 0% 5.00 ± 0% ~ (all equal) Change-Id: Icda0054d146e6c5e32ed8a4d13221bb6850d31b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/175261 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-05-14text/template: fix truth handling of typed interface nils in if and withBjørn Erik Pedersen
Before this commit, the two logically equivalent conditionals below would produce different output: {{ if not .NonEmptyInterfaceTypedNil }}OK{{ else }}{{ end }} {{ if .NonEmptyInterfaceTypedNil }}{{ else }}OK{{ end }} The functions `not`, `or`, and `and` all use the same `truth` function, which unwraps any concrete interface value before passing it to `isTrue`. `if` and `with` also use `isTrue` to establish truth, but was missing the interface indirect call. Fixes #30501 Change-Id: I9c49eed41e737d8f162e39bef1c3b82fd5518fed GitHub-Last-Rev: 95fc2c82f26d24a457de4deaa7e5756718fbf07c GitHub-Pull-Request: golang/go#30534 Reviewed-on: https://go-review.googlesource.com/c/go/+/164958 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2019-05-04all: add Unwrap and Is methods to various error typesDamien Neil
Add Unwrap methods to types which wrap an underlying error: "encodinc/csv".ParseError "encoding/json".MarshalerError "net/http".transportReadFromServerError "net".OpError "net".DNSConfigError "net/url".Error "os/exec".Error "signal/internal/pty".PtyError "text/template".ExecError Add os.ErrTemporary. A case could be made for putting this error value in package net, since no exported error types in package os include a Temporary method. However, syscall errors returned from the os package do include this method. Add Is methods to error types with a Timeout or Temporary method, making errors.Is(err, os.Err{Timeout,Temporary}) equivalent to testing the corresponding method: "context".DeadlineExceeded "internal/poll".TimeoutError "net".adrinfoErrno "net".OpError "net".DNSError "net/http".httpError "net/http".tlsHandshakeTimeoutError "net/pipe".timeoutError "net/url".Error Updates #30322 Updates #29934 Change-Id: I409fb20c072ea39116ebfb8c7534d493483870dc Reviewed-on: https://go-review.googlesource.com/c/go/+/170037 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-02-26text/template: error on method calls on nil interfacesDaniel Martí
Trying to call a method on a nil interface is a panic in Go. For example: var stringer fmt.Stringer println(stringer.String()) // nil pointer dereference In https://golang.org/cl/143097 we started recovering panics encountered during function and method calls. However, we didn't handle this case, as text/template panics before evalCall is ever run. In particular, reflect's MethodByName will panic if the receiver is of interface kind and nil: panic: reflect: Method on nil interface value Simply add a check for that edge case, and have Template.Execute return a helpful error. Note that Execute shouldn't just error if the interface contains a typed nil, since we're able to find a method to call in that case. Finally, add regression tests for both the nil and typed nil interface cases. Fixes #30143. Change-Id: Iffb21b40e14ba5fea0fcdd179cd80d1f23cabbab Reviewed-on: https://go-review.googlesource.com/c/161761 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-02-26text/template: improve nil errors in evalFieldDaniel Martí
If we're accessing a field on a nil struct pointer, and that field is present in the type, we should print a "nil pointer evaluating X.Y" error instead of the broader "can't evaluate field Y in X". The latter error should still be used for the cases where the field is simply missing. While at it, remove the isNil checks in the struct and map cases. The indirect func will only return a true isNil when returning a pointer or interface reflect.Value, so it's impossible for either of these checks to be useful. Finally, extend the test suite to test a handful of these edge cases, including the one shown in the original issue. Fixes #29137. Change-Id: I53408ced8a7b53807a0a8461b6baef1cd01d25ae Reviewed-on: https://go-review.googlesource.com/c/153341 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2019-02-26text/template: accept new number syntaxRuss Cox
This CL updates text/template's scanner to accept the new number syntaxes: - Hexadecimal floating-point values. - Digit-separating underscores. - Leading 0b and 0o prefixes. See golang.org/design/19308-number-literals for background. For #12711. For #19308. For #28493. For #29008. Change-Id: I68c16ea35c3f506701063781388de72bafee6b8d Reviewed-on: https://go-review.googlesource.com/c/160248 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2018-10-24text/template: recover panics during function callsDaniel Martí
There's precedent in handling panics that happen in functions called from the standard library. For example, if a fmt.Formatter implementation fails, fmt will absorb the panic into the output text. Recovering panics is useful, because otherwise one would have to wrap some Template.Execute calls with a recover. For example, if there's a chance that the callbacks may panic, or if part of the input data is nil when it shouldn't be. In particular, it's a common confusion amongst new Go developers that one can call a method on a nil receiver. Expecting text/template to error on such a call, they encounter a long and confusing panic if the method expects the receiver to be non-nil. To achieve this, introduce safeCall, which takes care of handling error returns as well as recovering panics. Handling panics in the "call" function isn't strictly necessary, as that func itself is run via evalCall. However, this makes the code more consistent, and can allow for better context in panics via the "call" function. Finally, add some test cases with a mix of funcs, methods, and func fields that panic. Fixes #28242. Change-Id: Id67be22cc9ebaedeb4b17fa84e677b4b6e09ec67 Reviewed-on: https://go-review.googlesource.com/c/143097 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-10-19text/template: drop unused sortKeys functionSeebs
Recent change golang.org/cl/142737 drops the only call site for the sortKeys function. If it's not in use, it should probably not be there in the code, lurking and preparing to bite us when someone calls that instead of the new key sorter in fmtsort, resulting in strange inconsistencies. Since the function isn't called, this should have no impact. Related to, but does not fix, #21095. Change-Id: I4695503ef4d5ce90d989ec952f01ea00cc15c79d Reviewed-on: https://go-review.googlesource.com/c/143178 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-10-18fmt: print maps in key-sorted orderRob Pike
For easier testing, change the way maps are printed so they appear in a consistent order between runs. Do this by printing them in key-sorted order. To do this, we add a package at the root, internal/fmtsort, that implements a general mechanism for sorting map keys regardless of their type. This is a little messy and probably slow, but formatted printing of maps has never been fast and is already always reflection-driven. The rules are: The ordering rules are more general than with Go's < operator: - when applicable, nil compares low - ints, floats, and strings order by < - NaN compares less than non-NaN floats - bool compares false before true - complex compares real, then imag - pointers compare by machine address - channel values compare by machine address - structs compare each field in turn - arrays compare each element in turn. - interface values compare first by reflect.Type describing the concrete type and then by concrete value as described in the previous rules. The new package is internal because we really do not want everyone using this to sort things. It is slow, not general, and only suitable for the subset of types that can be map keys. Also use the package in text/template, which already had a weaker version of this mechanism. This change requires adding a dependency on sort to the fmt package, but that isn't disruptive to the dependency tree. Fixes #21095 Change-Id: Ia602115c7de5d95993dbd609611d8bd96e054157 Reviewed-on: https://go-review.googlesource.com/c/142737 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2018-09-26all: use strings.ReplaceAll and bytes.ReplaceAll where applicableBrad Fitzpatrick
I omitted vendor directories and anything necessary for bootstrapping. (Tested by bootstrapping with Go 1.4) Updates #27864 Change-Id: I7d9b68d0372d3a34dee22966cca323513ece7e8a Reviewed-on: https://go-review.googlesource.com/137856 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-06-22text/template/parse: undo breaking API changesDaniel Theophanes
golang.org/cl/84480 altered the API for the parse package for clarity and consistency. However, the changes also broke the API for consumers of the package. This CL reverts the API to the previous spelling, adding only a single new exported symbol. Fixes #25968 Change-Id: Ieb81054b61eeac7df3bc3864ef446df43c26b80f Reviewed-on: https://go-review.googlesource.com/120355 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-30all: skip unsupported tests for js/wasmRichard Musiol
The general policy for the current state of js/wasm is that it only has to support tests that are also supported by nacl. The test nilptr3.go makes assumptions about which nil checks can be removed. Since WebAssembly does not signal on reading a null pointer, all nil checks have to be explicit. Updates #18892 Change-Id: I06a687860b8d22ae26b1c391499c0f5183e4c485 Reviewed-on: https://go-review.googlesource.com/110096 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04text/template: add variable assignmentsDaniel Martí
Variables can be declared and shadowing is supported, but modifying existing variables via assignments was not available. This meant that modifying a variable from a nested block was not possible: {{ $v := "init" }} {{ if true }} {{ $v := "changed" }} {{ end }} v: {{ $v }} {{/* "init" */}} Introduce the "=" assignment token, such that one can now do: {{ $v := "init" }} {{ if true }} {{ $v = "changed" }} {{ end }} v: {{ $v }} {{/* "changed" */}} To avoid confusion, rename PipeNode.Decl to PipeNode.Vars, as the variables may not always be declared after this change. Also change a few other names to better reflect the added ambiguity of variables in pipelines. Modifying the text/template/parse package in a backwards incompatible manner is acceptable, given that the package godoc clearly states that it isn't intended for general use. It's the equivalent of an internal package, back when internal packages didn't exist yet. To make the changes to the parse package sit well with the cmd/api test, update except.txt with the changes that we aren't worried about. Fixes #10608. Change-Id: I1f83a4297ee093fd45f9993cebb78fc9a9e81295 Reviewed-on: https://go-review.googlesource.com/84480 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-02-19text/template: differentiate nil from missing argDaniel Martí
reflect.Value is a struct and does not have a kind nor any flag for untyped nils. As a result, it is tricky to differentiate when we're missing a value, from when we have one but it is untyped nil. We could start using *reflect.Value instead, to add one level of indirection, using nil for missing values and new(reflect.Value) for untyped nils. However, that is a fairly invasive change, and would also mean unnecessary allocations. Instead, use a special reflect.Value that depicts when a value is missing. This is the case for the "final" reflect.Value in multiple scenarios, such as the start of a pipeline. Give it a specific, unexported type too, to make sure it cannot be mistaken for any other valid value. Finally, replace "final.IsValid()" with "final != missingVal", since final.IsValid() will be false when final is an untyped nil. Also add a few test cases, all different variants of the untyped nil versus missing value scenario. Fixes #18716. Change-Id: Ia9257a84660ead5a7007fd1cced7782760b62d9d Reviewed-on: https://go-review.googlesource.com/95215 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-02-13text/template: never call reflect.Zero(nil)Daniel Martí
It makes no sense to try to get the zero value of a nil type, hence the panic. When we have a nil type, use reflect.ValueOf(nil) instead. This was showing itself if one used a missing field on the data between parentheses, when the data was a nil interface: t := template.Must(template.New("test").Parse(`{{ (.).foo }}`)) var v interface{} t.Execute(os.Stdout, v) Resulting in: panic: reflect: Zero(nil) [recovered] panic: reflect: Zero(nil) Fixes #21171. Change-Id: Ifcc4a0c67e6df425b65bc9f82fde6fcf03828579 Reviewed-on: https://go-review.googlesource.com/84482 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-02-06text/template: revert CL 66410 "add break, continue actions in ranges"Ian Lance Taylor
The new break and continue actions do not work in html/template, and fixing them requires thinking about security issues that seem too tricky at this stage of the release. We will try again for 1.11. Original CL description: text/template: add break, continue actions in ranges Adds the two range control actions "break" and "continue". They act the same as the Go keywords break and continue, but are simplified in that only the innermost range statement can be broken out of or continued. Fixes #20531 Updates #20531 Updates #23683 Change-Id: Ia7fd3c409163e3bcb5dc42947ae90b15bdf89853 Reviewed-on: https://go-review.googlesource.com/92155 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2017-10-17text/template: add break, continue actions in rangesTim Cooper
Adds the two range control actions "break" and "continue". They act the same as the Go keywords break and continue, but are simplified in that only the innermost range statement can be broken out of or continued. Fixes #20531 Change-Id: I4412b3bbfd4dadb0ab74ae718e308c1ac7a0a1e9 Reviewed-on: https://go-review.googlesource.com/66410 Reviewed-by: Rob Pike <r@golang.org>
2017-09-22all: use sort.Slice in a few more placesDaniel Martí
Do the low-hanging fruit - tiny Less functions that are used exactly once. This reduces the amount of code and puts the logic in a single place. Change-Id: I9d4544cd68de5a95e55019bdad1fca0a1dbfae9c Reviewed-on: https://go-review.googlesource.com/63171 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-09-12all: remove strings.Contains check around ReplaceDaniel Martí
It doesn't change the outcome. It might have been useful at some point to avoid Replace from doing work or allocating. However, nowadays the func returns early without doing any work if Count returns 0. Change-Id: Id69dc74042a6e39672b405016484db8b50f43d58 Reviewed-on: https://go-review.googlesource.com/62991 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dave Cheney <dave@cheney.net>
2017-05-05template: warn about interleaved nature of writesDieter Plaetinck
Execute incurs separate writes for each "step", e.g. each variable that needs to be printed, and the final newline. While it is correct to state that templates can be executed concurrently, there is a more subtle nuance that is easily missed: when writing to the same writer, the writes from concurrent execute calls can be interleaved, leading to unexpected output. Change-Id: I0abbd7960d8a8d15e109a8a3eeff3b43b852bbbf Reviewed-on: https://go-review.googlesource.com/37444 Reviewed-by: Rob Pike <r@golang.org>
2017-02-09text/template: fix method lookup on addressable nil pointerRuss Cox
Fixes #18816. Change-Id: I4f8f1cac2680dbde492c56d3a5a038577605e7c1 Reviewed-on: https://go-review.googlesource.com/36542 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>