forked from go-reform/reform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
good.go
128 lines (106 loc) · 3.31 KB
/
good.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package models
import (
"time"
"github.com/AlekSi/pointer"
"gopkg.in/reform.v1"
)
//go:generate reform
type (
//reform:people
Person struct {
ID int32 `reform:"id,pk"`
GroupID *int32 `reform:"group_id"`
Name string `reform:"name"`
Email *string `reform:"email"`
CreatedAt time.Time `reform:"created_at"`
UpdatedAt *time.Time `reform:"updated_at"`
}
)
// BeforeInsert sets CreatedAt if it's not set,
// then converts to UTC, truncates to second and strips monotonic clock reading from both CreatedAt and UpdatedAt.
func (p *Person) BeforeInsert() error {
if p.CreatedAt.IsZero() {
p.CreatedAt = time.Now()
}
p.CreatedAt = p.CreatedAt.UTC().Truncate(time.Second).AddDate(0, 0, 0)
if p.UpdatedAt != nil {
p.UpdatedAt = pointer.ToTime(p.UpdatedAt.UTC().Truncate(time.Second).AddDate(0, 0, 0))
}
return nil
}
// BeforeUpdate sets CreatedAt if it's not set,
// sets UpdatedAt,
// then converts to UTC, truncates to second and strips monotonic clock reading from both CreatedAt and UpdatedAt.
func (p *Person) BeforeUpdate() error {
now := time.Now()
if p.CreatedAt.IsZero() {
p.CreatedAt = now
}
p.UpdatedAt = &now
p.CreatedAt = p.CreatedAt.UTC().Truncate(time.Second).AddDate(0, 0, 0)
p.UpdatedAt = pointer.ToTime(p.UpdatedAt.UTC().Truncate(time.Second).AddDate(0, 0, 0))
return nil
}
// AfterFind converts to UTC and truncates to second both CreatedAt and UpdatedAt.
func (p *Person) AfterFind() error {
p.CreatedAt = p.CreatedAt.UTC().Truncate(time.Second)
if p.UpdatedAt != nil {
p.UpdatedAt = pointer.ToTime(p.UpdatedAt.UTC().Truncate(time.Second))
}
return nil
}
// Project represents row in table projects
// (reform:projects).
type Project struct {
Name string `reform:"name"`
ID string `reform:"id,pk"`
Start time.Time `reform:"start"`
End *time.Time `reform:"end"`
}
// BeforeInsert converts to UTC, truncates to day and strips monotonic clock reading from both Start and End.
func (p *Project) BeforeInsert() error {
p.Start = p.Start.UTC().Truncate(24*time.Hour).AddDate(0, 0, 0)
if p.End != nil {
p.End = pointer.ToTime(p.End.UTC().Truncate(24*time.Hour).AddDate(0, 0, 0))
}
return nil
}
// BeforeUpdate converts to UTC, truncates to day and strips monotonic clock reading from both Start and End.
func (p *Project) BeforeUpdate() error {
p.Start = p.Start.UTC().Truncate(24*time.Hour).AddDate(0, 0, 0)
if p.End != nil {
p.End = pointer.ToTime(p.End.UTC().Truncate(24*time.Hour).AddDate(0, 0, 0))
}
return nil
}
// AfterFind converts to UTC both Start and End.
func (p *Project) AfterFind() error {
p.Start = p.Start.UTC()
if p.End != nil {
p.End = pointer.ToTime(p.End.UTC())
}
return nil
}
// PersonProject represents row in table person_project. reform:person_project
type PersonProject struct {
PersonID int32 `reform:"person_id"`
ProjectID string `reform:"project_id"`
}
//reform:legacy.people
type LegacyPerson struct {
ID int32 `reform:"id,pk"`
Name *string `reform:"name"`
}
// reform:id_only
type IDOnly struct {
ID int32 `reform:"id,pk"`
}
// check interfaces
var (
_ reform.BeforeInserter = (*Person)(nil)
_ reform.BeforeUpdater = (*Person)(nil)
_ reform.AfterFinder = (*Person)(nil)
_ reform.BeforeInserter = (*Project)(nil)
_ reform.BeforeUpdater = (*Project)(nil)
_ reform.AfterFinder = (*Project)(nil)
)