Skip to content

Commit a8717e5

Browse files
lunnylafriks
authored andcommitted
Use AfterLoad instead of AfterSet on Structs (#2628)
* use AfterLoad instead of AfterSet on Structs * fix the comments on AfterLoad * fix the comments on action AfterLoad
1 parent 1ad902d commit a8717e5

35 files changed

+333
-314
lines changed

models/action.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/Unknwon/com"
1818
"github.com/go-xorm/builder"
19-
"github.com/go-xorm/xorm"
2019

2120
"code.gitea.io/git"
2221
api "code.gitea.io/sdk/gitea"
@@ -91,12 +90,9 @@ type Action struct {
9190
CreatedUnix int64 `xorm:"INDEX created"`
9291
}
9392

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

10298
// GetOpType gets the ActionType of this action.

models/admin.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/util"
1313

1414
"github.com/Unknwon/com"
15-
"github.com/go-xorm/xorm"
1615
)
1716

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

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

4339
// TrStr returns a translation format string.

models/attachment.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"path"
1313
"time"
1414

15-
"github.com/go-xorm/xorm"
1615
gouuid "github.com/satori/go.uuid"
1716

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

34-
// AfterSet is invoked from XORM after setting the value of a field of
33+
// AfterLoad is invoked from XORM after setting the value of a field of
3534
// this object.
36-
func (a *Attachment) AfterSet(colName string, _ xorm.Cell) {
37-
switch colName {
38-
case "created_unix":
39-
a.Created = time.Unix(a.CreatedUnix, 0).Local()
40-
}
35+
func (a *Attachment) AfterLoad() {
36+
a.Created = time.Unix(a.CreatedUnix, 0).Local()
4137
}
4238

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

134130
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
135131
func GetAttachmentsByCommentID(commentID int64) ([]*Attachment, error) {
132+
return getAttachmentsByCommentID(x, commentID)
133+
}
134+
135+
func getAttachmentsByCommentID(e Engine, commentID int64) ([]*Attachment, error) {
136136
attachments := make([]*Attachment, 0, 10)
137137
return attachments, x.Where("comment_id=?", commentID).Find(&attachments)
138138
}

models/gpg_key.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ func (key *GPGKey) BeforeInsert() {
5252
key.CreatedUnix = key.Created.Unix()
5353
}
5454

55-
// AfterSet is invoked from XORM after setting the value of a field of this object.
56-
func (key *GPGKey) AfterSet(colName string, _ xorm.Cell) {
57-
switch colName {
58-
case "key_id":
59-
x.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
60-
case "added_unix":
61-
key.Added = time.Unix(key.AddedUnix, 0).Local()
62-
case "expired_unix":
63-
key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
64-
case "created_unix":
65-
key.Created = time.Unix(key.CreatedUnix, 0).Local()
55+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
56+
func (key *GPGKey) AfterLoad(session *xorm.Session) {
57+
key.Added = time.Unix(key.AddedUnix, 0).Local()
58+
key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
59+
key.Created = time.Unix(key.CreatedUnix, 0).Local()
60+
61+
err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
62+
if err != nil {
63+
log.Error(3, "Find Sub GPGkeys[%d]: %v", key.KeyID, err)
6664
}
6765
}
6866

models/issue.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,12 @@ func (issue *Issue) BeforeUpdate() {
6767
issue.DeadlineUnix = issue.Deadline.Unix()
6868
}
6969

70-
// AfterSet is invoked from XORM after setting the value of a field of
70+
// AfterLoad is invoked from XORM after setting the value of a field of
7171
// this object.
72-
func (issue *Issue) AfterSet(colName string, _ xorm.Cell) {
73-
switch colName {
74-
case "deadline_unix":
75-
issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local()
76-
case "created_unix":
77-
issue.Created = time.Unix(issue.CreatedUnix, 0).Local()
78-
case "updated_unix":
79-
issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local()
80-
}
72+
func (issue *Issue) AfterLoad() {
73+
issue.Deadline = time.Unix(issue.DeadlineUnix, 0).Local()
74+
issue.Created = time.Unix(issue.CreatedUnix, 0).Local()
75+
issue.Updated = time.Unix(issue.UpdatedUnix, 0).Local()
8176
}
8277

8378
func (issue *Issue) loadRepo(e Engine) (err error) {

models/issue_comment.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,25 @@ type Comment struct {
112112
ShowTag CommentTag `xorm:"-"`
113113
}
114114

115-
// AfterSet is invoked from XORM after setting the value of a field of this object.
116-
func (c *Comment) AfterSet(colName string, _ xorm.Cell) {
115+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
116+
func (c *Comment) AfterLoad(session *xorm.Session) {
117+
c.Created = time.Unix(c.CreatedUnix, 0).Local()
118+
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
119+
117120
var err error
118-
switch colName {
119-
case "id":
120-
c.Attachments, err = GetAttachmentsByCommentID(c.ID)
121-
if err != nil {
122-
log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err)
123-
}
121+
c.Attachments, err = getAttachmentsByCommentID(session, c.ID)
122+
if err != nil {
123+
log.Error(3, "getAttachmentsByCommentID[%d]: %v", c.ID, err)
124+
}
124125

125-
case "poster_id":
126-
c.Poster, err = GetUserByID(c.PosterID)
127-
if err != nil {
128-
if IsErrUserNotExist(err) {
129-
c.PosterID = -1
130-
c.Poster = NewGhostUser()
131-
} else {
132-
log.Error(3, "GetUserByID[%d]: %v", c.ID, err)
133-
}
126+
c.Poster, err = getUserByID(session, c.PosterID)
127+
if err != nil {
128+
if IsErrUserNotExist(err) {
129+
c.PosterID = -1
130+
c.Poster = NewGhostUser()
131+
} else {
132+
log.Error(3, "getUserByID[%d]: %v", c.ID, err)
134133
}
135-
case "created_unix":
136-
c.Created = time.Unix(c.CreatedUnix, 0).Local()
137-
case "updated_unix":
138-
c.Updated = time.Unix(c.UpdatedUnix, 0).Local()
139134
}
140135
}
141136

models/issue_milestone.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,21 @@ func (m *Milestone) BeforeUpdate() {
5151
m.ClosedDateUnix = m.ClosedDate.Unix()
5252
}
5353

54-
// AfterSet is invoked from XORM after setting the value of a field of
54+
// AfterLoad is invoked from XORM after setting the value of a field of
5555
// this object.
56-
func (m *Milestone) AfterSet(colName string, _ xorm.Cell) {
57-
switch colName {
58-
case "num_closed_issues":
59-
m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
60-
61-
case "deadline_unix":
62-
m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
63-
if m.Deadline.Year() == 9999 {
64-
return
65-
}
66-
67-
m.DeadlineString = m.Deadline.Format("2006-01-02")
68-
if time.Now().Local().After(m.Deadline) {
69-
m.IsOverDue = true
70-
}
56+
func (m *Milestone) AfterLoad() {
57+
m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
58+
m.Deadline = time.Unix(m.DeadlineUnix, 0).Local()
59+
if m.Deadline.Year() == 9999 {
60+
return
61+
}
7162

72-
case "closed_date_unix":
73-
m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
63+
m.DeadlineString = m.Deadline.Format("2006-01-02")
64+
if time.Now().Local().After(m.Deadline) {
65+
m.IsOverDue = true
7466
}
67+
68+
m.ClosedDate = time.Unix(m.ClosedDateUnix, 0).Local()
7569
}
7670

7771
// State returns string representation of milestone status.

models/issue_stopwatch.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package models
77
import (
88
"fmt"
99
"time"
10-
11-
"github.com/go-xorm/xorm"
1210
)
1311

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

29-
// AfterSet is invoked from XORM after setting the value of a field of this object.
30-
func (s *Stopwatch) AfterSet(colName string, _ xorm.Cell) {
31-
switch colName {
32-
33-
case "created_unix":
34-
s.Created = time.Unix(s.CreatedUnix, 0).Local()
35-
}
27+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
28+
func (s *Stopwatch) AfterLoad() {
29+
s.Created = time.Unix(s.CreatedUnix, 0).Local()
3630
}
3731

3832
func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {

models/issue_tracked_time.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/go-xorm/builder"
11-
"github.com/go-xorm/xorm"
1211
)
1312

1413
// TrackedTime represents a time that was spent for a specific issue.
@@ -17,22 +16,13 @@ type TrackedTime struct {
1716
IssueID int64 `xorm:"INDEX" json:"issue_id"`
1817
UserID int64 `xorm:"INDEX" json:"user_id"`
1918
Created time.Time `xorm:"-" json:"created"`
20-
CreatedUnix int64 `json:"-"`
19+
CreatedUnix int64 `xorm:"created" json:"-"`
2120
Time int64 `json:"time"`
2221
}
2322

24-
// BeforeInsert will be invoked by XORM before inserting a record
25-
// representing this object.
26-
func (t *TrackedTime) BeforeInsert() {
27-
t.CreatedUnix = time.Now().Unix()
28-
}
29-
30-
// AfterSet is invoked from XORM after setting the value of a field of this object.
31-
func (t *TrackedTime) AfterSet(colName string, _ xorm.Cell) {
32-
switch colName {
33-
case "created_unix":
34-
t.Created = time.Unix(t.CreatedUnix, 0).Local()
35-
}
23+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
24+
func (t *TrackedTime) AfterLoad() {
25+
t.Created = time.Unix(t.CreatedUnix, 0).Local()
3626
}
3727

3828
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.

models/lfs.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package models
33
import (
44
"errors"
55
"time"
6-
7-
"github.com/go-xorm/xorm"
86
)
97

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

112-
// AfterSet stores the LFSMetaObject creation time in the database as local time.
113-
func (m *LFSMetaObject) AfterSet(colName string, _ xorm.Cell) {
114-
switch colName {
115-
case "created_unix":
116-
m.Created = time.Unix(m.CreatedUnix, 0).Local()
117-
}
110+
// AfterLoad stores the LFSMetaObject creation time in the database as local time.
111+
func (m *LFSMetaObject) AfterLoad() {
112+
m.Created = time.Unix(m.CreatedUnix, 0).Local()
118113
}

models/login_source.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,10 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
183183
}
184184
}
185185

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

196192
// TypeName return name of this login source type.

models/pull.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,14 @@ func (pr *PullRequest) BeforeUpdate() {
8080
pr.MergedUnix = pr.Merged.Unix()
8181
}
8282

83-
// AfterSet is invoked from XORM after setting the value of a field of this object.
83+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
8484
// Note: don't try to get Issue because will end up recursive querying.
85-
func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
86-
switch colName {
87-
case "merged_unix":
88-
if !pr.HasMerged {
89-
return
90-
}
91-
92-
pr.Merged = time.Unix(pr.MergedUnix, 0).Local()
85+
func (pr *PullRequest) AfterLoad() {
86+
if !pr.HasMerged {
87+
return
9388
}
89+
90+
pr.Merged = time.Unix(pr.MergedUnix, 0).Local()
9491
}
9592

9693
// Note: don't try to get Issue because will end up recursive querying.

models/release.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/setting"
1616
api "code.gitea.io/sdk/gitea"
1717
"github.com/go-xorm/builder"
18-
"github.com/go-xorm/xorm"
1918
)
2019

2120
// Release represents a release of repository.
@@ -50,12 +49,9 @@ func (r *Release) BeforeInsert() {
5049
}
5150
}
5251

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

6157
func (r *Release) loadAttributes(e Engine) error {

0 commit comments

Comments
 (0)