解析 YAML 文件中的块 a。

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

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:&quot;host&quot;`
    	Resource		     []Resource    `yaml:&quot;resource&quot;`
    }
    
    type Resource struct {
    	ContainmentPath 	 string 	   `yaml:&quot;containment_path&quot;`
    	Skipped				 bool		   `yaml:&quot;skipped&quot;`
    	Failed				 bool		   `yaml:&quot;failed&quot;`
    }

func ScanningYAMLModulesStatus(filename string) (YamlModulesStatus, error) {
	
	f, err := os.Open(filename)
	if err != nil {
		fmt.Println(&quot;Error reading YAML &quot;, filename, &quot;: &quot;, err)
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	isRecord := false
	data := &quot;&quot;
	for scanner.Scan() {
		line := scanner.Text()
		if strings.Contains(scanner.Text(), &quot;host:&quot;) {
			data += line + &quot;\n&quot;
		}
		if strings.Contains(scanner.Text(), &quot;resource:&quot;) {
			isRecord = true
		}
		if strings.Contains(scanner.Text(), &quot;corrective_change:&quot;) {
			break
		}
		if isRecord {
			data += line + &quot;\n&quot;
		}
	}

	if err := scanner.Err(); err != nil {
		fmt.Println(&quot;Error scanning YAML &quot;, filename, &quot;: &quot;, err)
	}

	var ConfigResource YamlResource
	if err = yaml.Unmarshal([]byte(data), &amp;ConfigResource); err != nil {
		fmt.Println(&quot;Error parsing YAML &quot;, filename, &quot;: &quot;, 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:&quot;host&quot;`
	Resource Resource `yaml:&quot;resource&quot;`
}

type Resource struct {
	File File `yaml:&quot;File&quot;`
	Exec Exec `yaml:&quot;Exec&quot;`
}

type Exec struct {
	ContainmentPath []string `yaml:&quot;containment_path&quot;`
	Skipped         bool     `yaml:&quot;skipped&quot;`
	Failed          bool     `yaml:&quot;failed&quot;`
}

type File struct {
	ContainmentPath []string `yaml:&quot;containment_path&quot;`
	Skipped         bool     `yaml:&quot;skipped&quot;`
	Failed          bool     `yaml:&quot;failed&quot;`
}

huangapple
  • 本文由 发表于 2021年7月21日 16:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/68466387.html
匿名

发表评论

匿名网友

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

确定