从YAML文档中解析嵌套的映射。

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

Parse nested map from a YAML document

问题

我遇到了一个(我认为是)简单问题的困扰。
我正在尝试使用commando创建一个CLI,这个CLI将读取一个配置yaml文件,然后执行一些操作。然而,我在解析config.yaml文件时遇到了问题。
以下是我的代码:

  1. type Config struct {
  2. Apps map[string]App `yaml:"apps"`
  3. Branch string `yaml:"branch"`
  4. }
  5. type App struct {
  6. hostName string `yaml:"hostName"`
  7. }
  8. .
  9. . //func main和其他标准内容放在这里
  10. .
  11. commando.
  12. Register("read").
  13. AddArgument("config", "read yaml file", "./config.yaml").
  14. SetAction(
  15. func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
  16. //args["config"]等于./config.yaml,我将在下面给出
  17. file, err := ioutil.ReadFile(args["config"].Value)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. var data2 Config
  22. err3 := yaml.Unmarshal(file, &data2)
  23. if err3 != nil {
  24. log.Fatal(err3)
  25. }
  26. fmt.Printf("Ad demo?: %#v\n", data2.Apps["ad-demo"].hostName)
  27. fmt.Printf("Data 2: %#v\n", data2)
  28. })
  29. commando.Parse(nil)

我的config.yaml文件如下:

  1. apps:
  2. ad-tag:
  3. hostName: ad-tag1.krg.io
  4. ad-demo:
  5. hostName: demo1.krg.io
  6. branch: KAT-3821

这个代码在大部分情况下是可以工作的。问题是以下内容打印出来了:

  1. Ad demo?: ""
  2. Data 2: main.Config{Apps:map[string]main.App{"ad-demo":main.App{hostName:""}, "ad-tag":main.App{hostName:""}}, Branch:"KAT-3821"}

hostName的值为空字符串。

你有什么想法,我做错了什么?我觉得这应该很简单,但是我无论如何都找不出错在哪里。

谢谢。

英文:

I am having trouble with (what I believe to be) a simple issue.
I am trying to create a CLI using commando, and this CLI will read a configuration yaml, and then do stuff. However, I am stuck at the parsing of the config.yaml
The following is my code:

  1. type Config struct {
  2. Apps map[string]App `yaml:"apps"`
  3. Branch string `yaml:"branch"`
  4. }
  5. type App struct {
  6. hostName string `yaml:"hostName"`
  7. }
  8. .
  9. . //func main, and other standard stuff goes here
  10. .
  11. commando.
  12. Register("read").
  13. AddArgument("config", "read yaml file", "./config.yaml").
  14. SetAction(
  15. func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
  16. //args["config"] equates to ./config.yaml and I will give that below
  17. file, err := ioutil.ReadFile(args["config"].Value)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. var data2 Config
  22. err3 := yaml.Unmarshal(file, &data2)
  23. if err3 != nil {
  24. log.Fatal(err3)
  25. }
  26. fmt.Printf("Ad demo?: %#v\n", data2.Apps["ad-demo"].hostName)
  27. fmt.Printf("Data 2: %#v\n", data2)
  28. })
  29. commando.Parse(nil)

My config.yaml is as follows:

  1. apps:
  2. ad-tag:
  3. hostName: ad-tag1.krg.io
  4. ad-demo:
  5. hostName: demo1.krg.io
  6. branch: KAT-3821

This works for the most part. The problem is that the following prints:

  1. Ad demo?: ""
  2. Data 2: main.Config{Apps:map[string]main.App{"ad-demo":main.App{hostName:""}, "ad-tag":main.App{hostName:""}}, Branch:"KAT-3821"}

The hostName is coming up as an empty string.

Any idea as to what I am doing wrong? I feel like this should be very simple, but I cannot for the life of me figure out what I am doing wrong.

Thanks

答案1

得分: 1

主机名应该被导出,即首字母大写。

  1. type App struct {
  2. HostName string `yaml:"hostName"`
  3. }

如果不导出它,例如yamljson等包将无法读取该字段并填充它。

英文:

Hostname should be exported, i.e. capitalized

  1. type App struct {
  2. HostName string `yaml:"hostName"`
  3. }

If you don't export it, packages such as yaml or json won't be able to read the field and populate it.

huangapple
  • 本文由 发表于 2021年12月17日 23:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70395623.html
匿名

发表评论

匿名网友

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

确定