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: unclear about pipelines as arguments #61029

Open
juriad opened this issue Jun 27, 2023 · 1 comment
Open

text/template: unclear about pipelines as arguments #61029

juriad opened this issue Jun 27, 2023 · 1 comment
Assignees
Labels
Documentation help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@juriad
Copy link

juriad commented Jun 27, 2023

What is the URL of the page with the issue?

https://pkg.go.dev/text/template#hdr-Pipelines

I came here from https://stackoverflow.com/questions/59795596/how-to-make-nested-variables-optional-in-helm

What did you expect to see?

The documentation should change the definition of a Parenthesized Argument to refer to Pipeline instead. It should also describe what happens with the value when it is nil. Similar explanation is missing for Variables.

The reader should be able to understand why the following works and how it derives from the grammar.

{{ $x := .abc }}
{{ $x = $x.def }}
{{ $x = $x.ghi }}
{{ $x }}

{{ ((.abc).def).ghi }}

{{ $y := ((.abc).def) }}
{{ $y.ghi }}

and why {{ .abc.def.ghi }} does not work. This example shows that a Parenthesized Arguments work similarly to Variables.

The description of If Action defines what empty means for the other Actions and Functions. The definition of empty deserves a better place.

What did you see instead?

The documentation of Arguments, Commands and Pipelines is vague up to the point of being incorrect. It requires a lot of imagination to figure out that a Pipeline can be used as an Argument. It is hinted by the examples:

A parenthesized instance of one the above, for grouping. The result may be accessed by a field or map key invocation. print (.F1 arg1) (.F2 arg2) (.StructValuedMethod "arg").Field

Clearly .F1 arg1 is neither of the "above". It looks like a Method Call which is defined later under Command. The other example is:

{{printf "%q" (print "out" "put")}}
A parenthesized argument.

where the parentheses enclose a Function Call (which is a Command and not an Argument).

The SO reply author quotes

If the value of the pipeline is empty, no output is generated... The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero.

for the reason why the parentheses turn an empty value into no output which is then chained. This behavior is mentioned only within If Action and With Action, where I believe it describes the consequence of T1 not being executed.

@juriad juriad added the pkgsite label Jun 27, 2023
@gopherbot gopherbot added this to the Unreleased milestone Jun 27, 2023
@seankhliao seankhliao changed the title x/pkgsite: text/template is unclear about pipelines as arguments text/template is unclear about pipelines as arguments Jun 27, 2023
@seankhliao seankhliao changed the title text/template is unclear about pipelines as arguments text/template: unclear about pipelines as arguments Jun 27, 2023
@seankhliao seankhliao modified the milestones: Unreleased, Backlog Jun 27, 2023
@seankhliao seankhliao added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed pkgsite labels Jun 27, 2023
@ianlancetaylor
Copy link
Contributor

CC @robpike

@robpike robpike self-assigned this Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants