-
Notifications
You must be signed in to change notification settings - Fork 40
/
workflowinstance_create.go
575 lines (531 loc) · 16.4 KB
/
workflowinstance_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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
"github.com/vorteil/direktiv/ent/workflow"
"github.com/vorteil/direktiv/ent/workflowevents"
"github.com/vorteil/direktiv/ent/workflowinstance"
)
// WorkflowInstanceCreate is the builder for creating a WorkflowInstance entity.
type WorkflowInstanceCreate struct {
config
mutation *WorkflowInstanceMutation
hooks []Hook
}
// SetInstanceID sets the "instanceID" field.
func (wic *WorkflowInstanceCreate) SetInstanceID(s string) *WorkflowInstanceCreate {
wic.mutation.SetInstanceID(s)
return wic
}
// SetInvokedBy sets the "invokedBy" field.
func (wic *WorkflowInstanceCreate) SetInvokedBy(s string) *WorkflowInstanceCreate {
wic.mutation.SetInvokedBy(s)
return wic
}
// SetStatus sets the "status" field.
func (wic *WorkflowInstanceCreate) SetStatus(s string) *WorkflowInstanceCreate {
wic.mutation.SetStatus(s)
return wic
}
// SetRevision sets the "revision" field.
func (wic *WorkflowInstanceCreate) SetRevision(i int) *WorkflowInstanceCreate {
wic.mutation.SetRevision(i)
return wic
}
// SetBeginTime sets the "beginTime" field.
func (wic *WorkflowInstanceCreate) SetBeginTime(t time.Time) *WorkflowInstanceCreate {
wic.mutation.SetBeginTime(t)
return wic
}
// SetEndTime sets the "endTime" field.
func (wic *WorkflowInstanceCreate) SetEndTime(t time.Time) *WorkflowInstanceCreate {
wic.mutation.SetEndTime(t)
return wic
}
// SetNillableEndTime sets the "endTime" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableEndTime(t *time.Time) *WorkflowInstanceCreate {
if t != nil {
wic.SetEndTime(*t)
}
return wic
}
// SetFlow sets the "flow" field.
func (wic *WorkflowInstanceCreate) SetFlow(s []string) *WorkflowInstanceCreate {
wic.mutation.SetFlow(s)
return wic
}
// SetInput sets the "input" field.
func (wic *WorkflowInstanceCreate) SetInput(s string) *WorkflowInstanceCreate {
wic.mutation.SetInput(s)
return wic
}
// SetOutput sets the "output" field.
func (wic *WorkflowInstanceCreate) SetOutput(s string) *WorkflowInstanceCreate {
wic.mutation.SetOutput(s)
return wic
}
// SetNillableOutput sets the "output" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableOutput(s *string) *WorkflowInstanceCreate {
if s != nil {
wic.SetOutput(*s)
}
return wic
}
// SetStateData sets the "stateData" field.
func (wic *WorkflowInstanceCreate) SetStateData(s string) *WorkflowInstanceCreate {
wic.mutation.SetStateData(s)
return wic
}
// SetNillableStateData sets the "stateData" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableStateData(s *string) *WorkflowInstanceCreate {
if s != nil {
wic.SetStateData(*s)
}
return wic
}
// SetMemory sets the "memory" field.
func (wic *WorkflowInstanceCreate) SetMemory(s string) *WorkflowInstanceCreate {
wic.mutation.SetMemory(s)
return wic
}
// SetNillableMemory sets the "memory" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableMemory(s *string) *WorkflowInstanceCreate {
if s != nil {
wic.SetMemory(*s)
}
return wic
}
// SetDeadline sets the "deadline" field.
func (wic *WorkflowInstanceCreate) SetDeadline(t time.Time) *WorkflowInstanceCreate {
wic.mutation.SetDeadline(t)
return wic
}
// SetNillableDeadline sets the "deadline" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableDeadline(t *time.Time) *WorkflowInstanceCreate {
if t != nil {
wic.SetDeadline(*t)
}
return wic
}
// SetAttempts sets the "attempts" field.
func (wic *WorkflowInstanceCreate) SetAttempts(i int) *WorkflowInstanceCreate {
wic.mutation.SetAttempts(i)
return wic
}
// SetNillableAttempts sets the "attempts" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableAttempts(i *int) *WorkflowInstanceCreate {
if i != nil {
wic.SetAttempts(*i)
}
return wic
}
// SetErrorCode sets the "errorCode" field.
func (wic *WorkflowInstanceCreate) SetErrorCode(s string) *WorkflowInstanceCreate {
wic.mutation.SetErrorCode(s)
return wic
}
// SetNillableErrorCode sets the "errorCode" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableErrorCode(s *string) *WorkflowInstanceCreate {
if s != nil {
wic.SetErrorCode(*s)
}
return wic
}
// SetErrorMessage sets the "errorMessage" field.
func (wic *WorkflowInstanceCreate) SetErrorMessage(s string) *WorkflowInstanceCreate {
wic.mutation.SetErrorMessage(s)
return wic
}
// SetNillableErrorMessage sets the "errorMessage" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableErrorMessage(s *string) *WorkflowInstanceCreate {
if s != nil {
wic.SetErrorMessage(*s)
}
return wic
}
// SetStateBeginTime sets the "stateBeginTime" field.
func (wic *WorkflowInstanceCreate) SetStateBeginTime(t time.Time) *WorkflowInstanceCreate {
wic.mutation.SetStateBeginTime(t)
return wic
}
// SetNillableStateBeginTime sets the "stateBeginTime" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableStateBeginTime(t *time.Time) *WorkflowInstanceCreate {
if t != nil {
wic.SetStateBeginTime(*t)
}
return wic
}
// SetController sets the "controller" field.
func (wic *WorkflowInstanceCreate) SetController(s string) *WorkflowInstanceCreate {
wic.mutation.SetController(s)
return wic
}
// SetNillableController sets the "controller" field if the given value is not nil.
func (wic *WorkflowInstanceCreate) SetNillableController(s *string) *WorkflowInstanceCreate {
if s != nil {
wic.SetController(*s)
}
return wic
}
// SetWorkflowID sets the "workflow" edge to the Workflow entity by ID.
func (wic *WorkflowInstanceCreate) SetWorkflowID(id uuid.UUID) *WorkflowInstanceCreate {
wic.mutation.SetWorkflowID(id)
return wic
}
// SetWorkflow sets the "workflow" edge to the Workflow entity.
func (wic *WorkflowInstanceCreate) SetWorkflow(w *Workflow) *WorkflowInstanceCreate {
return wic.SetWorkflowID(w.ID)
}
// AddInstanceIDs adds the "instance" edge to the WorkflowEvents entity by IDs.
func (wic *WorkflowInstanceCreate) AddInstanceIDs(ids ...int) *WorkflowInstanceCreate {
wic.mutation.AddInstanceIDs(ids...)
return wic
}
// AddInstance adds the "instance" edges to the WorkflowEvents entity.
func (wic *WorkflowInstanceCreate) AddInstance(w ...*WorkflowEvents) *WorkflowInstanceCreate {
ids := make([]int, len(w))
for i := range w {
ids[i] = w[i].ID
}
return wic.AddInstanceIDs(ids...)
}
// Mutation returns the WorkflowInstanceMutation object of the builder.
func (wic *WorkflowInstanceCreate) Mutation() *WorkflowInstanceMutation {
return wic.mutation
}
// Save creates the WorkflowInstance in the database.
func (wic *WorkflowInstanceCreate) Save(ctx context.Context) (*WorkflowInstance, error) {
var (
err error
node *WorkflowInstance
)
if len(wic.hooks) == 0 {
if err = wic.check(); err != nil {
return nil, err
}
node, err = wic.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*WorkflowInstanceMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = wic.check(); err != nil {
return nil, err
}
wic.mutation = mutation
node, err = wic.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(wic.hooks) - 1; i >= 0; i-- {
mut = wic.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, wic.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (wic *WorkflowInstanceCreate) SaveX(ctx context.Context) *WorkflowInstance {
v, err := wic.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// check runs all checks and user-defined validators on the builder.
func (wic *WorkflowInstanceCreate) check() error {
if _, ok := wic.mutation.InstanceID(); !ok {
return &ValidationError{Name: "instanceID", err: errors.New("ent: missing required field \"instanceID\"")}
}
if _, ok := wic.mutation.InvokedBy(); !ok {
return &ValidationError{Name: "invokedBy", err: errors.New("ent: missing required field \"invokedBy\"")}
}
if _, ok := wic.mutation.Status(); !ok {
return &ValidationError{Name: "status", err: errors.New("ent: missing required field \"status\"")}
}
if _, ok := wic.mutation.Revision(); !ok {
return &ValidationError{Name: "revision", err: errors.New("ent: missing required field \"revision\"")}
}
if _, ok := wic.mutation.BeginTime(); !ok {
return &ValidationError{Name: "beginTime", err: errors.New("ent: missing required field \"beginTime\"")}
}
if _, ok := wic.mutation.Input(); !ok {
return &ValidationError{Name: "input", err: errors.New("ent: missing required field \"input\"")}
}
if _, ok := wic.mutation.WorkflowID(); !ok {
return &ValidationError{Name: "workflow", err: errors.New("ent: missing required edge \"workflow\"")}
}
return nil
}
func (wic *WorkflowInstanceCreate) sqlSave(ctx context.Context) (*WorkflowInstance, error) {
_node, _spec := wic.createSpec()
if err := sqlgraph.CreateNode(ctx, wic.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
return _node, nil
}
func (wic *WorkflowInstanceCreate) createSpec() (*WorkflowInstance, *sqlgraph.CreateSpec) {
var (
_node = &WorkflowInstance{config: wic.config}
_spec = &sqlgraph.CreateSpec{
Table: workflowinstance.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: workflowinstance.FieldID,
},
}
)
if value, ok := wic.mutation.InstanceID(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldInstanceID,
})
_node.InstanceID = value
}
if value, ok := wic.mutation.InvokedBy(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldInvokedBy,
})
_node.InvokedBy = value
}
if value, ok := wic.mutation.Status(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldStatus,
})
_node.Status = value
}
if value, ok := wic.mutation.Revision(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: workflowinstance.FieldRevision,
})
_node.Revision = value
}
if value, ok := wic.mutation.BeginTime(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: workflowinstance.FieldBeginTime,
})
_node.BeginTime = value
}
if value, ok := wic.mutation.EndTime(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: workflowinstance.FieldEndTime,
})
_node.EndTime = value
}
if value, ok := wic.mutation.Flow(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeJSON,
Value: value,
Column: workflowinstance.FieldFlow,
})
_node.Flow = value
}
if value, ok := wic.mutation.Input(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldInput,
})
_node.Input = value
}
if value, ok := wic.mutation.Output(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldOutput,
})
_node.Output = value
}
if value, ok := wic.mutation.StateData(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldStateData,
})
_node.StateData = value
}
if value, ok := wic.mutation.Memory(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldMemory,
})
_node.Memory = value
}
if value, ok := wic.mutation.Deadline(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: workflowinstance.FieldDeadline,
})
_node.Deadline = value
}
if value, ok := wic.mutation.Attempts(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: workflowinstance.FieldAttempts,
})
_node.Attempts = value
}
if value, ok := wic.mutation.ErrorCode(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldErrorCode,
})
_node.ErrorCode = value
}
if value, ok := wic.mutation.ErrorMessage(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldErrorMessage,
})
_node.ErrorMessage = value
}
if value, ok := wic.mutation.StateBeginTime(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: workflowinstance.FieldStateBeginTime,
})
_node.StateBeginTime = value
}
if value, ok := wic.mutation.Controller(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: workflowinstance.FieldController,
})
_node.Controller = value
}
if nodes := wic.mutation.WorkflowIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: workflowinstance.WorkflowTable,
Columns: []string{workflowinstance.WorkflowColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeUUID,
Column: workflow.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.workflow_instances = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := wic.mutation.InstanceIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: workflowinstance.InstanceTable,
Columns: []string{workflowinstance.InstanceColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: workflowevents.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// WorkflowInstanceCreateBulk is the builder for creating many WorkflowInstance entities in bulk.
type WorkflowInstanceCreateBulk struct {
config
builders []*WorkflowInstanceCreate
}
// Save creates the WorkflowInstance entities in the database.
func (wicb *WorkflowInstanceCreateBulk) Save(ctx context.Context) ([]*WorkflowInstance, error) {
specs := make([]*sqlgraph.CreateSpec, len(wicb.builders))
nodes := make([]*WorkflowInstance, len(wicb.builders))
mutators := make([]Mutator, len(wicb.builders))
for i := range wicb.builders {
func(i int, root context.Context) {
builder := wicb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*WorkflowInstanceMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
nodes[i], specs[i] = builder.createSpec()
var err error
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, wicb.builders[i+1].mutation)
} else {
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, wicb.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, wicb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (wicb *WorkflowInstanceCreateBulk) SaveX(ctx context.Context) []*WorkflowInstance {
v, err := wicb.Save(ctx)
if err != nil {
panic(err)
}
return v
}