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

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

Parse nested map from a YAML document

问题

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

type Config struct {
	Apps map[string]App `yaml:"apps"`
	Branch string  `yaml:"branch"`
}
type App struct {
	hostName string `yaml:"hostName"`
}
.
. //func main和其他标准内容放在这里
.
commando.
		Register("read").
		AddArgument("config", "read yaml file", "./config.yaml").
		SetAction(
			func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
				    //args["config"]等于./config.yaml,我将在下面给出
				    file, err := ioutil.ReadFile(args["config"].Value)
					if err != nil {
						log.Fatal(err)
					}

					var data2 Config
					err3 := yaml.Unmarshal(file, &data2)
					if err3 != nil {
						log.Fatal(err3)
					}
					fmt.Printf("Ad demo?: %#v\n", data2.Apps["ad-demo"].hostName)
					fmt.Printf("Data 2: %#v\n", data2)
			})
		commando.Parse(nil)

我的config.yaml文件如下:

apps:
  ad-tag:
    hostName: ad-tag1.krg.io
  ad-demo:
    hostName: demo1.krg.io
branch: KAT-3821

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

Ad demo?: ""
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:

type Config struct {
	Apps map[string]App `yaml:"apps"`
	Branch string  `yaml:"branch"`
}
type App struct {
	hostName string `yaml:"hostName"`
}
.
. //func main, and other standard stuff goes here
.
commando.
		Register("read").
		AddArgument("config", "read yaml file", "./config.yaml").
		SetAction(
			func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
				    //args["config"] equates to ./config.yaml and I will give that below
				    file, err := ioutil.ReadFile(args["config"].Value)
					if err != nil {
						log.Fatal(err)
					}

					var data2 Config
					err3 := yaml.Unmarshal(file, &data2)
					if err3 != nil {
						log.Fatal(err3)
					}
					fmt.Printf("Ad demo?: %#v\n", data2.Apps["ad-demo"].hostName)
					fmt.Printf("Data 2: %#v\n", data2)
			})
		commando.Parse(nil)

My config.yaml is as follows:

apps:
  ad-tag:
    hostName: ad-tag1.krg.io
  ad-demo:
    hostName: demo1.krg.io
branch: KAT-3821

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

Ad demo?: ""
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

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

type App struct {
    HostName string `yaml:"hostName"`
}

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

英文:

Hostname should be exported, i.e. capitalized

type App struct {
    HostName string `yaml:"hostName"`
}

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:

确定