dousha4804 2016-09-24 20:02
浏览 126
已采纳

开始读取YAML文件并映射到结构片

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条回答 默认 最新

  • douhuanqiao5290 2016-09-25 03:02
    关注

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?