Skip to content

Commit

Permalink
Use AfterLoad instead of AfterSet on Structs (#2628)
Browse files Browse the repository at this point in the history
* use AfterLoad instead of AfterSet on Structs

* fix the comments on AfterLoad

* fix the comments on action AfterLoad
  • Loading branch information
lunny authored and lafriks committed Oct 1, 2017
1 parent 1ad902d commit a8717e5
Show file tree
Hide file tree
Showing 35 changed files with 333 additions and 314 deletions.
10 changes: 3 additions & 7 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/Unknwon/com"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"

"code.gitea.io/git"
api "code.gitea.io/sdk/gitea"
Expand Down Expand Up @@ -91,12 +90,9 @@ type Action struct {
CreatedUnix int64 `xorm:"INDEX created"`
}

// AfterSet updates the webhook object upon setting a column.
func (a *Action) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
a.Created = time.Unix(a.CreatedUnix, 0).Local()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (a *Action) AfterLoad() {
a.Created = time.Unix(a.CreatedUnix, 0).Local()
}

// GetOpType gets the ActionType of this action.
Expand Down
10 changes: 3 additions & 7 deletions models/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"code.gitea.io/gitea/modules/util"

"github.com/Unknwon/com"
"github.com/go-xorm/xorm"
)

//NoticeType describes the notice type
Expand All @@ -32,12 +31,9 @@ type Notice struct {
CreatedUnix int64 `xorm:"INDEX created"`
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
n.Created = time.Unix(n.CreatedUnix, 0).Local()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (n *Notice) AfterLoad() {
n.Created = time.Unix(n.CreatedUnix, 0).Local()
}

// TrStr returns a translation format string.
Expand Down
14 changes: 7 additions & 7 deletions models/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"path"
"time"

"github.com/go-xorm/xorm"
gouuid "github.com/satori/go.uuid"

"code.gitea.io/gitea/modules/setting"
Expand All @@ -31,13 +30,10 @@ type Attachment struct {
CreatedUnix int64 `xorm:"created"`
}

// AfterSet is invoked from XORM after setting the value of a field of
// AfterLoad is invoked from XORM after setting the value of a field of
// this object.
func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
a.Created = time.Unix(a.CreatedUnix, 0).Local()
}
func (a *Attachment) AfterLoad() {
a.Created = time.Unix(a.CreatedUnix, 0).Local()
}

// IncreaseDownloadCount is update download count + 1
Expand Down Expand Up @@ -133,6 +129,10 @@ func GetAttachmentsByIssueID(issueID int64) ([]*Attachment, error) {

// GetAttachmentsByCommentID returns all attachments if comment by given ID.
func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
return getAttachmentsByCommentID(x, commentID)
}

func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
attachments := make([]*Attachment, 0, 10)
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
}
Expand Down
20 changes: 9 additions & 11 deletions models/gpg_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ func (key *GPGKey) BeforeInsert() {
key.CreatedUnix = key.Created.Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (key *GPGKey) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "key_id":
x.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
case "added_unix":
key.Added = time.Unix(key.AddedUnix, 0).Local()
case "expired_unix":
key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
case "created_unix":
key.Created = time.Unix(key.CreatedUnix, 0).Local()
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (key *GPGKey) AfterLoad(session *xorm.Session) {
key.Added = time.Unix(key.AddedUnix, 0).Local()
key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
key.Created = time.Unix(key.CreatedUnix, 0).Local()

err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
if err != nil {
log.Error(3, "Find Sub GPGkeys[%d]: %v", key.KeyID, err)
}
}

Expand Down
15 changes: 5 additions & 10 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,12 @@ func (issue *Issue) BeforeUpdate() {
issue.DeadlineUnix = issue.Deadline.Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of
// AfterLoad is invoked from XORM after setting the value of a field of
// this object.
func (issue *Issue) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "deadline_unix":
issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local()
case "created_unix":
issue.Created = time.Unix(issue.CreatedUnix, 0).Local()
case "updated_unix":
issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local()
}
func (issue *Issue) AfterLoad() {
issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local()
issue.Created = time.Unix(issue.CreatedUnix, 0).Local()
issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local()
}

func (issue *Issue) loadRepo(e Engine) (err error) {
Expand Down
37 changes: 16 additions & 21 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,25 @@ type Comment struct {
ShowTag CommentTag `xorm:"-"`
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (c *Comment) AfterLoad(session *xorm.Session) {
c.Created = time.Unix(c.CreatedUnix, 0).Local()
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()

var err error
switch colName {
case "id":
c.Attachments, err = GetAttachmentsByCommentID(c.ID)
if err != nil {
log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
}
c.Attachments, err = getAttachmentsByCommentID(session, c.ID)
if err != nil {
log.Error(3, "getAttachmentsByCommentID[%d]: %v", c.ID, err)
}

case "poster_id":
c.Poster, err = GetUserByID(c.PosterID)
if err != nil {
if IsErrUserNotExist(err) {
c.PosterID = -1
c.Poster = NewGhostUser()
} else {
log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
}
c.Poster, err = getUserByID(session, c.PosterID)
if err != nil {
if IsErrUserNotExist(err) {
c.PosterID = -1
c.Poster = NewGhostUser()
} else {
log.Error(3, "getUserByID[%d]: %v", c.ID, err)
}
case "created_unix":
c.Created = time.Unix(c.CreatedUnix, 0).Local()
case "updated_unix":
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
}
}

Expand Down
30 changes: 12 additions & 18 deletions models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,21 @@ func (m *Milestone) BeforeUpdate() {
m.ClosedDateUnix = m.ClosedDate.Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of
// AfterLoad is invoked from XORM after setting the value of a field of
// this object.
func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "num_closed_issues":
m.NumOpenIssues = m.NumIssues - m.NumClosedIssues

case "deadline_unix":
m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
if m.Deadline.Year() == 9999 {
return
}

m.DeadlineString = m.Deadline.Format("2006-01-02")
if time.Now().Local().After(m.Deadline) {
m.IsOverDue = true
}
func (m *Milestone) AfterLoad() {
m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
if m.Deadline.Year() == 9999 {
return
}

case "closed_date_unix":
m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
m.DeadlineString = m.Deadline.Format("2006-01-02")
if time.Now().Local().After(m.Deadline) {
m.IsOverDue = true
}

m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
}

// State returns string representation of milestone status.
Expand Down
12 changes: 3 additions & 9 deletions models/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package models
import (
"fmt"
"time"

"github.com/go-xorm/xorm"
)

// Stopwatch represents a stopwatch for time tracking.
Expand All @@ -26,13 +24,9 @@ func (s *Stopwatch) BeforeInsert() {
s.CreatedUnix = time.Now().Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (s *Stopwatch) AfterSet(colName string, _ xorm.Cell) {
switch colName {

case "created_unix":
s.Created = time.Unix(s.CreatedUnix, 0).Local()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (s *Stopwatch) AfterLoad() {
s.Created = time.Unix(s.CreatedUnix, 0).Local()
}

func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
Expand Down
18 changes: 4 additions & 14 deletions models/issue_tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
)

// TrackedTime represents a time that was spent for a specific issue.
Expand All @@ -17,22 +16,13 @@ type TrackedTime struct {
IssueID int64 `xorm:"INDEX" json:"issue_id"`
UserID int64 `xorm:"INDEX" json:"user_id"`
Created time.Time `xorm:"-" json:"created"`
CreatedUnix int64 `json:"-"`
CreatedUnix int64 `xorm:"created" json:"-"`
Time int64 `json:"time"`
}

// BeforeInsert will be invoked by XORM before inserting a record
// representing this object.
func (t *TrackedTime) BeforeInsert() {
t.CreatedUnix = time.Now().Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (t *TrackedTime) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
t.Created = time.Unix(t.CreatedUnix, 0).Local()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (t *TrackedTime) AfterLoad() {
t.Created = time.Unix(t.CreatedUnix, 0).Local()
}

// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
Expand Down
11 changes: 3 additions & 8 deletions models/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package models
import (
"errors"
"time"

"github.com/go-xorm/xorm"
)

// LFSMetaObject stores metadata for LFS tracked files.
Expand Down Expand Up @@ -109,10 +107,7 @@ func RemoveLFSMetaObjectByOid(oid string) error {
return sess.Commit()
}

// AfterSet stores the LFSMetaObject creation time in the database as local time.
func (m *LFSMetaObject) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
m.Created = time.Unix(m.CreatedUnix, 0).Local()
}
// AfterLoad stores the LFSMetaObject creation time in the database as local time.
func (m *LFSMetaObject) AfterLoad() {
m.Created = time.Unix(m.CreatedUnix, 0).Local()
}
12 changes: 4 additions & 8 deletions models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,10 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
}
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (source *LoginSource) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
source.Created = time.Unix(source.CreatedUnix, 0).Local()
case "updated_unix":
source.Updated = time.Unix(source.UpdatedUnix, 0).Local()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (source *LoginSource) AfterLoad() {
source.Created = time.Unix(source.CreatedUnix, 0).Local()
source.Updated = time.Unix(source.UpdatedUnix, 0).Local()
}

// TypeName return name of this login source type.
Expand Down
15 changes: 6 additions & 9 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ func (pr *PullRequest) BeforeUpdate() {
pr.MergedUnix = pr.Merged.Unix()
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
// Note: don't try to get Issue because will end up recursive querying.
func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "merged_unix":
if !pr.HasMerged {
return
}

pr.Merged = time.Unix(pr.MergedUnix, 0).Local()
func (pr *PullRequest) AfterLoad() {
if !pr.HasMerged {
return
}

pr.Merged = time.Unix(pr.MergedUnix, 0).Local()
}

// Note: don't try to get Issue because will end up recursive querying.
Expand Down
10 changes: 3 additions & 7 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/sdk/gitea"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
)

// Release represents a release of repository.
Expand Down Expand Up @@ -50,12 +49,9 @@ func (r *Release) BeforeInsert() {
}
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (r *Release) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
r.Created = time.Unix(r.CreatedUnix, 0).Local()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (r *Release) AfterLoad() {
r.Created = time.Unix(r.CreatedUnix, 0).Local()
}

func (r *Release) loadAttributes(e Engine) error {
Expand Down
Loading

0 comments on commit a8717e5

Please sign in to comment.