/
user_create.go
538 lines (483 loc) · 14.7 KB
/
user_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
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"github.com/facebook/ent/dialect/gremlin"
"github.com/facebook/ent/dialect/gremlin/graph/dsl"
"github.com/facebook/ent/dialect/gremlin/graph/dsl/__"
"github.com/facebook/ent/dialect/gremlin/graph/dsl/g"
"github.com/facebook/ent/dialect/gremlin/graph/dsl/p"
"github.com/facebook/ent/entc/integration/gremlin/ent/user"
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
// SetOptionalInt sets the optional_int field.
func (uc *UserCreate) SetOptionalInt(i int) *UserCreate {
uc.mutation.SetOptionalInt(i)
return uc
}
// SetNillableOptionalInt sets the optional_int field if the given value is not nil.
func (uc *UserCreate) SetNillableOptionalInt(i *int) *UserCreate {
if i != nil {
uc.SetOptionalInt(*i)
}
return uc
}
// SetAge sets the age field.
func (uc *UserCreate) SetAge(i int) *UserCreate {
uc.mutation.SetAge(i)
return uc
}
// SetName sets the name field.
func (uc *UserCreate) SetName(s string) *UserCreate {
uc.mutation.SetName(s)
return uc
}
// SetLast sets the last field.
func (uc *UserCreate) SetLast(s string) *UserCreate {
uc.mutation.SetLast(s)
return uc
}
// SetNillableLast sets the last field if the given value is not nil.
func (uc *UserCreate) SetNillableLast(s *string) *UserCreate {
if s != nil {
uc.SetLast(*s)
}
return uc
}
// SetNickname sets the nickname field.
func (uc *UserCreate) SetNickname(s string) *UserCreate {
uc.mutation.SetNickname(s)
return uc
}
// SetNillableNickname sets the nickname field if the given value is not nil.
func (uc *UserCreate) SetNillableNickname(s *string) *UserCreate {
if s != nil {
uc.SetNickname(*s)
}
return uc
}
// SetPhone sets the phone field.
func (uc *UserCreate) SetPhone(s string) *UserCreate {
uc.mutation.SetPhone(s)
return uc
}
// SetNillablePhone sets the phone field if the given value is not nil.
func (uc *UserCreate) SetNillablePhone(s *string) *UserCreate {
if s != nil {
uc.SetPhone(*s)
}
return uc
}
// SetPassword sets the password field.
func (uc *UserCreate) SetPassword(s string) *UserCreate {
uc.mutation.SetPassword(s)
return uc
}
// SetNillablePassword sets the password field if the given value is not nil.
func (uc *UserCreate) SetNillablePassword(s *string) *UserCreate {
if s != nil {
uc.SetPassword(*s)
}
return uc
}
// SetRole sets the role field.
func (uc *UserCreate) SetRole(u user.Role) *UserCreate {
uc.mutation.SetRole(u)
return uc
}
// SetNillableRole sets the role field if the given value is not nil.
func (uc *UserCreate) SetNillableRole(u *user.Role) *UserCreate {
if u != nil {
uc.SetRole(*u)
}
return uc
}
// SetSSOCert sets the SSOCert field.
func (uc *UserCreate) SetSSOCert(s string) *UserCreate {
uc.mutation.SetSSOCert(s)
return uc
}
// SetNillableSSOCert sets the SSOCert field if the given value is not nil.
func (uc *UserCreate) SetNillableSSOCert(s *string) *UserCreate {
if s != nil {
uc.SetSSOCert(*s)
}
return uc
}
// SetCardID sets the card edge to Card by id.
func (uc *UserCreate) SetCardID(id string) *UserCreate {
uc.mutation.SetCardID(id)
return uc
}
// SetNillableCardID sets the card edge to Card by id if the given value is not nil.
func (uc *UserCreate) SetNillableCardID(id *string) *UserCreate {
if id != nil {
uc = uc.SetCardID(*id)
}
return uc
}
// SetCard sets the card edge to Card.
func (uc *UserCreate) SetCard(c *Card) *UserCreate {
return uc.SetCardID(c.ID)
}
// AddPetIDs adds the pets edge to Pet by ids.
func (uc *UserCreate) AddPetIDs(ids ...string) *UserCreate {
uc.mutation.AddPetIDs(ids...)
return uc
}
// AddPets adds the pets edges to Pet.
func (uc *UserCreate) AddPets(p ...*Pet) *UserCreate {
ids := make([]string, len(p))
for i := range p {
ids[i] = p[i].ID
}
return uc.AddPetIDs(ids...)
}
// AddFileIDs adds the files edge to File by ids.
func (uc *UserCreate) AddFileIDs(ids ...string) *UserCreate {
uc.mutation.AddFileIDs(ids...)
return uc
}
// AddFiles adds the files edges to File.
func (uc *UserCreate) AddFiles(f ...*File) *UserCreate {
ids := make([]string, len(f))
for i := range f {
ids[i] = f[i].ID
}
return uc.AddFileIDs(ids...)
}
// AddGroupIDs adds the groups edge to Group by ids.
func (uc *UserCreate) AddGroupIDs(ids ...string) *UserCreate {
uc.mutation.AddGroupIDs(ids...)
return uc
}
// AddGroups adds the groups edges to Group.
func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate {
ids := make([]string, len(g))
for i := range g {
ids[i] = g[i].ID
}
return uc.AddGroupIDs(ids...)
}
// AddFriendIDs adds the friends edge to User by ids.
func (uc *UserCreate) AddFriendIDs(ids ...string) *UserCreate {
uc.mutation.AddFriendIDs(ids...)
return uc
}
// AddFriends adds the friends edges to User.
func (uc *UserCreate) AddFriends(u ...*User) *UserCreate {
ids := make([]string, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFriendIDs(ids...)
}
// AddFollowerIDs adds the followers edge to User by ids.
func (uc *UserCreate) AddFollowerIDs(ids ...string) *UserCreate {
uc.mutation.AddFollowerIDs(ids...)
return uc
}
// AddFollowers adds the followers edges to User.
func (uc *UserCreate) AddFollowers(u ...*User) *UserCreate {
ids := make([]string, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFollowerIDs(ids...)
}
// AddFollowingIDs adds the following edge to User by ids.
func (uc *UserCreate) AddFollowingIDs(ids ...string) *UserCreate {
uc.mutation.AddFollowingIDs(ids...)
return uc
}
// AddFollowing adds the following edges to User.
func (uc *UserCreate) AddFollowing(u ...*User) *UserCreate {
ids := make([]string, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFollowingIDs(ids...)
}
// SetTeamID sets the team edge to Pet by id.
func (uc *UserCreate) SetTeamID(id string) *UserCreate {
uc.mutation.SetTeamID(id)
return uc
}
// SetNillableTeamID sets the team edge to Pet by id if the given value is not nil.
func (uc *UserCreate) SetNillableTeamID(id *string) *UserCreate {
if id != nil {
uc = uc.SetTeamID(*id)
}
return uc
}
// SetTeam sets the team edge to Pet.
func (uc *UserCreate) SetTeam(p *Pet) *UserCreate {
return uc.SetTeamID(p.ID)
}
// SetSpouseID sets the spouse edge to User by id.
func (uc *UserCreate) SetSpouseID(id string) *UserCreate {
uc.mutation.SetSpouseID(id)
return uc
}
// SetNillableSpouseID sets the spouse edge to User by id if the given value is not nil.
func (uc *UserCreate) SetNillableSpouseID(id *string) *UserCreate {
if id != nil {
uc = uc.SetSpouseID(*id)
}
return uc
}
// SetSpouse sets the spouse edge to User.
func (uc *UserCreate) SetSpouse(u *User) *UserCreate {
return uc.SetSpouseID(u.ID)
}
// AddChildIDs adds the children edge to User by ids.
func (uc *UserCreate) AddChildIDs(ids ...string) *UserCreate {
uc.mutation.AddChildIDs(ids...)
return uc
}
// AddChildren adds the children edges to User.
func (uc *UserCreate) AddChildren(u ...*User) *UserCreate {
ids := make([]string, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddChildIDs(ids...)
}
// SetParentID sets the parent edge to User by id.
func (uc *UserCreate) SetParentID(id string) *UserCreate {
uc.mutation.SetParentID(id)
return uc
}
// SetNillableParentID sets the parent edge to User by id if the given value is not nil.
func (uc *UserCreate) SetNillableParentID(id *string) *UserCreate {
if id != nil {
uc = uc.SetParentID(*id)
}
return uc
}
// SetParent sets the parent edge to User.
func (uc *UserCreate) SetParent(u *User) *UserCreate {
return uc.SetParentID(u.ID)
}
// Mutation returns the UserMutation object of the builder.
func (uc *UserCreate) Mutation() *UserMutation {
return uc.mutation
}
// Save creates the User in the database.
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
var (
err error
node *User
)
uc.defaults()
if len(uc.hooks) == 0 {
if err = uc.check(); err != nil {
return nil, err
}
node, err = uc.gremlinSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*UserMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = uc.check(); err != nil {
return nil, err
}
uc.mutation = mutation
node, err = uc.gremlinSave(ctx)
mutation.done = true
return node, err
})
for i := len(uc.hooks) - 1; i >= 0; i-- {
mut = uc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, uc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (uc *UserCreate) SaveX(ctx context.Context) *User {
v, err := uc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// defaults sets the default values of the builder before save.
func (uc *UserCreate) defaults() {
if _, ok := uc.mutation.Last(); !ok {
v := user.DefaultLast
uc.mutation.SetLast(v)
}
if _, ok := uc.mutation.Role(); !ok {
v := user.DefaultRole
uc.mutation.SetRole(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (uc *UserCreate) check() error {
if v, ok := uc.mutation.OptionalInt(); ok {
if err := user.OptionalIntValidator(v); err != nil {
return &ValidationError{Name: "optional_int", err: fmt.Errorf("ent: validator failed for field \"optional_int\": %w", err)}
}
}
if _, ok := uc.mutation.Age(); !ok {
return &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
}
if _, ok := uc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
}
if _, ok := uc.mutation.Last(); !ok {
return &ValidationError{Name: "last", err: errors.New("ent: missing required field \"last\"")}
}
if _, ok := uc.mutation.Role(); !ok {
return &ValidationError{Name: "role", err: errors.New("ent: missing required field \"role\"")}
}
if v, ok := uc.mutation.Role(); ok {
if err := user.RoleValidator(v); err != nil {
return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)}
}
}
return nil
}
func (uc *UserCreate) gremlinSave(ctx context.Context) (*User, error) {
res := &gremlin.Response{}
query, bindings := uc.gremlin().Query()
if err := uc.driver.Exec(ctx, query, bindings, res); err != nil {
return nil, err
}
if err, ok := isConstantError(res); ok {
return nil, err
}
u := &User{config: uc.config}
if err := u.FromResponse(res); err != nil {
return nil, err
}
return u, nil
}
func (uc *UserCreate) gremlin() *dsl.Traversal {
type constraint struct {
pred *dsl.Traversal // constraint predicate.
test *dsl.Traversal // test matches and its constant.
}
constraints := make([]*constraint, 0, 8)
v := g.AddV(user.Label)
if value, ok := uc.mutation.OptionalInt(); ok {
v.Property(dsl.Single, user.FieldOptionalInt, value)
}
if value, ok := uc.mutation.Age(); ok {
v.Property(dsl.Single, user.FieldAge, value)
}
if value, ok := uc.mutation.Name(); ok {
v.Property(dsl.Single, user.FieldName, value)
}
if value, ok := uc.mutation.Last(); ok {
v.Property(dsl.Single, user.FieldLast, value)
}
if value, ok := uc.mutation.Nickname(); ok {
constraints = append(constraints, &constraint{
pred: g.V().Has(user.Label, user.FieldNickname, value).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(user.Label, user.FieldNickname, value)),
})
v.Property(dsl.Single, user.FieldNickname, value)
}
if value, ok := uc.mutation.Phone(); ok {
constraints = append(constraints, &constraint{
pred: g.V().Has(user.Label, user.FieldPhone, value).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueField(user.Label, user.FieldPhone, value)),
})
v.Property(dsl.Single, user.FieldPhone, value)
}
if value, ok := uc.mutation.Password(); ok {
v.Property(dsl.Single, user.FieldPassword, value)
}
if value, ok := uc.mutation.Role(); ok {
v.Property(dsl.Single, user.FieldRole, value)
}
if value, ok := uc.mutation.SSOCert(); ok {
v.Property(dsl.Single, user.FieldSSOCert, value)
}
for _, id := range uc.mutation.CardIDs() {
v.AddE(user.CardLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.CardLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(user.Label, user.CardLabel, id)),
})
}
for _, id := range uc.mutation.PetsIDs() {
v.AddE(user.PetsLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.PetsLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(user.Label, user.PetsLabel, id)),
})
}
for _, id := range uc.mutation.FilesIDs() {
v.AddE(user.FilesLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.FilesLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(user.Label, user.FilesLabel, id)),
})
}
for _, id := range uc.mutation.GroupsIDs() {
v.AddE(user.GroupsLabel).To(g.V(id)).OutV()
}
for _, id := range uc.mutation.FriendsIDs() {
v.AddE(user.FriendsLabel).To(g.V(id)).OutV()
}
for _, id := range uc.mutation.FollowersIDs() {
v.AddE(user.FollowingLabel).From(g.V(id)).InV()
}
for _, id := range uc.mutation.FollowingIDs() {
v.AddE(user.FollowingLabel).To(g.V(id)).OutV()
}
for _, id := range uc.mutation.TeamIDs() {
v.AddE(user.TeamLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.TeamLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(user.Label, user.TeamLabel, id)),
})
}
for _, id := range uc.mutation.SpouseIDs() {
v.AddE(user.SpouseLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.SpouseLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(user.Label, user.SpouseLabel, id)),
})
}
for _, id := range uc.mutation.ChildrenIDs() {
v.AddE(user.ParentLabel).From(g.V(id)).InV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(user.ParentLabel).OutV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(user.Label, user.ParentLabel, id)),
})
}
for _, id := range uc.mutation.ParentIDs() {
v.AddE(user.ParentLabel).To(g.V(id)).OutV()
}
if len(constraints) == 0 {
return v.ValueMap(true)
}
tr := constraints[0].pred.Coalesce(constraints[0].test, v.ValueMap(true))
for _, cr := range constraints[1:] {
tr = cr.pred.Coalesce(cr.test, tr)
}
return tr
}
// UserCreateBulk is the builder for creating a bulk of User entities.
type UserCreateBulk struct {
config
builders []*UserCreate
}