Skip to content

proposal: text/template: slice spread functionality in template func context #51674

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

Closed
ddemoss222 opened this issue Mar 15, 2022 · 5 comments
Closed

Comments

@ddemoss222
Copy link

Problem: Using variadic functions within text/templates there presently not a clean way to drop a defined slice into that function. Attempts to do so result in:

wrong type for value. Expected string, got []string

Direct usage of the spread operator valid in a go code context leads to a syntax error within a template context:

unexpected <.> in operand

I can imagine two solutions to this problem. Either a "spread" builtin template function that can be dropped ahead of the struct to "spread" the a generic slice in a manner identical to the ellipsis notation but valid within a template context, or automatic type flexibility that allows variadic template funcs to process slices of the same type T as though they were valid variadic types.

Present Workaround:
Presently the only way I am aware of to work around this problem is to create a func wrapper.

// variadic func in question
func variadicFunc(s ...string) output {
   ....
}

func wrapperFunc(s []string) output {
   return variadicFunc(s...)
}

If there are better workarounds, let me know. Otherwise, my humble thanks for considering this feature request.

@gopherbot gopherbot added this to the Proposal milestone Mar 15, 2022
@ianlancetaylor ianlancetaylor changed the title proposal: Slice spread functionality in template func context. affected/package: text/template, html/template proposal: text/template: slice spread functionality in template func context Mar 15, 2022
@ianlancetaylor
Copy link
Contributor

CC @robpike

@robpike
Copy link
Contributor

robpike commented Mar 15, 2022

I'm not quite sure what is being asked. Is the "spread" operator the "..." token? A complete example that demonstrates the problem and what a solution might be would help.

If the request is to add "..." to the template language, I don't think this is necessary or desirable. It's easy enough, as offered already, to do this with a custom function.

@ddemoss222
Copy link
Author

ddemoss222 commented Mar 15, 2022

My particular use case is in calling methods that use an "options" pattern that parses variadic function arguments to establish the method's options struct. In certain cases, when a pattern of similar usage emerges I have found a value in establishing reusable slices containing a grouping of options.

type objectOptions struct {
	...
}

func (o Object) Procedure(opts ...string) string{
var options objectOptions
	// parse opts -> options
	...
}

func (o Object) CommonBehavior() []string {
	return []string{"option1", "option2", "option3"}
}

obj := Object{}
tmpl, _ := template.New("test").Parse(`
	Ad Hoc Procedure: {{.Procedure "option1" "option2"}}
	Repeated Procedure: {{.Procedure (spread .CommonBehavior)}}`)
_ = tmpl.Execute(os.Stdout, obj)

The addition of the above as a built in template func could serve to bridge a gap between the template context and go context. Thank you for considering this proposal.

@robpike
Copy link
Contributor

robpike commented Mar 15, 2022

Just have Procedure (or a variant) take a slice. The variadic touch here is unnecessary.

Here's the variant:

func (o Object) SliceProcedure(opts []string) string { return o.Procedure(opts...) }

That's so easy I don't see the need for a new feature in the template libraries. And since you already wrote essentially that in your original proposal, it's doubly clear that there is no semantic gap here.

This is not a workaround, it's idiomatic Go.

@rsc
Copy link
Contributor

rsc commented Mar 16, 2022

This proposal has been declined as retracted.
— rsc for the proposal review group

@rsc rsc moved this to Declined in Proposals Aug 10, 2022
@rsc rsc added this to Proposals Aug 10, 2022
@rsc rsc removed this from Proposals Mar 15, 2023
@golang golang locked and limited conversation to collaborators Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants