Skip to content

proposal: text/template: New template option additionalkey #57857

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
drewgonzales360 opened this issue Jan 18, 2023 · 4 comments
Closed

proposal: text/template: New template option additionalkey #57857

drewgonzales360 opened this issue Jan 18, 2023 · 4 comments

Comments

@drewgonzales360
Copy link

drewgonzales360 commented Jan 18, 2023

👋 Hey folks, I'd like to add a new option when rendering templates that will error in the event that there are additional fields in the input. The original issue I'm trying to fix is in helm, so if anyone has an alternative solution to that, I'm open to it.

// Known options:
//
// missingkey: Control the behavior during execution if a map is
// indexed with a key that is not present in the map.
//
//	"missingkey=default" or "missingkey=invalid"
//		The default behavior: Do nothing and continue execution.
//		If printed, the result of the index operation is the string
//		"<no value>".
//	"missingkey=zero"
//		The operation returns the zero value for the map type's element.
//	"missingkey=error"
//		Execution stops immediately with an error.
//	"additionalkey=default"                                                      # New thing
//		Do nothing when additional keys are found.
//	"additionalkey=error"                                                        # New thing
//		Execution finishes with an error if a key is provided, but unused.

Then the following code (taken from the text/template example) would panic

package main

import (
	"html/template"
	"os"
)

type Inventory struct {
	Material   string
	Count      uint
	ExtraField string
}

func main() {
	sweaters := Inventory{"wool", 17, "unused field"}
	tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}}")
	
	// Throw an error if extra fields are found. In this case ExtraField is unused.
	tmpl.Option("additionalkey=error")

	if err != nil {
		panic(err)
	}
	err = tmpl.Execute(os.Stdout, sweaters)
	if err != nil {
		panic(err)
	}
}
@gopherbot gopherbot added this to the Proposal milestone Jan 18, 2023
@drewgonzales360 drewgonzales360 changed the title proposal: text/template: a proposal: text/template: New option additionalkey Jan 18, 2023
@drewgonzales360 drewgonzales360 changed the title proposal: text/template: New option additionalkey proposal: text/template: New template option additionalkey Jan 18, 2023
@seankhliao
Copy link
Member

In helm, everything is under .Values.
As an example, if I use .Values.image.tag, does this count as a usage of .Values ? At what level is "used" counted at?
Also, {{ .Values }} can be a implemented as a function call, it doesn't always have to return the same data, even within the same template. If there's some form of recursive tracking, how will that work?

@seankhliao
Copy link
Member

cc @robpike

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Jan 18, 2023
@robpike
Copy link
Contributor

robpike commented Jan 18, 2023

I do not understand this proposal. How does one "provide" a key to a map yet have it be "unused"? Is the question, if I build a data structure (using a map, but maybe that's not important), pass it to a template, and don't use a field of it during execution, raise an error? If so, that is not something I would be in favor of doing in the template library. It's too peculiar a desire for a general-purpose library, and never a correctness problem in the template itself.

If you must address this, you could use one of the static analyzers if it's the static problem you're after; if it's the dynamic one, you're looking for a solution in the wrong place.

@drewgonzales360
Copy link
Author

Is the question, if I build a data structure (using a map, but maybe that's not important), pass it to a template, and don't use a field of it during execution, raise an error?

Yes, that's exactly the question/proposal.

Thanks for the quick and thoughtful responses. I'll close this issue and find a fix elsewhere.

@golang golang locked and limited conversation to collaborators Jan 18, 2024
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

4 participants