From 2add4bd9b90e65c896191ced875e10d9b2f90a5c Mon Sep 17 00:00:00 2001 From: Jannis Andrija Schnitzer Date: Tue, 16 Jan 2018 14:38:17 +0100 Subject: [PATCH] Use custom "Date" type for Duedate field Jira doesn't use its default time format ("2006-01-02T15:04:05.999-0700") for the duedate field, but instead a shorter representation that contains only the year, month and day fields, i. e. "2006-01-02". This commit adds a new type "Date" that is unmarshalled using the short format. --- issue.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/issue.go b/issue.go index f3eb9dc..41ba3f0 100644 --- a/issue.go +++ b/issue.go @@ -103,7 +103,7 @@ type IssueFields struct { Priority *Priority `json:"priority,omitempty" structs:"priority,omitempty"` Resolutiondate Time `json:"resolutiondate,omitempty" structs:"resolutiondate,omitempty"` Created Time `json:"created,omitempty" structs:"created,omitempty"` - Duedate Time `json:"duedate,omitempty" structs:"duedate,omitempty"` + Duedate Date `json:"duedate,omitempty" structs:"duedate,omitempty"` Watches *Watches `json:"watches,omitempty" structs:"watches,omitempty"` Assignee *User `json:"assignee,omitempty" structs:"assignee,omitempty"` Updated Time `json:"updated,omitempty" structs:"updated,omitempty"` @@ -294,6 +294,9 @@ type Parent struct { // Time represents the Time definition of JIRA as a time.Time of go type Time time.Time +// Date represents the Date definition of JIRA as a time.Time of go +type Date time.Time + // Wrapper struct for search result type transitionResult struct { Transitions []Transition `json:"transitions" structs:"transitions"` @@ -339,7 +342,7 @@ type Option struct { func (t *Time) UnmarshalJSON(b []byte) error { // Ignore null, like in the main JSON package. if string(b) == "null" { - return nil + return nil } ti, err := time.Parse("\"2006-01-02T15:04:05.999-0700\"", string(b)) if err != nil { @@ -349,6 +352,21 @@ func (t *Time) UnmarshalJSON(b []byte) error { return nil } +// UnmarshalJSON will transform the JIRA date into a time.Time +// during the transformation of the JIRA JSON response +func (t *Date) UnmarshalJSON(b []byte) error { + // Ignore null, like in the main JSON package. + if string(b) == "null" { + return nil + } + ti, err := time.Parse("\"2006-01-02\"", string(b)) + if err != nil { + return err + } + *t = Date(ti) + return nil +} + // Worklog represents the work log of a JIRA issue. // One Worklog contains zero or n WorklogRecords // JIRA Wiki: https://confluence.atlassian.com/jira/logging-work-on-an-issue-185729605.html