diff options
| author | Teddy Wing | 2017-03-12 13:39:15 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2017-03-12 13:39:15 +0100 | 
| commit | aee4a11e2d7263e6f2056ff0af8273e842e14ff4 (patch) | |
| tree | 9831a181a40ff51a1fd8ef338229e15f6b1907e2 | |
| parent | e6eee887588efff57d7e8b39894143880357f61c (diff) | |
| download | timetasker-aee4a11e2d7263e6f2056ff0af8273e842e14ff4.tar.bz2 | |
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.
| -rw-r--r-- | main.go | 25 | ||||
| -rw-r--r-- | timetask/time_entry.go | 46 | 
2 files changed, 70 insertions, 1 deletions
| @@ -1,10 +1,14 @@  package main  import ( +	"fmt"  	"io/ioutil"  	"log" +	"os" -	"gopkg.in/yaml.v1" +	"com.teddywing/timetasker/timetask" + +	"gopkg.in/yaml.v2"  )  type Config struct { @@ -41,6 +45,25 @@ var config Config  func main() {  	loadConfig() + +	if len(os.Args) == 1 { +		fmt.Println("Not enough arguments") +		os.Exit(1) +	} + +	file_path := os.Args[len(os.Args)-1] +	file, err := ioutil.ReadFile(file_path) +	if err != nil { +		log.Println(err) +	} + +	time_entries := []timetask.TimeEntry{} +	err = yaml.Unmarshal(file, &time_entries) +	if err != nil { +		log.Println(err) +	} + +	log.Printf("%+v", time_entries)  }  func loadConfig() { 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 +} | 
