英文:
Is there a better way where I can check if a template property was not resolved?
问题
我正在尝试使用text/template构建一个字符串,其中模板字符串可以具有通过映射解析的任意属性。
我想要实现的目标是识别模板属性中是否有任何一个未解析,并返回一个错误。
目前,我正在使用regexp,但我想向社区寻求是否有更好的解决方案。
package main
import (
	"bytes"
	"fmt"
	"regexp"
	"text/template"
)
func main() {
	data := "teststring/{{.someData}}/{{.notExist}}/{{.another}}"
	// 这里的问题是data可以是任意的,所以我不能使用很多未知的if语句
	t := template.Must(template.New("").Parse(data))
	var b bytes.Buffer
	fillers := map[string]interface{}{
		"someData": "123",
		"another":  true,
		// 在这种情况下,notExist未定义,因此模板将无法解析它
	}
	if err := t.Execute(&b, fillers); err != nil {
		panic(err)
	}
	fmt.Println(b.String())
	// teststring/123/<no value>/true
	// 这里我试图捕捉模板是否需要一个未提供的值
	hasResolved := regexp.MustCompile(`<no value>`)
	fmt.Println(hasResolved.MatchString(b.String()))
	// 将notExist添加到fillers映射中
	fillers["notExist"] = "testdata"
	b.Reset()
	if err := t.Execute(&b, fillers); err != nil {
		panic(err)
	}
	fmt.Println(b.String())
	fmt.Println(hasResolved.MatchString(b.String()))
	// 输出:
	// teststring/123/<no value>/true
	// true
	// teststring/123/testdata/true
	// false
}
英文:
I am trying to build a string using text/template, where the template string could have arbitrary properties that are resolved via a map.
What I am trying to accomplish is identifying where one/any of the template properties is not resolved and return an error.
At the moment, I am using regexp but reaching out to the community of see if there was a better solution.
package main
import (
	"bytes"
	"fmt"
	"regexp"
	"text/template"
)
func main() {
	data := "teststring/{{.someData}}/{{.notExist}}/{{.another}}"
	// the issue here is that data can be arbitrary so i cannot do
	// a lot of unknown if statements
	t := template.Must(template.New("").Parse(data))
	var b bytes.Buffer
	fillers := map[string]interface{}{
		"someData": "123",
		"another":  true,
		// in this case, notExist is not defined, so the template will
		// note resolve it
	}
	if err := t.Execute(&b, fillers); err != nil {
		panic(err)
	}
	fmt.Println(b.String())
	// teststring/123/<no value>/true
	// here i am trying to catch if a the template required a value that was not provided
	hasResolved := regexp.MustCompile(`<no value>`)
	fmt.Println(hasResolved.MatchString(b.String()))
	// add notExist to the fillers map
	fillers["notExist"] = "testdata"
	b.Reset()
	if err := t.Execute(&b, fillers); err != nil {
		panic(err)
	}
	fmt.Println(b.String())
	fmt.Println(hasResolved.MatchString(b.String()))
	// Output:
	// teststring/123/<no value>/true
	// true
	// teststring/123/testdata/true
	// false
}
答案1
得分: 2
你可以通过在模板上设置选项来使其失败:
func (t *Template) Option(opt ...string) *Template
"missingkey=default" 或 "missingkey=invalid"
默认行为:什么都不做,继续执行。
如果打印出来,索引操作的结果是字符串 "<no value>"。
"missingkey=zero"
该操作返回映射类型元素的零值。
"missingkey=error"
立即停止执行并报错。
如果你将其设置为 missingkey=error,你将得到你想要的结果。
t = t.Options("missingkey=error")
英文:
You can let it fail by settings the options on the template:
func (t *Template) Option(opt ...string) *Template
"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.
If you set it to missingkey=error, you get what what want.
t = t.Options("missingkey=error")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论