读取 YAML 文件并映射到结构体切片。

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

GO reading YAML file and mapping to slice of structs

问题

我正在尝试使用GO读取一个YAML文件,并将其映射到我定义的结构体中。以下是要翻译的内容:

我使用以下代码来读取文件、解析数据,然后打印部分数据。

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v2"
  5. "io/ioutil"
  6. "log"
  7. "time"
  8. )
  9. type Date_File struct {
  10. Owner string `yaml:"owner"`
  11. Init time.Time `yaml:"initialized"`
  12. TimeData []Time_Data `yaml:"time_data"`
  13. }
  14. type Time_Data struct {
  15. Action string `yaml:"action"`
  16. Time time.Time `yaml:"time"`
  17. }
  18. func checkerr(err error) {
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. }
  23. func read() (td *Date_File) {
  24. gtt_config, err := ioutil.ReadFile("go_time_tracker.yml")
  25. checkerr(err)
  26. err = yaml.Unmarshal(gtt_config, &td)
  27. return td
  28. }
  29. func main() {
  30. time_data := read()
  31. fmt.Println(time_data)
  32. fmt.Println(time_data.TimeData[0])
  33. fmt.Println(time_data.Owner)
  34. }

当我运行这段代码时,第一个fmt.Println(time_data)可以正常工作,显示引用及其数据。但是下一行报错,提示索引超出范围。错误信息如下:

  1. $ go run yaml_practice_2.go
  2. &{Phillip Dudley 0001-01-01 00:00:00 +0000 UTC []}
  3. panic: runtime error: index out of range
  4. goroutine 1 [running]:
  5. panic(0x559840, 0xc82000a0e0)
  6. /usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
  7. main.main()
  8. /home/predatorian/Documents/go/src/predatorian/yaml/yaml_practice_2.go:41 +0x2aa
  9. exit status 2

然后我想也许我的YAML格式不正确,所以我将YAML文件加载到Ruby的IRB中,得到了以下结果:

  1. irb(main):004:0> data2 = YAML.load(File.read("go_time_tracker.yml"))
  2. => {"owner"=>"Phillip Dudley", "initialized"=>"2012-10-31 15:50:13.793654 +0000 UTC", "time_data"=>[{"action"=>"start", "time"=>"2012-10-31 15:50:13.793654 +0000 UTC"}, {"action"=>"stop", "time"=>"2012-10-31 16:00:00.000000 +0000 UTC"}]}

IRB的输出显示我的YAML格式正确,但是我认为我没有正确地进行解析。然而,我不确定需要做什么才能使其工作。我确定我没有正确地思考,因为Ruby隐藏了很多细节。

英文:

I'm attempting to read a YAML file using GO and mapping it to a structure that I've defined. The YAML is below:

  1. --- # go_time_tracker.yml
  2. owner: "Phillip Dudley"
  3. initialized: "2012-10-31 15:50:13.793654 +0000 UTC"
  4. time_data:
  5. - action: "start"
  6. time: "2012-10-31 15:50:13.793654 +0000 UTC"
  7. - action: "stop"
  8. time: "2012-10-31 16:00:00.000000 +0000 UTC"

I used the following code to read in the file, Unmarshal the data, and then print some of the data.

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v2"
  5. "io/ioutil"
  6. "log"
  7. "time"
  8. )
  9. type Date_File struct {
  10. Owner string `yaml:"owner"`
  11. Init time.Time `yaml:"initialized"`
  12. TimeData []Time_Data `yaml:"time_data"`
  13. }
  14. type Time_Data struct {
  15. //
  16. Action string `yaml:"action"`
  17. Time time.Time `yaml:"time"`
  18. }
  19. func checkerr(err error) {
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. }
  24. func read() (td *Date_File) {
  25. //td := &Date_File{}
  26. gtt_config, err := ioutil.ReadFile("go_time_tracker.yml")
  27. checkerr(err)
  28. err = yaml.Unmarshal(gtt_config, &td)
  29. return td
  30. }
  31. func main() {
  32. //
  33. time_data := read()
  34. fmt.Println(time_data)
  35. fmt.Println(time_data.TimeData[0])
  36. fmt.Println(time_data.Owner)
  37. }

When I run this, the first fmt.Println(time_data) works, showing the reference and its data. The next line though fails saying that the index is out of range. This is the error:

  1. $ go run yaml_practice_2.go
  2. &{Phillip Dudley 0001-01-01 00:00:00 +0000 UTC []}
  3. panic: runtime error: index out of range
  4. goroutine 1 [running]:
  5. panic(0x559840, 0xc82000a0e0)
  6. /usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
  7. main.main()
  8. /home/predatorian/Documents/go/src/predatorian/yaml/yaml_practice_2.go:41 +0x2aa
  9. exit status 2

I then thought maybe my YAML wasn't formatted properly, so I loaded the YAML file into Ruby's IRB, and this is what I got.

  1. irb(main):004:0> data2 = YAML.load(File.read("go_time_tracker.yml"))
  2. => {"owner"=>"Phillip Dudley", "initialized"=>"2012-10-31 15:50:13.793654 +0000 UTC", "time_data"=>[{"action"=>"start", "time"=>"2012-10-31 15:50:13.793654 +0000 UTC"}, {"action"=>"stop", "time"=>"2012-10-31 16:00:00.000000 +0000 UTC"}]}

The IRB output shows that my YAML is formatted properly, however, I don't think I'm Unmarshalling it properly then. However, I'm not sure what I would need to do to get this to work. I'm sure I'm not thinking of how to do this properly since Ruby hides a lot of it.

答案1

得分: 4

首先,在以下代码中添加checkerr(err)

  1. err = yaml.Unmarshal(gtt_config, &td)

你将得到相应的错误,即time parsing erroryaml解码器期望时间以RFC3339格式表示。有几种方法可以解决这个问题:

  1. 将YAML文件中的时间数据更改为RFC3339格式,例如"2012-10-31T15:50:13.793654Z"
  2. 如果无法修改YAML文件,则需要实现自定义解码器。

对于第二种方法,我想到了两个解决方案:

  1. 实现yaml.Unmarshaler接口
  2. time.Time包装到自定义类型中,然后实现encoding.TextUnmarshaler接口

解决方案(1):你需要为Date_FileTime_Data类型实现自定义的Unmarshaler。将以下代码添加到你的源代码中:

  1. func (df *Date_File) UnmarshalYAML(unmarshal func(interface{}) error) error {
  2. // 将时间解码为字符串,然后手动转换为time.Time
  3. var tmp struct {
  4. Owner string `yaml:"owner"`
  5. Init string `yaml:"initialized"`
  6. TimeData []Time_Data `yaml:"time_data"`
  7. }
  8. if err := unmarshal(&tmp); err != nil {
  9. return err
  10. }
  11. const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
  12. tm, err := time.Parse(layout, tmp.Init)
  13. if err != nil {
  14. return err
  15. }
  16. df.Owner = tmp.Owner
  17. df.Init = tm
  18. df.TimeData = tmp.TimeData
  19. return nil
  20. }
  21. func (td *Time_Data) UnmarshalYAML(unmarshal func(interface{}) error) error {
  22. // 将时间解码为字符串,然后手动转换为time.Time
  23. var tmp struct {
  24. Action string `yaml:"action"`
  25. Time string `yaml:"time"`
  26. }
  27. if err := unmarshal(&tmp); err != nil {
  28. return err
  29. }
  30. const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
  31. tm, err := time.Parse(layout, tmp.Time)
  32. if err != nil {
  33. return err
  34. }
  35. td.Action = tmp.Action
  36. td.Time = tm
  37. return nil
  38. }

如果你有许多具有time.Time字段的types,解决方案(1)可能不太实用。

解决方案(2):如果你查看yaml解码器的源代码,它依赖于TextUnmarshaler将字符串转换为相应的类型。在这里,你需要:

  1. 定义自定义时间类型(例如CustomTime
  2. 将结构体中的每个time.Time字段替换为CustomTime
  3. 实现UnmarshalText

解决方案(3)的代码片段:

  1. type CustomTime struct {
  2. time.Time
  3. }
  4. func (tm *CustomTime) UnmarshalText(text []byte) error {
  5. const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
  6. tmValue, err := time.Parse(layout, string(text))
  7. if err != nil {
  8. return err
  9. }
  10. tm.Time = tmValue
  11. return nil
  12. }
  13. // 与直接相关的,用于打印函数等。
  14. func (tm CustomTime) String() string {
  15. return tm.Time.String()
  16. }
英文:

First, by adding checkerr(err) after

  1. err = yaml.Unmarshal(gtt_config, &td)

you will get the corresponding error which is time parsing error. The yaml decoder expect time in RFC3339 format. There are several ways to fix this:

  1. Change time data in your YAML file to RFC3339 format, e.g. "2012-10-31T15:50:13.793654Z"
  2. If you can not modify the YAML file you need to implement custom decoder.

For (2), two solutions come to my mind:

  1. Implement yaml.Unmarshaler interface, or
  2. Wrap time.Time to a custom type then implement encoding.TextUnmarshaler interface

Solution (1): You need to implement custom Unmarshaler for Date_File and Time_Data types. Add the following to your source code.

  1. func (df *Date_File) UnmarshalYAML(unmarshal func(interface{}) error) error {
  2. //Unmarshal time to string then convert to time.Time manually
  3. var tmp struct {
  4. Owner string `yaml:"owner"`
  5. Init string `yaml:"initialized"`
  6. TimeData []Time_Data `yaml:"time_data"`
  7. }
  8. if err := unmarshal(&tmp); err != nil {
  9. return err;
  10. }
  11. const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
  12. tm, err := time.Parse(layout, tmp.Init)
  13. if err != nil {
  14. return err
  15. }
  16. df.Owner = tmp.Owner
  17. df.Init = tm
  18. df.TimeData = tmp.TimeData
  19. return nil
  20. }
  21. func (td *Time_Data) UnmarshalYAML(unmarshal func(interface{}) error) error {
  22. //Unmarshal time to string then convert to time.Time manually
  23. var tmp struct {
  24. Action string `yaml:"action"`
  25. Time string `yaml:"time"`
  26. }
  27. if err := unmarshal(&tmp); err != nil {
  28. return err;
  29. }
  30. const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
  31. tm, err := time.Parse(layout, tmp.Time)
  32. if err != nil {
  33. return err
  34. }
  35. td.Action = tmp.Action
  36. td.Time = tm
  37. return nil
  38. }

If you have many types having time.Time field, solution (1) maybe impractical.

Solution (2): If you look at source code of yaml decoder, it rely on TextUnmarshaler to convert string to corresponding type. Here you need to:

  1. Define custom time type (e.g. CustomTime)
  2. Replace each time.Time field in your struct with CustomTime
  3. Implement UnmarshalText

The snippet for (3):

  1. type CustomTime struct {
  2. time.Time
  3. }
  4. func (tm *CustomTime) UnmarshalText(text []byte) error {
  5. const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
  6. tmValue, err := time.Parse(layout, string(text))
  7. if err != nil {
  8. return err
  9. }
  10. tm.Time = tmValue
  11. return nil
  12. }
  13. //Not directly related, for print function etc.
  14. func (tm CustomTime) String() string {
  15. return tm.Time.String()
  16. }

huangapple
  • 本文由 发表于 2016年9月25日 04:02:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/39680445.html
匿名

发表评论

匿名网友

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

确定