Parsing Only Certain Keys in a YAML File in Golang

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

Parsing Only Certain Keys in a YAML File in Golang

问题

我有一个YAML文件,我正在使用Golang读取并解组到一个结构体中。该文件包含多个键,但我只需要其中的某些键。我发现,如果我将不需要的键放在YAML文件的底部,一切都按预期工作,但如果键的顺序是随机的,解组/解析将返回意外的值。例如:

简单的示例YAML文件:

---
  cat_name: rusty
  dog_name: billy
  cat_food: Purina
  dog_food: Blue
  cat_rescued: true
  dog_rescued: false
  cat_age: 4
  dog_age: 6
  happy: true

结构体定义如下:

type MyStruct struct {
    Happy   bool    `yaml:"happy"`
    CatName string  `yaml:"cat_name"`
    CatAge  int     `yaml:"cat_age"`
}

解组代码如下:

data, err := ioutil.ReadFile(filename)
if err != nil {
    return nil, fmt.Errorf("无法读取文件 %s。发生错误:%w", filename, err)
}

params := MyStruct{}
if err = yaml.Unmarshal(data, &params); err != nil {
    return nil, fmt.Errorf("无法解析来自文件的YAML:%w", err)
}

return &params, nil

我发现,我会得到正确的CatName,但其他两个字段(HappyCatAge)将分别为false0

然而,如果我更改YAML文件中键的顺序,使得我需要的三个键是YAML文件中的前三个键,那么我将得到预期的数据true4

我对Golang还不太熟悉,想知道在进行了大量搜索和实验之后,我漏掉了什么?我正在使用https://github.com/goccy/go-yaml库进行解组。在将YAML解组到结构体时,预期YAML文件中的键的顺序与结构体字段的顺序相同吗?提前感谢您的帮助。

英文:

I have a YAML file that I am reading in in Golang and unmarshaling to a struct. The file contains several keys, but I only need certain ones. What I am finding is that if I place the keys that aren't needed for this application at the bottom of the YAML file, everything works as expected, but if the keys are in random order, the unmarshaling / parsing returns unexpected values. For example:

Sample Simple YAML file:

---
  cat_name: rusty
  dog_name: billy
  cat_food: Purina
  dog_food: Blue
  cat_rescued: true
  dog_rescued: false
  cat_age: 4
  dog_age: 6
  happy: true
type MyStruct struct {
    Happy  bool  `yaml:"happy"`
    CatName  string  `yaml:"cat_name"`
    CatAge  int  `yaml:"cat_age"`
}

Code that does the unmarshaling:

data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, fmt.Errorf("Could not read file %s. An error has occurred: %w", filename, err)
	}

	params := MyStruct{}
	if err = yaml.Unmarshal(data, &params); err != nil {
		return nil, fmt.Errorf("could not parse yaml from file. %w", err)
	}

	return &params, nil

What I am finding is that, I will get back the correct CatName, but that the other two fields (Happy and CatAge) will be false and 0, respectively.

However, if I change the ordering of the keys in the yaml file so that the three keys that I need for MyStruct are the top three in the YAML file, I will get back the expected data of true and 4.

I am pretty new to Golang and am wondering, after doing a lot of searching and experimentation, what I am missing? I am using the https://github.com/goccy/go-yaml library to do the unmarshaling. Is it expected when unmarshaling YAML to a struct that the order of the keys in the YAML file are in the same order as the struct fields? Thanks in advance.

答案1

得分: 1

我发现的是,我会得到正确的CatName,但是其他两个字段(Happy和CatAge)分别为false和0。

不,上述假设是不正确的。根据你的定义和代码,所有必需的字段应该按预期更新,YAML或结构字段的顺序对最终的非编组输出没有影响。

go-yaml库的Go playground上有一个示例。

英文:

> What I am finding is that, I will get back the correct CatName, but that the other two fields (Happy and CatAge) will be false and 0, respectively.

No, the above assumption is incorrect. Per the definitions and code you have, all the required fields should be updated as expected and the ordering of keys in the YAML or struct fields has no relevance on the final un-marshalled output.

See an example with go-yaml library at Go playground

huangapple
  • 本文由 发表于 2022年9月21日 22:17:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/73802329.html
匿名

发表评论

匿名网友

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

确定