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

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

GO reading YAML file and mapping to slice of structs

问题

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

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

package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
	"time"
)

type Date_File struct {
	Owner    string      `yaml:"owner"`
	Init     time.Time   `yaml:"initialized"`
	TimeData []Time_Data `yaml:"time_data"`
}

type Time_Data struct {
	Action string    `yaml:"action"`
	Time   time.Time `yaml:"time"`
}

func checkerr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func read() (td *Date_File) {
	gtt_config, err := ioutil.ReadFile("go_time_tracker.yml")
	checkerr(err)
	err = yaml.Unmarshal(gtt_config, &td)
	return td
}

func main() {
	time_data := read()
	fmt.Println(time_data)
	fmt.Println(time_data.TimeData[0])
	fmt.Println(time_data.Owner)
}

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

$ go run yaml_practice_2.go 
&{Phillip Dudley 0001-01-01 00:00:00 +0000 UTC []}
panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x559840, 0xc82000a0e0)
	/usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
main.main()
	/home/predatorian/Documents/go/src/predatorian/yaml/yaml_practice_2.go:41 +0x2aa
exit status 2

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

irb(main):004:0> data2 = YAML.load(File.read("go_time_tracker.yml"))
=> {"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:

--- # go_time_tracker.yml
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"

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

package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
	"time"
)

type Date_File struct {
	Owner    string      `yaml:"owner"`
	Init     time.Time   `yaml:"initialized"`
	TimeData []Time_Data `yaml:"time_data"`
}

type Time_Data struct {
	//
	Action string    `yaml:"action"`
	Time   time.Time `yaml:"time"`
}

func checkerr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func read() (td *Date_File) {
	//td := &Date_File{}
	gtt_config, err := ioutil.ReadFile("go_time_tracker.yml")
	checkerr(err)
	err = yaml.Unmarshal(gtt_config, &td)
	return td
}

func main() {
	//
	time_data := read()
	fmt.Println(time_data)
	fmt.Println(time_data.TimeData[0])
	fmt.Println(time_data.Owner)
}

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:

$ go run yaml_practice_2.go 
&{Phillip Dudley 0001-01-01 00:00:00 +0000 UTC []}
panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x559840, 0xc82000a0e0)
	/usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
main.main()
	/home/predatorian/Documents/go/src/predatorian/yaml/yaml_practice_2.go:41 +0x2aa
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.

irb(main):004:0> data2 = YAML.load(File.read("go_time_tracker.yml"))
=> {"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)

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。将以下代码添加到你的源代码中:

func (df *Date_File) UnmarshalYAML(unmarshal func(interface{}) error) error {
    // 将时间解码为字符串,然后手动转换为time.Time
    var tmp struct {
        Owner    string      `yaml:"owner"`
        Init     string      `yaml:"initialized"`
        TimeData []Time_Data `yaml:"time_data"`
    }
    if err := unmarshal(&tmp); err != nil {
        return err
    }

    const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
    tm, err := time.Parse(layout, tmp.Init)
    if err != nil {
        return err
    }

    df.Owner = tmp.Owner
    df.Init = tm
    df.TimeData = tmp.TimeData

    return nil
}

func (td *Time_Data) UnmarshalYAML(unmarshal func(interface{}) error) error {
    // 将时间解码为字符串,然后手动转换为time.Time
    var tmp struct {
        Action string `yaml:"action"`
        Time   string `yaml:"time"`
    }
    if err := unmarshal(&tmp); err != nil {
        return err
    }

    const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
    tm, err := time.Parse(layout, tmp.Time)
    if err != nil {
        return err
    }

    td.Action = tmp.Action
    td.Time = tm

    return nil
}

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

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

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

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

type CustomTime struct {
    time.Time
}
func (tm *CustomTime) UnmarshalText(text []byte) error {
    const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
    tmValue, err := time.Parse(layout, string(text))
    if err != nil {
        return err
    }

    tm.Time = tmValue
    return nil
}
// 与直接相关的,用于打印函数等。
func (tm CustomTime) String() string {
    return tm.Time.String()
}
英文:

First, by adding checkerr(err) after

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.

func (df *Date_File) UnmarshalYAML(unmarshal func(interface{}) error) error {
    //Unmarshal time to string then convert to time.Time manually
    var tmp struct {
        Owner    string      `yaml:"owner"`
        Init     string      `yaml:"initialized"`
        TimeData []Time_Data `yaml:"time_data"`
    }
    if err := unmarshal(&tmp); err != nil {
        return err;
    }
    
    const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
    tm, err := time.Parse(layout, tmp.Init)
    if err != nil {
        return err
    }
    
    df.Owner    = tmp.Owner
    df.Init     = tm
    df.TimeData = tmp.TimeData
    
    return nil
}

func (td *Time_Data) UnmarshalYAML(unmarshal func(interface{}) error) error {
    //Unmarshal time to string then convert to time.Time manually
    var tmp struct {
        Action string `yaml:"action"`
        Time   string `yaml:"time"`
    }
    if err := unmarshal(&tmp); err != nil {
        return err;
    }
    
    const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
    tm, err := time.Parse(layout, tmp.Time)
    if err != nil {
        return err
    }
    
    td.Action = tmp.Action
    td.Time   = tm
    
    return nil
}

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):

type CustomTime struct {
    time.Time
}
func (tm *CustomTime) UnmarshalText(text []byte) error {
    const layout = "2006-01-02 15:04:05.999999999 -0700 MST"
    tmValue, err := time.Parse(layout, string(text))
    if err != nil {
        return err
    }
    
    tm.Time = tmValue
    return nil    
}
//Not directly related, for print function etc.
func (tm CustomTime) String() string {
    return tm.Time.String()
}

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:

确定