Golang解析YAML文件结构的挑战

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

golang parse yaml file struct challenged

问题

遇到了解析这种类型的YAML文件的问题。使用"yaml.v2"库。

info: "abc"

data:
  source: http://intra
  destination: /tmp

run:
  - id: "A1"
    exe: "run.a1"
    output: "output.A1"

  - id: "A2"
    exe: "run.a2"
    output: "output.A2"

我想要获取YAML文件的所有值,以便有一个基本的结构体如下:

type Config struct {
  Info string
  Data struct {
    Source      string `yaml:"source"`
    Destination string `yaml:"destination"`
  }
}

这部分是可以工作的。

但是,我不确定如何为"run"设置结构体。额外的层级让我感到困惑。

type Run struct {
  ...
}
英文:

Having a problem parsing this sort of yaml file. Using "yaml.v2"

info:  "abc"

data:
  source:  http://intra
  destination:  /tmp

run:
  - id:  "A1"
    exe:  "run.a1"
    output:  "output.A1"

  - id:  "A2"
    exe:  "run.a2"
    output:  "output.A2"

I would like to get all the values of the YAML file so I have a basic struct like this

type Config struct {
  Info  string
  Data struct {
    Source  string `yaml:"source"`
    Destination  string `yaml:"destination"`
    }
 }

This works

But, I am not sure how to setup the struct for "run". The extra layer is confusing me.

type Run struct {
 ...
}

答案1

得分: 1

OP的YAML示例是无效的。当run的值是字典列表时,应该像这样:

info: "abc"

data:
  source: http://intra
  destination: /tmp

run:
  - id: "A1"
    exe: "run.a1"
    output: "output.A1"

  - id: "A2"
    exe: "run.a2"
    output: "output.A2"

以下是相应的数据结构和将YAML解码为golang结构的示例:

package main

import (
	"fmt"
	"io/ioutil"
	"os"

	yaml "gopkg.in/yaml.v2"
)

type Config struct {
	Info string
	Data struct {
		Source      string
		Destination string
	}
	Run []struct {
		Id     string
		Exe    string
		Output string
	}
}

func main() {
	var conf Config
	reader, _ := os.Open("example.yaml")
	buf, _ := ioutil.ReadAll(reader)
	yaml.Unmarshal(buf, &conf)
	fmt.Printf("%+v\n", conf)
}

运行此代码将输出(为了可读性添加了一些缩进):

{
  Info: "abc",
  Data: {
    Source: "http://intra",
    Destination: "/tmp"
  },
  Run: [
    {
      Id: "A1",
      Exe: "run.a1",
      Output: "output.A1"
    },
    {
      Id: "A2",
      Exe: "run.a2",
      Output: "output.A2"
    }
  ]
}
英文:

the OP's example of YAML is invalid. When value of run is list of dictionary it should be something like this:

<!-- language: lang-none -->

info:  &quot;abc&quot;

data:
  source:  http://intra
  destination:  /tmp

run:
  - id:  &quot;A1&quot;
    exe:  &quot;run.a1&quot;
    output:  &quot;output.A1&quot;

  - id:  &quot;A2&quot;
    exe:  &quot;run.a2&quot;
    output:  &quot;output.A2&quot;

And here's the corresponding data struture, and example for decoding YAML into golang's structure.

<!-- language: language-none -->

package main

import (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;os&quot;

	yaml &quot;gopkg.in/yaml.v2&quot;
)

type Config struct {
	Info string
	Data struct {
		Source      string
		Destination string
	}
	Run []struct {
		Id     string
		Exe    string
		Output string
	}
}

func main() {
	var conf Config
	reader, _ := os.Open(&quot;example.yaml&quot;)
	buf, _ := ioutil.ReadAll(reader)
	yaml.Unmarshal(buf, &amp;conf)
	fmt.Printf(&quot;%+v\n&quot;, conf)
}

running this will output (added some indent for readability):

<!-- language: lang-none -->

{Info:abc
 Data:{Source:http://intra Destination:/tmp}
 Run:[{Id:A1 Exe:run.a1 Output:output.A1}
      {Id:A2 Exe:run.a2 Output:output.A2}]

huangapple
  • 本文由 发表于 2017年1月27日 08:38:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/41885442.html
匿名

发表评论

匿名网友

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

确定