/
action_create.go
290 lines (268 loc) · 7.39 KB
/
action_create.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
279
280
281
282
283
284
285
286
287
288
289
290
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"github.com/blushft/strana/modules/sink/reporter/store/ent/action"
"github.com/blushft/strana/modules/sink/reporter/store/ent/event"
"github.com/facebook/ent/dialect/sql/sqlgraph"
"github.com/facebook/ent/schema/field"
"github.com/google/uuid"
)
// ActionCreate is the builder for creating a Action entity.
type ActionCreate struct {
config
mutation *ActionMutation
hooks []Hook
}
// SetAction sets the action field.
func (ac *ActionCreate) SetAction(s string) *ActionCreate {
ac.mutation.SetAction(s)
return ac
}
// SetCategory sets the category field.
func (ac *ActionCreate) SetCategory(s string) *ActionCreate {
ac.mutation.SetCategory(s)
return ac
}
// SetActionLabel sets the action_label field.
func (ac *ActionCreate) SetActionLabel(s string) *ActionCreate {
ac.mutation.SetActionLabel(s)
return ac
}
// SetNillableActionLabel sets the action_label field if the given value is not nil.
func (ac *ActionCreate) SetNillableActionLabel(s *string) *ActionCreate {
if s != nil {
ac.SetActionLabel(*s)
}
return ac
}
// SetProperty sets the property field.
func (ac *ActionCreate) SetProperty(s string) *ActionCreate {
ac.mutation.SetProperty(s)
return ac
}
// SetNillableProperty sets the property field if the given value is not nil.
func (ac *ActionCreate) SetNillableProperty(s *string) *ActionCreate {
if s != nil {
ac.SetProperty(*s)
}
return ac
}
// SetValue sets the value field.
func (ac *ActionCreate) SetValue(b []byte) *ActionCreate {
ac.mutation.SetValue(b)
return ac
}
// SetEventID sets the event edge to Event by id.
func (ac *ActionCreate) SetEventID(id uuid.UUID) *ActionCreate {
ac.mutation.SetEventID(id)
return ac
}
// SetEvent sets the event edge to Event.
func (ac *ActionCreate) SetEvent(e *Event) *ActionCreate {
return ac.SetEventID(e.ID)
}
// Mutation returns the ActionMutation object of the builder.
func (ac *ActionCreate) Mutation() *ActionMutation {
return ac.mutation
}
// Save creates the Action in the database.
func (ac *ActionCreate) Save(ctx context.Context) (*Action, error) {
if err := ac.preSave(); err != nil {
return nil, err
}
var (
err error
node *Action
)
if len(ac.hooks) == 0 {
node, err = ac.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ActionMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
ac.mutation = mutation
node, err = ac.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(ac.hooks) - 1; i >= 0; i-- {
mut = ac.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, ac.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (ac *ActionCreate) SaveX(ctx context.Context) *Action {
v, err := ac.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (ac *ActionCreate) preSave() error {
if _, ok := ac.mutation.Action(); !ok {
return &ValidationError{Name: "action", err: errors.New("ent: missing required field \"action\"")}
}
if _, ok := ac.mutation.Category(); !ok {
return &ValidationError{Name: "category", err: errors.New("ent: missing required field \"category\"")}
}
if _, ok := ac.mutation.EventID(); !ok {
return &ValidationError{Name: "event", err: errors.New("ent: missing required edge \"event\"")}
}
return nil
}
func (ac *ActionCreate) sqlSave(ctx context.Context) (*Action, error) {
a, _spec := ac.createSpec()
if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
a.ID = int(id)
return a, nil
}
func (ac *ActionCreate) createSpec() (*Action, *sqlgraph.CreateSpec) {
var (
a = &Action{config: ac.config}
_spec = &sqlgraph.CreateSpec{
Table: action.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: action.FieldID,
},
}
)
if value, ok := ac.mutation.Action(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: action.FieldAction,
})
a.Action = value
}
if value, ok := ac.mutation.Category(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: action.FieldCategory,
})
a.Category = value
}
if value, ok := ac.mutation.ActionLabel(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: action.FieldActionLabel,
})
a.ActionLabel = value
}
if value, ok := ac.mutation.Property(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: action.FieldProperty,
})
a.Property = value
}
if value, ok := ac.mutation.Value(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBytes,
Value: value,
Column: action.FieldValue,
})
a.Value = value
}
if nodes := ac.mutation.EventIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: true,
Table: action.EventTable,
Columns: []string{action.EventColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeUUID,
Column: event.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return a, _spec
}
// ActionCreateBulk is the builder for creating a bulk of Action entities.
type ActionCreateBulk struct {
config
builders []*ActionCreate
}
// Save creates the Action entities in the database.
func (acb *ActionCreateBulk) Save(ctx context.Context) ([]*Action, error) {
specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
nodes := make([]*Action, len(acb.builders))
mutators := make([]Mutator, len(acb.builders))
for i := range acb.builders {
func(i int, root context.Context) {
builder := acb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
if err := builder.preSave(); err != nil {
return nil, err
}
mutation, ok := m.(*ActionMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
builder.mutation = mutation
nodes[i], specs[i] = builder.createSpec()
var err error
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, acb.builders[i+1].mutation)
} else {
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, acb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
}
}
mutation.done = true
if err != nil {
return nil, err
}
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(id)
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, acb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX calls Save and panics if Save returns an error.
func (acb *ActionCreateBulk) SaveX(ctx context.Context) []*Action {
v, err := acb.Save(ctx)
if err != nil {
panic(err)
}
return v
}