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.
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:
"2012-10-31T15:50:13.793654Z"
For (2), two solutions come to my mind:
yaml.Unmarshaler
interface, ortime.Time
to a custom type then implement encoding.TextUnmarshaler
interfaceSolution (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:
CustomTime
)time.Time
field in your struct with CustomTime
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()
}