-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchangeset.go
162 lines (134 loc) · 3.46 KB
/
changeset.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Package changeset used to cast and validate data before saving it to the database.
package changeset
import (
"database/sql"
"reflect"
"time"
"github.com/go-rel/changeset/params"
"github.com/go-rel/rel"
)
// Changeset used to cast and validate data before saving it to the database.
type Changeset struct {
errors []error
params params.Params
changes map[string]interface{}
values map[string]interface{}
types map[string]reflect.Type
constraints Constraints
zero bool
ignorePrimary bool
}
// Errors of changeset.
func (c Changeset) Errors() []error {
return c.errors
}
// Error of changeset, returns the first error if any.
func (c Changeset) Error() error {
if c.errors != nil {
return c.errors[0]
}
return nil
}
// Get a change from changeset.
func (c Changeset) Get(field string) interface{} {
return c.changes[field]
}
// Fetch a change or value from changeset.
func (c Changeset) Fetch(field string) interface{} {
if change, ok := c.changes[field]; ok {
return change
}
return c.values[field]
}
// Changes of changeset.
func (c Changeset) Changes() map[string]interface{} {
return c.changes
}
// Values of changeset.
func (c Changeset) Values() map[string]interface{} {
return c.values
}
// Types of changeset.
func (c Changeset) Types() map[string]reflect.Type {
return c.types
}
// Constraints of changeset.
func (c Changeset) Constraints() Constraints {
return c.constraints
}
// Apply mutation.
func (c *Changeset) Apply(doc *rel.Document, mut *rel.Mutation) {
var (
pField = doc.PrimaryField()
now = time.Now().Truncate(time.Second)
)
for field, value := range c.changes {
switch v := value.(type) {
case *Changeset:
if mut.Cascade {
c.applyAssocOne(doc, field, mut, v)
}
case []*Changeset:
if mut.Cascade {
c.applyAssocMany(doc, field, mut, v)
}
default:
if (pField != field || pField == field && !c.ignorePrimary) && scannable(c.types[field]) {
c.set(doc, mut, field, v)
}
}
}
// insert timestamp
if doc.Flag(rel.HasCreatedAt) {
if value, ok := doc.Value("created_at"); ok && value.(time.Time).IsZero() {
c.set(doc, mut, "created_at", now)
}
}
// update timestamp
if doc.Flag(rel.HasUpdatedAt) {
c.set(doc, mut, "updated_at", now)
}
// add error func
if len(c.constraints) > 0 {
mut.ErrorFunc = c.constraints.GetError
}
}
func (c *Changeset) set(doc *rel.Document, mut *rel.Mutation, field string, value interface{}) {
if doc.SetValue(field, value) {
mut.Add(rel.Set(field, value))
}
}
func (c *Changeset) applyAssocOne(doc *rel.Document, field string, mut *rel.Mutation, ch *Changeset) {
var (
assoc = doc.Association(field)
assocDoc, _ = assoc.Document()
assocMut = rel.Apply(assocDoc, ch)
)
mut.SetAssoc(field, assocMut)
}
func (c *Changeset) applyAssocMany(doc *rel.Document, field string, mut *rel.Mutation, chs []*Changeset) {
var (
assoc = doc.Association(field)
col, _ = assoc.Collection()
muts = make([]rel.Mutation, len(chs))
)
// Reset assoc.
col.Reset()
for i := range chs {
muts[i] = rel.Apply(col.Add(), chs[i])
}
mut.SetAssoc(field, muts...)
}
var (
rtTime = reflect.TypeOf(time.Time{})
rtScanner = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
)
func scannable(rt reflect.Type) bool {
var (
kind = rt.Kind()
)
return !((kind == reflect.Struct || kind == reflect.Slice || kind == reflect.Array) &&
kind != reflect.Uint8 &&
!rt.Implements(rtScanner) &&
!rt.ConvertibleTo(rtTime))
}