Go模板缺失键选项始终返回错误

huangapple go评论75阅读模式
英文:

Go template missingkey option always return error

问题

所以这个问题似乎之前已经被问过几次,但之前的答案都对我没用,我一直从错误到错误,没有结果。

所以,我肯定是漏掉了一些我没有看到的东西,我希望能得到一些帮助:

res, err := os.Create(strings.Replace(f, ".tmpl", "", -1))
if err != nil {
  log.Fatal(err)
}

t, err := template.ParseFiles(f)
if err != nil {
  log.Fatal(err)
}

removes = append(removes, res.Name())

config := make(map[string]string)
for _, v := range vars {
  config[v] = os.Getenv(v)
}

err = t.Execute(res, config)
if err != nil {
  log.Fatal(err)
}

res.Close()

所以解释一下我在做什么,我将一个字符串传递给一个具有yaml.tmpl扩展名的文件(路径/文件)。结果文件应该是yaml,所以我删除最后一部分以生成结果文件名。

然后,我使用Go模板解析文件,然后使用我生成的configmap执行。

这样做很好,但我想添加:.Option("missingkey=error"),以便在我没有给模板中的变量提供configmap的值时生成错误。

所以我尝试在模板解析文件中添加选项,像这样:

t, err := template.New("test").Option("missingkey=error").ParseFiles(f)

但是我不能使用template.Execute,而必须使用template.ExecuteTemplate,但是使用这些我得到:
template: no template "test" associated with template "test"template: test: "test" is an incomplete or empty template

在我没有得到错误的罕见情况下,它会忽略该选项,就像我这样做:

err = t.Option("missingkey=error").Execute(res, config)

有人知道我做错了什么吗?

编辑

我根据Cerise Limon的答案更新了代码,这是playground链接:playground

目前,该playground会忽略错误,即使传递的config为空,并且模板中没有条件。

英文:

So the question seems to have been asked a couple of times before, but none of the previous answers worked for me, I go from errors to errors to no results.

So as I am most certainly missing something that I don't see I would like for some help:

res, err := os.Create(strings.Replace(f, ".tmpl", "", -1))
if err != nil {
  log.Fatal(err)
}

t, err := template.ParseFiles(f)
if err != nil {
  log.Fatal(err)
}

removes = append(removes, res.Name())

config := make(map[string]string)
for _, v := range vars {
  config[v] = os.Getenv(v)
}

err = t.Execute(res, config)
if err != nil {
  log.Fatal(err)
}

res.Close()

So to explain what I am doing, I'm passing a string to a file (path/file) that has a yaml.tmpl extension. The result file should be yaml so I remove the last part to generate the result file name.

I then parse the file with go template and then I execute with a configmap that I generate.

This is working fine like this but I would like to add: .Option("missingkey=error") to have it generate an error in case I don't give a value from the configmap to a variable in the template.

So I tried to add the options in the template parse file like this:

t, err := template.New("test").Option("missingkey=error").ParseFiles(f)

But I can't use template Exectute and have to use template ExecuteTemplate but with those I get:
template: no template "test" associated with template "test" or template: test: "test" is an incomplete or empty template

In the rare cases I don't get an error, it just ignore the option like if I do this:

err = t.Option("missingkey=error").Execute(res, config)

Does anyone has an idea of what I'm doing wrong?

EDIT

I updated the code with the answer of Cerise Limon and here is the playground: playground

Currently that playground just ignore errors and do the template even if the config passed is empty and there is no or condition in the template.

答案1

得分: 0

ParseFiles方法的文档中提到:

由ParseFiles创建的模板的名称是根据参数文件的基本名称命名的,因此t通常应该具有文件的一个(基本)名称。

使用filepath.Base获取文件的基本名称。将该名称用作模板的名称:

t, err := template.New(filepath.Base(f)).Option("missingkey=error").ParseFiles(f)

在Playground上运行示例:在Playground上运行示例

英文:

The ParseFiles method documentation says:

> Since the templates created by ParseFiles are named by the base names of the argument files, t should usually have the name of one of the (base) names of the files.

Use filepath.Base to get the base name of the file. Use that name as the name of the template:

 t, err := template.New(filepath.Base(f)).Option("missingkey=error").ParseFiles(f)

Run an example on the Playground.

huangapple
  • 本文由 发表于 2021年5月24日 22:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/67674336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定