/
role_update.go
786 lines (711 loc) · 23 KB
/
role_update.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// SPDX-FileCopyrightText: 2024 Seal, Inc
// SPDX-License-Identifier: Apache-2.0
// Code generated by "walrus". DO NOT EDIT.
package model
import (
"context"
stdsql "database/sql"
"errors"
"fmt"
"reflect"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/dialect/sql/sqljson"
"entgo.io/ent/schema/field"
"github.com/seal-io/walrus/pkg/dao/model/internal"
"github.com/seal-io/walrus/pkg/dao/model/predicate"
"github.com/seal-io/walrus/pkg/dao/model/role"
"github.com/seal-io/walrus/pkg/dao/model/subjectrolerelationship"
"github.com/seal-io/walrus/pkg/dao/types"
"github.com/seal-io/walrus/pkg/dao/types/object"
)
// RoleUpdate is the builder for updating Role entities.
type RoleUpdate struct {
config
hooks []Hook
mutation *RoleMutation
modifiers []func(*sql.UpdateBuilder)
object *Role
}
// Where appends a list predicates to the RoleUpdate builder.
func (ru *RoleUpdate) Where(ps ...predicate.Role) *RoleUpdate {
ru.mutation.Where(ps...)
return ru
}
// SetUpdateTime sets the "update_time" field.
func (ru *RoleUpdate) SetUpdateTime(t time.Time) *RoleUpdate {
ru.mutation.SetUpdateTime(t)
return ru
}
// SetDescription sets the "description" field.
func (ru *RoleUpdate) SetDescription(s string) *RoleUpdate {
ru.mutation.SetDescription(s)
return ru
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (ru *RoleUpdate) SetNillableDescription(s *string) *RoleUpdate {
if s != nil {
ru.SetDescription(*s)
}
return ru
}
// ClearDescription clears the value of the "description" field.
func (ru *RoleUpdate) ClearDescription() *RoleUpdate {
ru.mutation.ClearDescription()
return ru
}
// SetPolicies sets the "policies" field.
func (ru *RoleUpdate) SetPolicies(tp types.RolePolicies) *RoleUpdate {
ru.mutation.SetPolicies(tp)
return ru
}
// AppendPolicies appends tp to the "policies" field.
func (ru *RoleUpdate) AppendPolicies(tp types.RolePolicies) *RoleUpdate {
ru.mutation.AppendPolicies(tp)
return ru
}
// SetApplicableEnvironmentTypes sets the "applicable_environment_types" field.
func (ru *RoleUpdate) SetApplicableEnvironmentTypes(s []string) *RoleUpdate {
ru.mutation.SetApplicableEnvironmentTypes(s)
return ru
}
// AppendApplicableEnvironmentTypes appends s to the "applicable_environment_types" field.
func (ru *RoleUpdate) AppendApplicableEnvironmentTypes(s []string) *RoleUpdate {
ru.mutation.AppendApplicableEnvironmentTypes(s)
return ru
}
// ClearApplicableEnvironmentTypes clears the value of the "applicable_environment_types" field.
func (ru *RoleUpdate) ClearApplicableEnvironmentTypes() *RoleUpdate {
ru.mutation.ClearApplicableEnvironmentTypes()
return ru
}
// AddSubjectIDs adds the "subjects" edge to the SubjectRoleRelationship entity by IDs.
func (ru *RoleUpdate) AddSubjectIDs(ids ...object.ID) *RoleUpdate {
ru.mutation.AddSubjectIDs(ids...)
return ru
}
// AddSubjects adds the "subjects" edges to the SubjectRoleRelationship entity.
func (ru *RoleUpdate) AddSubjects(s ...*SubjectRoleRelationship) *RoleUpdate {
ids := make([]object.ID, len(s))
for i := range s {
ids[i] = s[i].ID
}
return ru.AddSubjectIDs(ids...)
}
// Mutation returns the RoleMutation object of the builder.
func (ru *RoleUpdate) Mutation() *RoleMutation {
return ru.mutation
}
// ClearSubjects clears all "subjects" edges to the SubjectRoleRelationship entity.
func (ru *RoleUpdate) ClearSubjects() *RoleUpdate {
ru.mutation.ClearSubjects()
return ru
}
// RemoveSubjectIDs removes the "subjects" edge to SubjectRoleRelationship entities by IDs.
func (ru *RoleUpdate) RemoveSubjectIDs(ids ...object.ID) *RoleUpdate {
ru.mutation.RemoveSubjectIDs(ids...)
return ru
}
// RemoveSubjects removes "subjects" edges to SubjectRoleRelationship entities.
func (ru *RoleUpdate) RemoveSubjects(s ...*SubjectRoleRelationship) *RoleUpdate {
ids := make([]object.ID, len(s))
for i := range s {
ids[i] = s[i].ID
}
return ru.RemoveSubjectIDs(ids...)
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (ru *RoleUpdate) Save(ctx context.Context) (int, error) {
if err := ru.defaults(); err != nil {
return 0, err
}
return withHooks(ctx, ru.sqlSave, ru.mutation, ru.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (ru *RoleUpdate) SaveX(ctx context.Context) int {
affected, err := ru.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (ru *RoleUpdate) Exec(ctx context.Context) error {
_, err := ru.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ru *RoleUpdate) ExecX(ctx context.Context) {
if err := ru.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (ru *RoleUpdate) defaults() error {
if _, ok := ru.mutation.UpdateTime(); !ok {
if role.UpdateDefaultUpdateTime == nil {
return fmt.Errorf("model: uninitialized role.UpdateDefaultUpdateTime (forgotten import model/runtime?)")
}
v := role.UpdateDefaultUpdateTime()
ru.mutation.SetUpdateTime(v)
}
return nil
}
// Set is different from other Set* methods,
// it sets the value by judging the definition of each field within the entire object.
//
// For default fields, Set calls if the value is not zero.
//
// For no default but required fields, Set calls directly.
//
// For no default but optional fields, Set calls if the value is not zero,
// or clears if the value is zero.
//
// For example:
//
// ## Without Default
//
// ### Required
//
// db.SetX(obj.X)
//
// ### Optional or Default
//
// if _is_zero_value_(obj.X) {
// db.SetX(obj.X)
// } else {
// db.ClearX()
// }
//
// ## With Default
//
// if _is_zero_value_(obj.X) {
// db.SetX(obj.X)
// }
func (ru *RoleUpdate) Set(obj *Role) *RoleUpdate {
// Without Default.
if obj.Description != "" {
ru.SetDescription(obj.Description)
} else {
ru.ClearDescription()
}
ru.SetPolicies(obj.Policies)
if !reflect.ValueOf(obj.ApplicableEnvironmentTypes).IsZero() {
ru.SetApplicableEnvironmentTypes(obj.ApplicableEnvironmentTypes)
} else {
ru.ClearApplicableEnvironmentTypes()
}
// With Default.
if obj.UpdateTime != nil {
ru.SetUpdateTime(*obj.UpdateTime)
}
// Record the given object.
ru.object = obj
return ru
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (ru *RoleUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *RoleUpdate {
ru.modifiers = append(ru.modifiers, modifiers...)
return ru
}
func (ru *RoleUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := sqlgraph.NewUpdateSpec(role.Table, role.Columns, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString))
if ps := ru.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := ru.mutation.UpdateTime(); ok {
_spec.SetField(role.FieldUpdateTime, field.TypeTime, value)
}
if value, ok := ru.mutation.Description(); ok {
_spec.SetField(role.FieldDescription, field.TypeString, value)
}
if ru.mutation.DescriptionCleared() {
_spec.ClearField(role.FieldDescription, field.TypeString)
}
if value, ok := ru.mutation.Policies(); ok {
_spec.SetField(role.FieldPolicies, field.TypeJSON, value)
}
if value, ok := ru.mutation.AppendedPolicies(); ok {
_spec.AddModifier(func(u *sql.UpdateBuilder) {
sqljson.Append(u, role.FieldPolicies, value)
})
}
if value, ok := ru.mutation.ApplicableEnvironmentTypes(); ok {
_spec.SetField(role.FieldApplicableEnvironmentTypes, field.TypeJSON, value)
}
if value, ok := ru.mutation.AppendedApplicableEnvironmentTypes(); ok {
_spec.AddModifier(func(u *sql.UpdateBuilder) {
sqljson.Append(u, role.FieldApplicableEnvironmentTypes, value)
})
}
if ru.mutation.ApplicableEnvironmentTypesCleared() {
_spec.ClearField(role.FieldApplicableEnvironmentTypes, field.TypeJSON)
}
if ru.mutation.SubjectsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: role.SubjectsTable,
Columns: []string{role.SubjectsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(subjectrolerelationship.FieldID, field.TypeString),
},
}
edge.Schema = ru.schemaConfig.SubjectRoleRelationship
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := ru.mutation.RemovedSubjectsIDs(); len(nodes) > 0 && !ru.mutation.SubjectsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: role.SubjectsTable,
Columns: []string{role.SubjectsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(subjectrolerelationship.FieldID, field.TypeString),
},
}
edge.Schema = ru.schemaConfig.SubjectRoleRelationship
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := ru.mutation.SubjectsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: role.SubjectsTable,
Columns: []string{role.SubjectsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(subjectrolerelationship.FieldID, field.TypeString),
},
}
edge.Schema = ru.schemaConfig.SubjectRoleRelationship
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.Node.Schema = ru.schemaConfig.Role
ctx = internal.NewSchemaConfigContext(ctx, ru.schemaConfig)
_spec.AddModifiers(ru.modifiers...)
if n, err = sqlgraph.UpdateNodes(ctx, ru.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{role.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
ru.mutation.done = true
return n, nil
}
// RoleUpdateOne is the builder for updating a single Role entity.
type RoleUpdateOne struct {
config
fields []string
hooks []Hook
mutation *RoleMutation
modifiers []func(*sql.UpdateBuilder)
object *Role
}
// SetUpdateTime sets the "update_time" field.
func (ruo *RoleUpdateOne) SetUpdateTime(t time.Time) *RoleUpdateOne {
ruo.mutation.SetUpdateTime(t)
return ruo
}
// SetDescription sets the "description" field.
func (ruo *RoleUpdateOne) SetDescription(s string) *RoleUpdateOne {
ruo.mutation.SetDescription(s)
return ruo
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (ruo *RoleUpdateOne) SetNillableDescription(s *string) *RoleUpdateOne {
if s != nil {
ruo.SetDescription(*s)
}
return ruo
}
// ClearDescription clears the value of the "description" field.
func (ruo *RoleUpdateOne) ClearDescription() *RoleUpdateOne {
ruo.mutation.ClearDescription()
return ruo
}
// SetPolicies sets the "policies" field.
func (ruo *RoleUpdateOne) SetPolicies(tp types.RolePolicies) *RoleUpdateOne {
ruo.mutation.SetPolicies(tp)
return ruo
}
// AppendPolicies appends tp to the "policies" field.
func (ruo *RoleUpdateOne) AppendPolicies(tp types.RolePolicies) *RoleUpdateOne {
ruo.mutation.AppendPolicies(tp)
return ruo
}
// SetApplicableEnvironmentTypes sets the "applicable_environment_types" field.
func (ruo *RoleUpdateOne) SetApplicableEnvironmentTypes(s []string) *RoleUpdateOne {
ruo.mutation.SetApplicableEnvironmentTypes(s)
return ruo
}
// AppendApplicableEnvironmentTypes appends s to the "applicable_environment_types" field.
func (ruo *RoleUpdateOne) AppendApplicableEnvironmentTypes(s []string) *RoleUpdateOne {
ruo.mutation.AppendApplicableEnvironmentTypes(s)
return ruo
}
// ClearApplicableEnvironmentTypes clears the value of the "applicable_environment_types" field.
func (ruo *RoleUpdateOne) ClearApplicableEnvironmentTypes() *RoleUpdateOne {
ruo.mutation.ClearApplicableEnvironmentTypes()
return ruo
}
// AddSubjectIDs adds the "subjects" edge to the SubjectRoleRelationship entity by IDs.
func (ruo *RoleUpdateOne) AddSubjectIDs(ids ...object.ID) *RoleUpdateOne {
ruo.mutation.AddSubjectIDs(ids...)
return ruo
}
// AddSubjects adds the "subjects" edges to the SubjectRoleRelationship entity.
func (ruo *RoleUpdateOne) AddSubjects(s ...*SubjectRoleRelationship) *RoleUpdateOne {
ids := make([]object.ID, len(s))
for i := range s {
ids[i] = s[i].ID
}
return ruo.AddSubjectIDs(ids...)
}
// Mutation returns the RoleMutation object of the builder.
func (ruo *RoleUpdateOne) Mutation() *RoleMutation {
return ruo.mutation
}
// ClearSubjects clears all "subjects" edges to the SubjectRoleRelationship entity.
func (ruo *RoleUpdateOne) ClearSubjects() *RoleUpdateOne {
ruo.mutation.ClearSubjects()
return ruo
}
// RemoveSubjectIDs removes the "subjects" edge to SubjectRoleRelationship entities by IDs.
func (ruo *RoleUpdateOne) RemoveSubjectIDs(ids ...object.ID) *RoleUpdateOne {
ruo.mutation.RemoveSubjectIDs(ids...)
return ruo
}
// RemoveSubjects removes "subjects" edges to SubjectRoleRelationship entities.
func (ruo *RoleUpdateOne) RemoveSubjects(s ...*SubjectRoleRelationship) *RoleUpdateOne {
ids := make([]object.ID, len(s))
for i := range s {
ids[i] = s[i].ID
}
return ruo.RemoveSubjectIDs(ids...)
}
// Where appends a list predicates to the RoleUpdate builder.
func (ruo *RoleUpdateOne) Where(ps ...predicate.Role) *RoleUpdateOne {
ruo.mutation.Where(ps...)
return ruo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (ruo *RoleUpdateOne) Select(field string, fields ...string) *RoleUpdateOne {
ruo.fields = append([]string{field}, fields...)
return ruo
}
// Save executes the query and returns the updated Role entity.
func (ruo *RoleUpdateOne) Save(ctx context.Context) (*Role, error) {
if err := ruo.defaults(); err != nil {
return nil, err
}
return withHooks(ctx, ruo.sqlSave, ruo.mutation, ruo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (ruo *RoleUpdateOne) SaveX(ctx context.Context) *Role {
node, err := ruo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (ruo *RoleUpdateOne) Exec(ctx context.Context) error {
_, err := ruo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ruo *RoleUpdateOne) ExecX(ctx context.Context) {
if err := ruo.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (ruo *RoleUpdateOne) defaults() error {
if _, ok := ruo.mutation.UpdateTime(); !ok {
if role.UpdateDefaultUpdateTime == nil {
return fmt.Errorf("model: uninitialized role.UpdateDefaultUpdateTime (forgotten import model/runtime?)")
}
v := role.UpdateDefaultUpdateTime()
ruo.mutation.SetUpdateTime(v)
}
return nil
}
// Set is different from other Set* methods,
// it sets the value by judging the definition of each field within the entire object.
//
// For default fields, Set calls if the value changes from the original.
//
// For no default but required fields, Set calls if the value changes from the original.
//
// For no default but optional fields, Set calls if the value changes from the original,
// or clears if changes to zero.
//
// For example:
//
// ## Without Default
//
// ### Required
//
// db.SetX(obj.X)
//
// ### Optional or Default
//
// if _is_zero_value_(obj.X) {
// if _is_not_equal_(db.X, obj.X) {
// db.SetX(obj.X)
// }
// } else {
// db.ClearX()
// }
//
// ## With Default
//
// if _is_zero_value_(obj.X) && _is_not_equal_(db.X, obj.X) {
// db.SetX(obj.X)
// }
func (ruo *RoleUpdateOne) Set(obj *Role) *RoleUpdateOne {
h := func(n ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
mt := m.(*RoleMutation)
db, err := mt.Client().Role.Get(ctx, *mt.id)
if err != nil {
return nil, fmt.Errorf("failed getting Role with id: %v", *mt.id)
}
// Without Default.
if obj.Description != "" {
if db.Description != obj.Description {
ruo.SetDescription(obj.Description)
}
} else {
ruo.ClearDescription()
}
if !reflect.DeepEqual(db.Policies, obj.Policies) {
ruo.SetPolicies(obj.Policies)
}
if !reflect.ValueOf(obj.ApplicableEnvironmentTypes).IsZero() {
if !reflect.DeepEqual(db.ApplicableEnvironmentTypes, obj.ApplicableEnvironmentTypes) {
ruo.SetApplicableEnvironmentTypes(obj.ApplicableEnvironmentTypes)
}
} else {
ruo.ClearApplicableEnvironmentTypes()
}
// With Default.
if (obj.UpdateTime != nil) && (!reflect.DeepEqual(db.UpdateTime, obj.UpdateTime)) {
ruo.SetUpdateTime(*obj.UpdateTime)
}
// Record the given object.
ruo.object = obj
return n.Mutate(ctx, m)
})
}
ruo.hooks = append(ruo.hooks, h)
return ruo
}
// getClientSet returns the ClientSet for the given builder.
func (ruo *RoleUpdateOne) getClientSet() (mc ClientSet) {
if _, ok := ruo.config.driver.(*txDriver); ok {
tx := &Tx{config: ruo.config}
tx.init()
mc = tx
} else {
cli := &Client{config: ruo.config}
cli.init()
mc = cli
}
return mc
}
// SaveE calls the given function after updated the Role entity,
// which is always good for cascading update operations.
func (ruo *RoleUpdateOne) SaveE(ctx context.Context, cbs ...func(ctx context.Context, mc ClientSet, updated *Role) error) (*Role, error) {
obj, err := ruo.Save(ctx)
if err != nil &&
(ruo.object == nil || !errors.Is(err, stdsql.ErrNoRows)) {
return nil, err
}
if len(cbs) == 0 {
return obj, err
}
mc := ruo.getClientSet()
if obj == nil {
obj = ruo.object
} else if x := ruo.object; x != nil {
if _, set := ruo.mutation.Field(role.FieldDescription); set {
obj.Description = x.Description
}
if _, set := ruo.mutation.Field(role.FieldPolicies); set {
obj.Policies = x.Policies
}
if _, set := ruo.mutation.Field(role.FieldApplicableEnvironmentTypes); set {
obj.ApplicableEnvironmentTypes = x.ApplicableEnvironmentTypes
}
obj.Edges = x.Edges
}
for i := range cbs {
if err = cbs[i](ctx, mc, obj); err != nil {
return nil, err
}
}
return obj, nil
}
// SaveEX is like SaveE, but panics if an error occurs.
func (ruo *RoleUpdateOne) SaveEX(ctx context.Context, cbs ...func(ctx context.Context, mc ClientSet, updated *Role) error) *Role {
obj, err := ruo.SaveE(ctx, cbs...)
if err != nil {
panic(err)
}
return obj
}
// ExecE calls the given function after executed the query,
// which is always good for cascading update operations.
func (ruo *RoleUpdateOne) ExecE(ctx context.Context, cbs ...func(ctx context.Context, mc ClientSet, updated *Role) error) error {
_, err := ruo.SaveE(ctx, cbs...)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ruo *RoleUpdateOne) ExecEX(ctx context.Context, cbs ...func(ctx context.Context, mc ClientSet, updated *Role) error) {
if err := ruo.ExecE(ctx, cbs...); err != nil {
panic(err)
}
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (ruo *RoleUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *RoleUpdateOne {
ruo.modifiers = append(ruo.modifiers, modifiers...)
return ruo
}
func (ruo *RoleUpdateOne) sqlSave(ctx context.Context) (_node *Role, err error) {
_spec := sqlgraph.NewUpdateSpec(role.Table, role.Columns, sqlgraph.NewFieldSpec(role.FieldID, field.TypeString))
id, ok := ruo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`model: missing "Role.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := ruo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, role.FieldID)
for _, f := range fields {
if !role.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("model: invalid field %q for query", f)}
}
if f != role.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := ruo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := ruo.mutation.UpdateTime(); ok {
_spec.SetField(role.FieldUpdateTime, field.TypeTime, value)
}
if value, ok := ruo.mutation.Description(); ok {
_spec.SetField(role.FieldDescription, field.TypeString, value)
}
if ruo.mutation.DescriptionCleared() {
_spec.ClearField(role.FieldDescription, field.TypeString)
}
if value, ok := ruo.mutation.Policies(); ok {
_spec.SetField(role.FieldPolicies, field.TypeJSON, value)
}
if value, ok := ruo.mutation.AppendedPolicies(); ok {
_spec.AddModifier(func(u *sql.UpdateBuilder) {
sqljson.Append(u, role.FieldPolicies, value)
})
}
if value, ok := ruo.mutation.ApplicableEnvironmentTypes(); ok {
_spec.SetField(role.FieldApplicableEnvironmentTypes, field.TypeJSON, value)
}
if value, ok := ruo.mutation.AppendedApplicableEnvironmentTypes(); ok {
_spec.AddModifier(func(u *sql.UpdateBuilder) {
sqljson.Append(u, role.FieldApplicableEnvironmentTypes, value)
})
}
if ruo.mutation.ApplicableEnvironmentTypesCleared() {
_spec.ClearField(role.FieldApplicableEnvironmentTypes, field.TypeJSON)
}
if ruo.mutation.SubjectsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: role.SubjectsTable,
Columns: []string{role.SubjectsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(subjectrolerelationship.FieldID, field.TypeString),
},
}
edge.Schema = ruo.schemaConfig.SubjectRoleRelationship
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := ruo.mutation.RemovedSubjectsIDs(); len(nodes) > 0 && !ruo.mutation.SubjectsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: role.SubjectsTable,
Columns: []string{role.SubjectsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(subjectrolerelationship.FieldID, field.TypeString),
},
}
edge.Schema = ruo.schemaConfig.SubjectRoleRelationship
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := ruo.mutation.SubjectsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: role.SubjectsTable,
Columns: []string{role.SubjectsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(subjectrolerelationship.FieldID, field.TypeString),
},
}
edge.Schema = ruo.schemaConfig.SubjectRoleRelationship
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.Node.Schema = ruo.schemaConfig.Role
ctx = internal.NewSchemaConfigContext(ctx, ruo.schemaConfig)
_spec.AddModifiers(ruo.modifiers...)
_node = &Role{config: ruo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, ruo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{role.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
ruo.mutation.done = true
return _node, nil
}