-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathstructset.go
138 lines (113 loc) · 2.84 KB
/
structset.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
package rel
import (
"fmt"
"time"
)
var (
Now NowFunc = func() time.Time {
return time.Now().Truncate(time.Second)
}
)
// NowFunc is the type of function that returns the current time.
type NowFunc func() time.Time
// Structset can be used as mutation for repository insert or update operation.
// This will save every field in struct and it's association as long as it's loaded.
// This is the default mutator used by repository.
type Structset struct {
doc *Document
skipZero bool
}
// Apply mutation.
func (s Structset) Apply(doc *Document, mut *Mutation) {
var (
pFields = s.doc.PrimaryFields()
t = Now()
)
for _, field := range s.doc.Fields() {
switch field {
case "created_at", "inserted_at":
if doc.Flag(HasCreatedAt) {
if value, ok := doc.Value(field); ok && value.(time.Time).IsZero() {
s.set(doc, mut, field, t, true)
continue
}
}
case "updated_at":
if doc.Flag(HasUpdatedAt) {
s.set(doc, mut, field, t, true)
continue
}
}
if len(pFields) == 1 && pFields[0] == field {
// allow setting primary key as long as it's not zero.
s.applyValue(doc, mut, field, true)
} else {
s.applyValue(doc, mut, field, s.skipZero)
}
}
if mut.Cascade {
s.applyAssoc(mut)
}
}
func (s Structset) set(doc *Document, mut *Mutation, field string, value any, force bool) {
if (force || doc.v != s.doc.v) && !doc.SetValue(field, value) {
panic(fmt.Sprint("rel: cannot assign ", value, " as ", field, " into ", doc.Table()))
}
mut.Add(Set(field, value))
}
func (s Structset) applyValue(doc *Document, mut *Mutation, field string, skipZero bool) {
if value, ok := s.doc.Value(field); ok {
if skipZero && isZero(value) {
return
}
s.set(doc, mut, field, value, false)
}
}
func (s Structset) applyAssoc(mut *Mutation) {
for _, field := range s.doc.BelongsTo() {
s.buildAssoc(field, mut)
}
for _, field := range s.doc.HasOne() {
s.buildAssoc(field, mut)
}
for _, field := range s.doc.HasMany() {
s.buildAssocMany(field, mut)
}
}
func (s Structset) buildAssoc(field string, mut *Mutation) {
assoc := s.doc.Association(field)
if assoc.IsZero() {
return
}
var (
doc, _ = assoc.Document()
)
mut.SetAssoc(field, Apply(doc, newStructset(doc, s.skipZero)))
}
func (s Structset) buildAssocMany(field string, mut *Mutation) {
assoc := s.doc.Association(field)
if assoc.IsZero() {
return
}
var (
col, _ = assoc.Collection()
muts = make([]Mutation, col.Len())
)
for i := range muts {
var (
doc = col.Get(i)
)
muts[i] = Apply(doc, newStructset(doc, s.skipZero))
}
mut.SetAssoc(field, muts...)
}
func newStructset(doc *Document, skipZero bool) Structset {
return Structset{
doc: doc,
skipZero: skipZero,
}
}
// NewStructset from a struct.
func NewStructset(entity any, skipZero bool) Structset {
return newStructset(NewDocument(entity), skipZero)
}