-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[authentication] user.CreatedAt uses timestamp type to write to db co…
…nsistently
- Loading branch information
Showing
8 changed files
with
91 additions
and
15 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
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,36 @@ | ||
package user | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// we want to consistently format the time when serializing it | ||
// e.g. when saving it to DB or sending it over HTTP | ||
|
||
type timestamp time.Time | ||
|
||
func NewTimestamp(t time.Time) timestamp { | ||
return timestamp(t) | ||
} | ||
|
||
// Equal allows tools like `cmp` to compare two timestamps for equality | ||
func (t timestamp) Equal(other timestamp) bool { | ||
return t.String() == other.String() | ||
} | ||
|
||
// Scan implements Scanner interface supporting types: time.Time | ||
func (t *timestamp) Scan(v interface{}) error { | ||
if v == nil { | ||
return nil | ||
} | ||
if v, ok := v.(time.Time); ok { | ||
*t = NewTimestamp(v) | ||
return nil | ||
} | ||
return fmt.Errorf("Can not scan %v to timestamp", v) | ||
} | ||
|
||
func (t timestamp) String() string { | ||
return time.Time(t).Format(time.RFC3339) | ||
} |
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,40 @@ | ||
package user_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/bhongy/kimidori/authentication/user" | ||
) | ||
|
||
const referenceTime = "2020-04-12T08:49:52Z" | ||
|
||
func newReferenceTime(t *testing.T) time.Time { | ||
tt, err := time.Parse(time.RFC3339, referenceTime) | ||
if err != nil { | ||
t.Fatalf("Cannot parse time: %q", tt) | ||
} | ||
return tt | ||
} | ||
|
||
// TODO: test Equal method | ||
|
||
func Test_Timestamp_Scan(t *testing.T) { | ||
ts := user.NewTimestamp(time.Now()) | ||
tt := newReferenceTime(t) | ||
err := ts.Scan(tt) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if got, want := ts.String(), referenceTime; got != want { | ||
t.Errorf("Scanned value incorrect. want: %v, got: %v", got, want) | ||
} | ||
} | ||
|
||
func Test_Timestamp_String(t *testing.T) { | ||
tt := newReferenceTime(t) | ||
ts := user.NewTimestamp(tt) | ||
if formatted := ts.String(); formatted != referenceTime { | ||
t.Error(formatted) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
package user | ||
|
||
import "time" | ||
|
||
// User models a user account in an authentication context. | ||
type User struct { | ||
ID string | ||
Username string | ||
Password password // a hashed password as stored in the database | ||
CreatedAt time.Time | ||
CreatedAt timestamp | ||
} |