-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
mutation.go
278 lines (231 loc) · 5.88 KB
/
mutation.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package rel
import (
"fmt"
"reflect"
)
// Mutator is interface for a entity mutator.
type Mutator interface {
Apply(doc *Document, mutation *Mutation)
}
// Apply using given mutators.
func Apply(doc *Document, mutators ...Mutator) Mutation {
return applyMutators(doc, true, true, mutators...)
}
// apply given mutators with customized default values
func applyMutators(doc *Document, cascade, applyStructset bool, mutators ...Mutator) Mutation {
var (
optionsCount int
mutation = Mutation{
Unscoped: false,
Reload: false,
Cascade: Cascade(cascade),
}
)
for i := range mutators {
switch mut := mutators[i].(type) {
case Unscoped, Reload, Cascade, OnConflict:
optionsCount++
mut.Apply(doc, &mutation)
default:
mut.Apply(doc, &mutation)
}
}
// fallback to structset.
if applyStructset && optionsCount == len(mutators) {
newStructset(doc, false).Apply(doc, &mutation)
}
return mutation
}
// AssocMutation represents mutation for association.
type AssocMutation struct {
Mutations []Mutation
DeletedIDs []any // This is array of single id, and doesn't support composite primary key.
}
// Mutation represents value to be inserted or updated to database.
// It's not safe to be used multiple time. some operation my alter mutation data.
type Mutation struct {
Mutates map[string]Mutate
Assoc map[string]AssocMutation
OnConflict OnConflict
Unscoped Unscoped
Reload Reload
Cascade Cascade
ErrorFunc ErrorFunc
}
func (m *Mutation) initMutates() {
if m.Mutates == nil {
m.Mutates = make(map[string]Mutate)
}
}
func (m *Mutation) initAssoc() {
if m.Assoc == nil {
m.Assoc = make(map[string]AssocMutation)
}
}
// IsEmpty returns true if no mutates operation and assoc's mutation is defined.
func (m *Mutation) IsEmpty() bool {
return m.IsMutatesEmpty() && m.IsAssocEmpty()
}
// IsMutatesEmpty returns true if no mutates operation is defined.
func (m *Mutation) IsMutatesEmpty() bool {
return len(m.Mutates) == 0
}
// IsAssocEmpty returns true if no assoc's mutation is defined.
func (m *Mutation) IsAssocEmpty() bool {
return len(m.Assoc) == 0
}
// Add a mutate.
func (m *Mutation) Add(mut Mutate) {
m.initMutates()
m.Mutates[mut.Field] = mut
}
// SetAssoc mutation.
func (m *Mutation) SetAssoc(field string, muts ...Mutation) {
m.initAssoc()
assoc := m.Assoc[field]
assoc.Mutations = muts
m.Assoc[field] = assoc
}
// SetDeletedIDs mutation.
// nil slice will clear association.
func (m *Mutation) SetDeletedIDs(field string, ids []any) {
m.initAssoc()
assoc := m.Assoc[field]
assoc.DeletedIDs = ids
m.Assoc[field] = assoc
}
// ChangeOp represents type of mutate operation.
type ChangeOp int
const (
// ChangeInvalidOp operation.
ChangeInvalidOp ChangeOp = iota
// ChangeSetOp operation.
ChangeSetOp
// ChangeIncOp operation.
ChangeIncOp
// ChangeFragmentOp operation.
ChangeFragmentOp
)
// Mutate stores mutation instruction.
type Mutate struct {
Type ChangeOp
Field string
Value any
}
// Apply mutation.
func (m Mutate) Apply(doc *Document, mutation *Mutation) {
invalid := false
switch m.Type {
case ChangeSetOp:
if !doc.SetValue(m.Field, m.Value) {
invalid = true
}
case ChangeFragmentOp:
mutation.Reload = true
default:
if typ, ok := doc.Type(m.Field); ok {
kind := typ.Kind()
invalid = m.Type == ChangeIncOp && (kind < reflect.Int || kind > reflect.Uint64)
} else {
invalid = true
}
mutation.Reload = true
}
if invalid {
panic(fmt.Sprint("rel: cannot assign ", m.Value, " as ", m.Field, " into ", doc.Table()))
}
mutation.Add(m)
}
// String representation
func (m Mutate) String() string {
str := "≤Invalid Mutator>"
switch m.Type {
case ChangeSetOp:
str = fmt.Sprintf("rel.Set(\"%s\", %s)", m.Field, fmtAny(m.Value))
case ChangeIncOp:
str = fmt.Sprintf("rel.IncBy(\"%s\", %s)", m.Field, fmtAny(m.Value))
case ChangeFragmentOp:
str = fmt.Sprintf("rel.SetFragment(\"%s\", %s)", m.Field, fmtAnys(m.Value.([]any)))
}
return str
}
// Set create a mutate using set operation.
func Set(field string, value any) Mutate {
return Mutate{
Type: ChangeSetOp,
Field: field,
Value: value,
}
}
// Inc create a mutate using increment operation.
func Inc(field string) Mutate {
return IncBy(field, 1)
}
// IncBy create a mutate using increment operation with custom increment value.
func IncBy(field string, n int) Mutate {
return Mutate{
Type: ChangeIncOp,
Field: field,
Value: n,
}
}
// Dec create a mutate using deccrement operation.
func Dec(field string) Mutate {
return DecBy(field, 1)
}
// DecBy create a mutate using decrement operation with custom decrement value.
func DecBy(field string, n int) Mutate {
return Mutate{
Type: ChangeIncOp,
Field: field,
Value: -n,
}
}
// SetFragment create a mutate operation using fragment operation.
// Only available for Update.
func SetFragment(raw string, args ...any) Mutate {
return Mutate{
Type: ChangeFragmentOp,
Field: raw,
Value: args,
}
}
// Setf is an alias for SetFragment
var Setf = SetFragment
// Reload force reload after insert/update.
// Default to false.
type Reload bool
// Apply mutation.
func (r Reload) Apply(doc *Document, mutation *Mutation) {
mutation.Reload = r
}
// Build query.
func (r Reload) Build(query *Query) {
query.ReloadQuery = r
}
// Cascade enable or disable updating associations.
// Default to true.
type Cascade bool
// Build query.
func (c Cascade) Build(query *Query) {
query.CascadeQuery = c
}
// Apply mutation.
func (c Cascade) Apply(doc *Document, mutation *Mutation) {
mutation.Cascade = c
}
func (c Cascade) String() string {
return fmt.Sprintf("rel.Cascade(%t)", c)
}
// ErrorFunc allows conversion REL's error to Application custom errors.
type ErrorFunc func(error) error
// Apply mutation.
func (ef ErrorFunc) Apply(doc *Document, mutation *Mutation) {
mutation.ErrorFunc = ef
}
func (ef ErrorFunc) transform(err error) error {
if ef != nil && err != nil {
return ef(err)
}
return err
}