/
group_create.go
276 lines (251 loc) · 7.46 KB
/
group_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
// Copyright (c) Facebook, Inc. and its affiliates. 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"
"time"
"github.com/facebookincubator/ent/dialect/gremlin"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/__"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/g"
"github.com/facebookincubator/ent/dialect/gremlin/graph/dsl/p"
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/group"
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/user"
)
// GroupCreate is the builder for creating a Group entity.
type GroupCreate struct {
config
mutation *GroupMutation
hooks []Hook
}
// SetActive sets the active field.
func (gc *GroupCreate) SetActive(b bool) *GroupCreate {
gc.mutation.SetActive(b)
return gc
}
// SetNillableActive sets the active field if the given value is not nil.
func (gc *GroupCreate) SetNillableActive(b *bool) *GroupCreate {
if b != nil {
gc.SetActive(*b)
}
return gc
}
// SetExpire sets the expire field.
func (gc *GroupCreate) SetExpire(t time.Time) *GroupCreate {
gc.mutation.SetExpire(t)
return gc
}
// SetType sets the type field.
func (gc *GroupCreate) SetType(s string) *GroupCreate {
gc.mutation.SetType(s)
return gc
}
// SetNillableType sets the type field if the given value is not nil.
func (gc *GroupCreate) SetNillableType(s *string) *GroupCreate {
if s != nil {
gc.SetType(*s)
}
return gc
}
// SetMaxUsers sets the max_users field.
func (gc *GroupCreate) SetMaxUsers(i int) *GroupCreate {
gc.mutation.SetMaxUsers(i)
return gc
}
// SetNillableMaxUsers sets the max_users field if the given value is not nil.
func (gc *GroupCreate) SetNillableMaxUsers(i *int) *GroupCreate {
if i != nil {
gc.SetMaxUsers(*i)
}
return gc
}
// SetName sets the name field.
func (gc *GroupCreate) SetName(s string) *GroupCreate {
gc.mutation.SetName(s)
return gc
}
// AddFileIDs adds the files edge to File by ids.
func (gc *GroupCreate) AddFileIDs(ids ...string) *GroupCreate {
gc.mutation.AddFileIDs(ids...)
return gc
}
// AddFiles adds the files edges to File.
func (gc *GroupCreate) AddFiles(f ...*File) *GroupCreate {
ids := make([]string, len(f))
for i := range f {
ids[i] = f[i].ID
}
return gc.AddFileIDs(ids...)
}
// AddBlockedIDs adds the blocked edge to User by ids.
func (gc *GroupCreate) AddBlockedIDs(ids ...string) *GroupCreate {
gc.mutation.AddBlockedIDs(ids...)
return gc
}
// AddBlocked adds the blocked edges to User.
func (gc *GroupCreate) AddBlocked(u ...*User) *GroupCreate {
ids := make([]string, len(u))
for i := range u {
ids[i] = u[i].ID
}
return gc.AddBlockedIDs(ids...)
}
// AddUserIDs adds the users edge to User by ids.
func (gc *GroupCreate) AddUserIDs(ids ...string) *GroupCreate {
gc.mutation.AddUserIDs(ids...)
return gc
}
// AddUsers adds the users edges to User.
func (gc *GroupCreate) AddUsers(u ...*User) *GroupCreate {
ids := make([]string, len(u))
for i := range u {
ids[i] = u[i].ID
}
return gc.AddUserIDs(ids...)
}
// SetInfoID sets the info edge to GroupInfo by id.
func (gc *GroupCreate) SetInfoID(id string) *GroupCreate {
gc.mutation.SetInfoID(id)
return gc
}
// SetInfo sets the info edge to GroupInfo.
func (gc *GroupCreate) SetInfo(g *GroupInfo) *GroupCreate {
return gc.SetInfoID(g.ID)
}
// Save creates the Group in the database.
func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
if _, ok := gc.mutation.Active(); !ok {
v := group.DefaultActive
gc.mutation.SetActive(v)
}
if _, ok := gc.mutation.Expire(); !ok {
return nil, errors.New("ent: missing required field \"expire\"")
}
if v, ok := gc.mutation.GetType(); ok {
if err := group.TypeValidator(v); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"type\": %v", err)
}
}
if _, ok := gc.mutation.MaxUsers(); !ok {
v := group.DefaultMaxUsers
gc.mutation.SetMaxUsers(v)
}
if v, ok := gc.mutation.MaxUsers(); ok {
if err := group.MaxUsersValidator(v); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"max_users\": %v", err)
}
}
if _, ok := gc.mutation.Name(); !ok {
return nil, errors.New("ent: missing required field \"name\"")
}
if v, ok := gc.mutation.Name(); ok {
if err := group.NameValidator(v); err != nil {
return nil, fmt.Errorf("ent: validator failed for field \"name\": %v", err)
}
}
if _, ok := gc.mutation.InfoID(); !ok {
return nil, errors.New("ent: missing required edge \"info\"")
}
var (
err error
node *Group
)
if len(gc.hooks) == 0 {
node, err = gc.gremlinSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*GroupMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
gc.mutation = mutation
node, err = gc.gremlinSave(ctx)
return node, err
})
for i := len(gc.hooks) - 1; i >= 0; i-- {
mut = gc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, gc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
v, err := gc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (gc *GroupCreate) gremlinSave(ctx context.Context) (*Group, error) {
res := &gremlin.Response{}
query, bindings := gc.gremlin().Query()
if err := gc.driver.Exec(ctx, query, bindings, res); err != nil {
return nil, err
}
if err, ok := isConstantError(res); ok {
return nil, err
}
gr := &Group{config: gc.config}
if err := gr.FromResponse(res); err != nil {
return nil, err
}
return gr, nil
}
func (gc *GroupCreate) gremlin() *dsl.Traversal {
type constraint struct {
pred *dsl.Traversal // constraint predicate.
test *dsl.Traversal // test matches and its constant.
}
constraints := make([]*constraint, 0, 2)
v := g.AddV(group.Label)
if value, ok := gc.mutation.Active(); ok {
v.Property(dsl.Single, group.FieldActive, value)
}
if value, ok := gc.mutation.Expire(); ok {
v.Property(dsl.Single, group.FieldExpire, value)
}
if value, ok := gc.mutation.GetType(); ok {
v.Property(dsl.Single, group.FieldType, value)
}
if value, ok := gc.mutation.MaxUsers(); ok {
v.Property(dsl.Single, group.FieldMaxUsers, value)
}
if value, ok := gc.mutation.Name(); ok {
v.Property(dsl.Single, group.FieldName, value)
}
for _, id := range gc.mutation.FilesIDs() {
v.AddE(group.FilesLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(group.FilesLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(group.Label, group.FilesLabel, id)),
})
}
for _, id := range gc.mutation.BlockedIDs() {
v.AddE(group.BlockedLabel).To(g.V(id)).OutV()
constraints = append(constraints, &constraint{
pred: g.E().HasLabel(group.BlockedLabel).InV().HasID(id).Count(),
test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(group.Label, group.BlockedLabel, id)),
})
}
for _, id := range gc.mutation.UsersIDs() {
v.AddE(user.GroupsLabel).From(g.V(id)).InV()
}
for _, id := range gc.mutation.InfoIDs() {
v.AddE(group.InfoLabel).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
}