-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / lint (1.17, ubuntu-latest)
Check failure on line 27 in pkg/infra/models/jira_time.go GitHub Actions / lint (1.18, ubuntu-latest)
|
||
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 GitHub Actions / lint (1.17, ubuntu-latest)
Check failure on line 48 in pkg/infra/models/jira_time.go GitHub Actions / lint (1.18, ubuntu-latest)
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / lint (1.17, ubuntu-latest)
Check failure on line 34 in pkg/infra/models/jira_time_test.go GitHub Actions / lint (1.18, ubuntu-latest)
|
||
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()") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters