From aee4a11e2d7263e6f2056ff0af8273e842e14ff4 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 12 Mar 2017 13:39:15 +0100 Subject: Parse timesheet files Expect a timesheet file as the last argument to the program. Parse the contents into `TimeEntry` objects. `TimeEntry`ies will then be able to be POSTed to Time Task to submit times. The time entries input file is a YAML document in this format: - client: A client project: A project module: A module task: A task work_type: type date: 2017-03-06 time: 7 billable: true description: It contains an array where each element is a time entry. Had a lot of trouble parsing the date into a `time.Time`. Finally realised that my first and biggest problem was somehow I was importing `yaml.v1` instead of `yaml.v2`, and thus my `UnmarshalYAML` function was never getting called. Wanted a way to get the time as a string and parse it myself into a time. At first tried using an `UnmarshalText` function: type Time time.Time func (t *Time) UnmarshalText(text []byte) error { parsed, err := time.Parse("2006-01-02", string(text)) if err == nil { *t = Time(parsed) } return err } But in order to do that I had to make a type alias to `time.Time`. Doing so was not ideal, because then I'd have to convert my `Time` into a `time.Time` any time I wanted to use it for real. Ended up going with a suggestion from here: https://mlafeldt.github.io/blog/decoding-yaml-in-go/ Creating an auxiliary struct in `UnmarshalYAML` to unmarshal the date into a string and then parse it myself as a date. I don't really like it because it's a lot of ceremony just to parse one type myself, but can't come up with a better solution right now so there you have it. --- timetask/time_entry.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 timetask/time_entry.go (limited to 'timetask') diff --git a/timetask/time_entry.go b/timetask/time_entry.go new file mode 100644 index 0000000..6dfb787 --- /dev/null +++ b/timetask/time_entry.go @@ -0,0 +1,46 @@ +package timetask + +import "time" + +type TimeEntry struct { + Client string + Project string + Task string + WorkType string `yaml:"work_type"` + Date time.Time + Billable bool + Description string +} + +// Parse date string into a real date +func (te *TimeEntry) UnmarshalYAML(unmarshal func(interface{}) error) error { + var auxiliary struct { + Client string + Project string + Task string + WorkType string `yaml:"work_type"` + Date string + Billable bool + Description string + } + + err := unmarshal(&auxiliary) + if err != nil { + return err + } + + date, err := time.Parse("2006-01-02", auxiliary.Date) + if err != nil { + return err + } + + te.Client = auxiliary.Client + te.Project = auxiliary.Project + te.Task = auxiliary.Task + te.WorkType = auxiliary.WorkType + te.Date = date + te.Billable = auxiliary.Billable + te.Description = auxiliary.Description + + return nil +} -- cgit v1.2.3