登录
首页 >  Golang >  Go问答

分块解析 YAML 文件

来源:stackoverflow

时间:2024-04-17 13:15:37 245浏览 收藏

本篇文章给大家分享《分块解析 YAML 文件》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

我有一个 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

那么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"`
    }

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 
}

我收到此错误,我想按块解析资源,因为我有很多数据,例如 file 块、exec 块,并且我无法修改我的 yaml 文件

我使用包“gopkg.in/yaml.v2”

line 3: cannot unmarshal !!map into []main.YamlResource

如何在 go 中执行此操作,以解析 file、exec 等?

谢谢


正确答案


看起来要解组的结构定义与评论中建议的不匹配。

我重构了代码以更正结构定义。工作代码可以在演示中找到。 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"`
}

好了,本文到此结束,带大家了解了《分块解析 YAML 文件》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>