Skip to content

Commit

Permalink
Added DateScheme and DateTimeScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Fank committed Jul 1, 2024
1 parent dde3c03 commit 2496b27
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 27 deletions.
11 changes: 5 additions & 6 deletions pkg/infra/models/jira_issue_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import (
"encoding/json"
"time"

"dario.cat/mergo"
)
Expand Down Expand Up @@ -112,21 +111,21 @@ type IssueFieldsSchemeV2 struct {
Reporter *UserScheme `json:"reporter,omitempty"`
Assignee *UserScheme `json:"assignee,omitempty"`
Resolution *ResolutionScheme `json:"resolution,omitempty"`
Resolutiondate string `json:"resolutiondate,omitempty"`
Resolutiondate *DateTimeScheme `json:"resolutiondate,omitempty"`
Workratio int `json:"workratio,omitempty"`
StatusCategoryChangeDate string `json:"statuscategorychangedate,omitempty"`
StatusCategoryChangeDate *DateTimeScheme `json:"statuscategorychangedate,omitempty"`
LastViewed string `json:"lastViewed,omitempty"`
Summary string `json:"summary,omitempty"`
Created *time.Time `json:"created,omitempty"`
Updated *time.Time `json:"updated,omitempty"`
Created *DateTimeScheme `json:"created,omitempty"`
Updated *DateTimeScheme `json:"updated,omitempty"`
Labels []string `json:"labels,omitempty"`
Status *StatusScheme `json:"status,omitempty"`
Description string `json:"description,omitempty"`
Comment *IssueCommentPageSchemeV2 `json:"comment,omitempty"`
Subtasks []*IssueScheme `json:"subtasks,omitempty"`
Security *SecurityScheme `json:"security,omitempty"`
Worklog *IssueWorklogRichTextPageScheme `json:"worklog,omitempty"`
DueDate string `json:"duedate,omitempty"`
DueDate *DateScheme `json:"duedate,omitempty"`
}

// ParentScheme represents the parent of an issue in Jira.
Expand Down
11 changes: 5 additions & 6 deletions pkg/infra/models/jira_issue_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package models

import (
"encoding/json"
"time"

"dario.cat/mergo"
)
Expand Down Expand Up @@ -112,13 +111,13 @@ type IssueFieldsScheme struct {
Reporter *UserScheme `json:"reporter,omitempty"` // The user who reported the issue.
Assignee *UserScheme `json:"assignee,omitempty"` // The user assigned to the issue.
Resolution *ResolutionScheme `json:"resolution,omitempty"` // The resolution of the issue.
Resolutiondate string `json:"resolutiondate,omitempty"` // The date the issue was resolved.
Resolutiondate *DateTimeScheme `json:"resolutiondate,omitempty"` // The date the issue was resolved.
Workratio int `json:"workratio,omitempty"` // The work ratio of the issue.
StatusCategoryChangeDate string `json:"statuscategorychangedate,omitempty"` // The date the status category changed.
StatusCategoryChangeDate *DateTimeScheme `json:"statuscategorychangedate,omitempty"` // The date the status category changed.
LastViewed string `json:"lastViewed,omitempty"` // The last time the issue was viewed.
Summary string `json:"summary,omitempty"` // The summary of the issue.
Created *time.Time `json:"created,omitempty"` // The date the issue was created.
Updated *time.Time `json:"updated,omitempty"` // The date the issue was last updated.
Created *DateTimeScheme `json:"created,omitempty"` // The date the issue was created.
Updated *DateTimeScheme `json:"updated,omitempty"` // The date the issue was last updated.
Labels []string `json:"labels,omitempty"` // The labels associated with the issue.
Status *StatusScheme `json:"status,omitempty"` // The status of the issue.
Description *CommentNodeScheme `json:"description,omitempty"` // The description of the issue.
Expand All @@ -127,7 +126,7 @@ type IssueFieldsScheme struct {
Security *SecurityScheme `json:"security,omitempty"` // The security level of the issue.
Attachment []*AttachmentScheme `json:"attachment,omitempty"` // The attachments of the issue.
Worklog *IssueWorklogADFPageScheme `json:"worklog,omitempty"` // The worklog of the issue.
DueDate string `json:"duedate,omitempty"` // The due date of the issue.
DueDate *DateScheme `json:"duedate,omitempty"` // The due date of the issue.
}

// IssueTransitionScheme represents a transition of an issue in Jira.
Expand Down
6 changes: 3 additions & 3 deletions pkg/infra/models/jira_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ type ProjectScheme struct {
Insight *ProjectInsightScheme `json:"insight,omitempty"` // The insight of the project.
Category *ProjectCategoryScheme `json:"projectCategory,omitempty"` // The category of the project.
Deleted bool `json:"deleted,omitempty"` // Indicates if the project is deleted.
RetentionTillDate string `json:"retentionTillDate,omitempty"` // The retention till date of the project.
DeletedDate string `json:"deletedDate,omitempty"` // The date the project was deleted.
RetentionTillDate *DateTimeScheme `json:"retentionTillDate,omitempty"` // The retention till date of the project.
DeletedDate *DateTimeScheme `json:"deletedDate,omitempty"` // The date the project was deleted.
DeletedBy *UserScheme `json:"deletedBy,omitempty"` // The user who deleted the project.
Archived bool `json:"archived,omitempty"` // Indicates if the project is archived.
ArchivedDate string `json:"archivedDate,omitempty"` // The date the project was archived.
ArchivedDate *DateTimeScheme `json:"archivedDate,omitempty"` // The date the project was archived.
ArchivedBy *UserScheme `json:"archivedBy,omitempty"` // The user who archived the project.
}

Expand Down
50 changes: 50 additions & 0 deletions pkg/infra/models/jira_time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package models

import "time"

const (
TimeFormat = "2006-01-02T15:04:05-0700"
DateFormat = "2006-01-02"
)

// DateScheme is a custom time type for Jira dates.
type DateScheme time.Time

func (d DateScheme) MarshalJSON() ([]byte, error) {
return []byte(time.Time(d).Format(DateFormat)), nil
}

func (d DateScheme) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}

parsed, err := time.Parse(`"`+DateFormat+`"`, string(data))
if err != nil {
return err
}

d = DateScheme(parsed)

Check failure on line 27 in pkg/infra/models/jira_time.go

View workflow job for this annotation

GitHub Actions / lint (1.17, ubuntu-latest)

ineffectual assignment to d (ineffassign)

Check failure on line 27 in pkg/infra/models/jira_time.go

View workflow job for this annotation

GitHub Actions / lint (1.18, ubuntu-latest)

ineffectual assignment to d (ineffassign)

Check failure on line 27 in pkg/infra/models/jira_time.go

View workflow job for this annotation

GitHub Actions / lint (1.18, macos-latest)

ineffectual assignment to d (ineffassign)
return nil
}

// DateTimeScheme is a custom time type for Jira times.
type DateTimeScheme time.Time

func (d DateTimeScheme) MarshalJSON() ([]byte, error) {
return []byte(time.Time(d).Format(TimeFormat)), nil
}

func (d DateTimeScheme) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}

parsed, err := time.Parse(`"`+TimeFormat+`"`, string(data))
if err != nil {
return err
}

d = DateTimeScheme(parsed)

Check failure on line 48 in pkg/infra/models/jira_time.go

View workflow job for this annotation

GitHub Actions / lint (1.17, ubuntu-latest)

ineffectual assignment to d (ineffassign)

Check failure on line 48 in pkg/infra/models/jira_time.go

View workflow job for this annotation

GitHub Actions / lint (1.18, ubuntu-latest)

ineffectual assignment to d (ineffassign)

Check failure on line 48 in pkg/infra/models/jira_time.go

View workflow job for this annotation

GitHub Actions / lint (1.18, macos-latest)

ineffectual assignment to d (ineffassign)
return nil
}
84 changes: 84 additions & 0 deletions pkg/infra/models/jira_time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package models

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestDateTimeScheme_MarshalJSON(t *testing.T) {
tests := []struct {
name string
t DateTimeScheme
want []byte
wantErr assert.ErrorAssertionFunc
}{
{
name: "random time",
t: DateTimeScheme(time.Date(2021, 6, 22, 7, 34, 41, 123, time.UTC)),
want: []byte(`2021-06-22T07:34:41+0000`),
wantErr: assert.NoError,
},
{
name: "timezone",
t: DateTimeScheme(time.Date(2021, 6, 22, 7, 34, 41, 123, time.FixedZone("UTC+2", 7200))),
want: []byte(`2021-06-22T07:34:41+0200`),
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.t.MarshalJSON()
if !tt.wantErr(t, err, fmt.Sprintf("MarshalJSON()")) {

Check failure on line 34 in pkg/infra/models/jira_time_test.go

View workflow job for this annotation

GitHub Actions / lint (1.17, ubuntu-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 34 in pkg/infra/models/jira_time_test.go

View workflow job for this annotation

GitHub Actions / lint (1.18, ubuntu-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)

Check failure on line 34 in pkg/infra/models/jira_time_test.go

View workflow job for this annotation

GitHub Actions / lint (1.18, macos-latest)

S1039: unnecessary use of fmt.Sprintf (gosimple)
return
}
assert.Equalf(t, string(tt.want), string(got), "MarshalJSON()")
})
}
}

func TestDateTimeScheme_UnmarshalJSON(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
want DateTimeScheme
wantErr assert.ErrorAssertionFunc
}{
{
name: "null",
args: args{
data: []byte(`null`),
},
want: DateTimeScheme{},
wantErr: assert.NoError,
},
{
name: "random date",
args: args{
data: []byte(`"2021-06-22T07:34:41+0000"`),
},
want: DateTimeScheme{},
wantErr: assert.NoError,
},
{
name: "invalid date",
args: args{
data: []byte(`"asdf"`),
},
want: DateTimeScheme{},
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := DateTimeScheme{}
tt.wantErr(t, d.UnmarshalJSON(tt.args.data), fmt.Sprintf("UnmarshalJSON(%v)", tt.args.data))
assert.Equalf(t, tt.want, d, "MarshalJSON()")
})
}
}
8 changes: 4 additions & 4 deletions pkg/infra/models/sm_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type InfoScheme struct {

// InfoBuildDataScheme represents the build date of a system.
type InfoBuildDataScheme struct {
Iso8601 string `json:"iso8601,omitempty"` // The ISO 8601 format of the build date.
Jira string `json:"jira,omitempty"` // The Jira format of the build date.
Friendly string `json:"friendly,omitempty"` // The friendly format of the build date.
EpochMillis int64 `json:"epochMillis,omitempty"` // The epoch milliseconds of the build date.
Iso8601 DateTimeScheme `json:"iso8601,omitempty"` // The ISO 8601 format of the build date.
Jira string `json:"jira,omitempty"` // The Jira format of the build date.
Friendly string `json:"friendly,omitempty"` // The friendly format of the build date.
EpochMillis int64 `json:"epochMillis,omitempty"` // The epoch milliseconds of the build date.
}

// InfoLinkScheme represents a link related to a system.
Expand Down
16 changes: 8 additions & 8 deletions pkg/infra/models/sm_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ type CustomerRequestServiceDeskScheme struct {

// CustomerRequestDateScheme represents a date for a customer request.
type CustomerRequestDateScheme struct {
Iso8601 string `json:"iso8601,omitempty"` // The ISO 8601 format of the date.
Jira string `json:"jira,omitempty"` // The Jira format of the date.
Friendly string `json:"friendly,omitempty"` // The friendly format of the date.
EpochMillis int `json:"epochMillis,omitempty"` // The epoch milliseconds of the date.
Iso8601 DateTimeScheme `json:"iso8601,omitempty"` // The ISO 8601 format of the date.
Jira string `json:"jira,omitempty"` // The Jira format of the date.
Friendly string `json:"friendly,omitempty"` // The friendly format of the date.
EpochMillis int `json:"epochMillis,omitempty"` // The epoch milliseconds of the date.
}

// CustomerRequestReporterScheme represents a reporter for a customer request.
Expand Down Expand Up @@ -108,10 +108,10 @@ type CustomerRequestCurrentStatusScheme struct {

// CustomerRequestCurrentStatusDateScheme represents a date for a customer request current status.
type CustomerRequestCurrentStatusDateScheme struct {
Iso8601 string `json:"iso8601,omitempty"` // The ISO 8601 format of the date.
Jira string `json:"jira,omitempty"` // The Jira format of the date.
Friendly string `json:"friendly,omitempty"` // The friendly format of the date.
EpochMillis int `json:"epochMillis,omitempty"` // The epoch milliseconds of the date.
Iso8601 DateTimeScheme `json:"iso8601,omitempty"` // The ISO 8601 format of the date.
Jira string `json:"jira,omitempty"` // The Jira format of the date.
Friendly string `json:"friendly,omitempty"` // The friendly format of the date.
EpochMillis int `json:"epochMillis,omitempty"` // The epoch milliseconds of the date.
}

// CustomerRequestLinksScheme represents the links related to a customer request.
Expand Down

0 comments on commit 2496b27

Please sign in to comment.