fix: handle the zero cases for oidc.Time
This commit is contained in:
parent
890a7f3ed4
commit
115813ee38
2 changed files with 57 additions and 0 deletions
|
@ -173,10 +173,16 @@ func NewEncoder() *schema.Encoder {
|
||||||
type Time int64
|
type Time int64
|
||||||
|
|
||||||
func (ts Time) AsTime() time.Time {
|
func (ts Time) AsTime() time.Time {
|
||||||
|
if ts == 0 {
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
return time.Unix(int64(ts), 0)
|
return time.Unix(int64(ts), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromTime(tt time.Time) Time {
|
func FromTime(tt time.Time) Time {
|
||||||
|
if tt.IsZero() {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return Time(tt.Unix())
|
return Time(tt.Unix())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/schema"
|
"github.com/gorilla/schema"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -467,6 +468,56 @@ func TestNewEncoder(t *testing.T) {
|
||||||
assert.Equal(t, a, b)
|
assert.Equal(t, a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTime_AsTime(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
ts Time
|
||||||
|
want time.Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "unset",
|
||||||
|
ts: 0,
|
||||||
|
want: time.Time{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "set",
|
||||||
|
ts: 1,
|
||||||
|
want: time.Unix(1, 0),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := tt.ts.AsTime()
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTime_FromTime(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
tt time.Time
|
||||||
|
want Time
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "zero",
|
||||||
|
tt: time.Time{},
|
||||||
|
want: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "set",
|
||||||
|
tt: time.Unix(1, 0),
|
||||||
|
want: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := FromTime(tt.tt)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTime_UnmarshalJSON(t *testing.T) {
|
func TestTime_UnmarshalJSON(t *testing.T) {
|
||||||
type dst struct {
|
type dst struct {
|
||||||
UpdatedAt Time `json:"updated_at"`
|
UpdatedAt Time `json:"updated_at"`
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue