英文:
Parsing in block a YAML File
问题
我有一个当前写成yaml文件的文件,内容如下:
host: hostname
resource:
File:
title: "file"
containment_path:
- Test
- Test::Value
tags:
- file
failed: false
skipped: false
corrective_change: false
Exec:
title: Exec
containment_path:
- Test
- Value::Test
tags:
- exec
failed: true
skipped: false
corrective_change: false
resource中的File和Exec等值每次可能不同。我使用以下结构来解析我的YAML数据:
type YamlResource struct {
Host string `yaml:"host"`
Resource []Resource `yaml:"resource"`
}
type Resource struct {
ContainmentPath string `yaml:"containment_path"`
Skipped bool `yaml:"skipped"`
Failed bool `yaml:"failed"`
}
我遇到了以下错误,并且我想按块解析资源,因为我有很多数据,比如一个File块,一个Exec块,而且我不能修改我的YAML文件。
line 3: cannot unmarshal !!map into []main.YamlResource
在Go中,我该如何解析File、Exec和其他资源?谢谢。
英文:
I have a yaml file that is currently written as:
host: hostname
resource:
File:
title: "file"
containment_path:
- Test
- Test::Value
tags:
- file
failed: false
skipped: false
corrective_change: false
Exec:
title: Exec
containment_path:
- Test
- Value::Test
tags:
- exec
failed: true
skipped: false
corrective_change: false
Then the values in resource like File and Exec can be different each time <br>
I use this structure to parse my YAML data
type YamlResource struct {
Host string `yaml:"host"`
Resource []Resource `yaml:"resource"`
}
type Resource struct {
ContainmentPath string `yaml:"containment_path"`
Skipped bool `yaml:"skipped"`
Failed bool `yaml:"failed"`
}
func ScanningYAMLModulesStatus(filename string) (YamlModulesStatus, error) {
f, err := os.Open(filename)
if err != nil {
fmt.Println("Error reading YAML ", filename, ": ", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
isRecord := false
data := ""
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(scanner.Text(), "host:") {
data += line + "\n"
}
if strings.Contains(scanner.Text(), "resource:") {
isRecord = true
}
if strings.Contains(scanner.Text(), "corrective_change:") {
break
}
if isRecord {
data += line + "\n"
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Error scanning YAML ", filename, ": ", err)
}
var ConfigResource YamlResource
if err = yaml.Unmarshal([]byte(data), &ConfigResource); err != nil {
fmt.Println("Error parsing YAML ", filename, ": ", err)
}
return ConfigResource, err
}
I get this error and I would like to parse the resources by block because I have a lot of data such as a File block, an Exec block and I can't modify my YAML file <br>
I use package "gopkg.in/yaml.v2"
line 3: cannot unmarshal !!map into []main.YamlResource
How can I do this in Go, to parse File, Exec and others ? <br>
Thanks
答案1
得分: 2
看起来需要反序列化的结构定义与评论中建议的不匹配。
我已经重构了代码以修正结构定义。可以在这里的playground上找到可工作的代码。链接
type YamlResource struct {
Host string `yaml:"host"`
Resource Resource `yaml:"resource"`
}
type Resource struct {
File File `yaml:"File"`
Exec Exec `yaml:"Exec"`
}
type Exec struct {
ContainmentPath []string `yaml:"containment_path"`
Skipped bool `yaml:"skipped"`
Failed bool `yaml:"failed"`
}
type File struct {
ContainmentPath []string `yaml:"containment_path"`
Skipped bool `yaml:"skipped"`
Failed bool `yaml:"failed"`
}
英文:
Looks like the to be unmarshalled struct definition is mismatching as suggested in comments.
I have refactored the code to correct struct definitions. The working code can be found here on playground. Link
type YamlResource struct {
Host string `yaml:"host"`
Resource Resource `yaml:"resource"`
}
type Resource struct {
File File `yaml:"File"`
Exec Exec `yaml:"Exec"`
}
type Exec struct {
ContainmentPath []string `yaml:"containment_path"`
Skipped bool `yaml:"skipped"`
Failed bool `yaml:"failed"`
}
type File struct {
ContainmentPath []string `yaml:"containment_path"`
Skipped bool `yaml:"skipped"`
Failed bool `yaml:"failed"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论