Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separated TimeTracker #17

Merged
merged 1 commit into from
Apr 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ If you need to, you can access the raw `mgo` session with `connection.Session`

### Create a Document

Any struct can be used as a document as long as it satisfies the `Document` interface (`SetId(bson.ObjectId)`, `GetId() bson.ObjectId`). We recommend that you use the `DocumentBase` provided with Bongo, which implements that interface as well as the `NewTracker` and `TimeTracker` interfaces (to keep track of new/existing documents and created/modified timestamps). If you use the `DocumentBase` or something similar, make sure you use `bson:",inline"` otherwise you will get nested behavior when the data goes to your database.
Any struct can be used as a document as long as it satisfies the `Document` interface (`SetId(bson.ObjectId)`, `GetId() bson.ObjectId`). We recommend that you use the `DocumentBase` provided with Bongo, which implements that interface as well as the `NewTracker`, `TimeCreatedTracker` and `TimeModifiedTracker` interfaces (to keep track of new/existing documents and created/modified timestamps). If you use the `DocumentBase` or something similar, make sure you use `bson:",inline"` otherwise you will get nested behavior when the data goes to your database.

For example:

Expand Down
14 changes: 9 additions & 5 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ type ValidationError struct {
Errors []error
}

type TimeTracker interface {
type TimeCreatedTracker interface {
SetCreated(time.Time)
}

type TimeModifiedTracker interface {
SetModified(time.Time)
}

Expand Down Expand Up @@ -131,10 +134,11 @@ func (c *Collection) Save(doc Document) error {
// Add created/modified time. Also set on the model itself if it has those fields.
now := time.Now()

if tt, ok := doc.(TimeTracker); ok {
if isNew {
tt.SetCreated(now)
}
if tt, ok := doc.(TimeCreatedTracker); ok && isNew {
tt.SetCreated(now)
}

if tt, ok := doc.(TimeModifiedTracker); ok {
tt.SetModified(now)
}

Expand Down