Skip to content

Commit

Permalink
Add 'TimeStamp' struct for embed
Browse files Browse the repository at this point in the history
  • Loading branch information
naoina committed Feb 24, 2014
1 parent ba1a421 commit bff20f1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ type User struct {

Update/Insert/Delete hooks of an embedded struct will be called before the hooks of the child struct.

Also Genmai has defined `TimeStamp` struct for commonly used fields.

```
type User struct {
Id int64
genmai.TimeStamp
}
```

See the Godoc of [TimeStamp](http://godoc.org/github.com/naoina/genmai#TimeStamp) for more information.

## Documentation

API document and more examples are available here:
Expand Down
28 changes: 28 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package genmai

import "time"

// TimeStamp is fields for timestamps that commonly used.
type TimeStamp struct {
// Time of creation. This field will be set automatically by BeforeInsert.
CreatedAt time.Time

// Time of update. This field will be set by BeforeInsert or BeforeUpdate.
UpdatedAt time.Time
}

// BeforeInsert sets current time to CreatedAt and UpdatedAt field.
// It always returns nil.
func (ts *TimeStamp) BeforeInsert() error {
n := now()
ts.CreatedAt = n
ts.UpdatedAt = n
return nil
}

// BeforeUpdate sets current time to UpdatedAt field.
// It always returns nil.
func (ts *TimeStamp) BeforeUpdate() error {
ts.UpdatedAt = now()
return nil
}
85 changes: 85 additions & 0 deletions field_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package genmai

import (
"reflect"
"testing"
"time"
)

func TestTimeStamp_BeforeInsert(t *testing.T) {
createdAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 22:36:56")
if err != nil {
t.Fatal(err)
}
updatedAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 23:51:26")
if err != nil {
t.Fatal(err)
}
n, err := time.Parse("2006-01-02 15:04:05", "2000-02-02 16:57:38")
if err != nil {
t.Fatal(err)
}
baknow := now
now = func() time.Time {
return n
}
defer func() {
now = baknow
}()
tm := &TimeStamp{
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
if err := tm.BeforeInsert(); err != nil {
t.Fatal(err)
}
actual := tm.CreatedAt
expected := n
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
actual = tm.UpdatedAt
expected = n
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}

func TestTimeStamp_BeforeUpdate(t *testing.T) {
createdAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 22:36:56")
if err != nil {
t.Fatal(err)
}
updatedAt, err := time.Parse("2006-01-02 15:04:05", "2014-02-24 23:51:26")
if err != nil {
t.Fatal(err)
}
n, err := time.Parse("2006-01-02 15:04:05", "2000-02-02 16:57:38")
if err != nil {
t.Fatal(err)
}
baknow := now
now = func() time.Time {
return n
}
defer func() {
now = baknow
}()
tm := &TimeStamp{
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}
if err := tm.BeforeUpdate(); err != nil {
t.Fatal(err)
}
actual := tm.CreatedAt
expected := createdAt
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
actual = tm.UpdatedAt
expected = n
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}

0 comments on commit bff20f1

Please sign in to comment.