Debian Codegraph @

ent/attestationcollection_create.go

raw
1 // Code generated by ent, DO NOT EDIT.
2
3 package ent
4
5 import (
6 "context"
7 "errors"
8 "fmt"
9
10 "entgo.io/ent/dialect/sql/sqlgraph"
11 "entgo.io/ent/schema/field"
12 "github.com/google/uuid"
13 "github.com/in-toto/archivista/ent/attestation"
14 "github.com/in-toto/archivista/ent/attestationcollection"
15 "github.com/in-toto/archivista/ent/statement"
16 )
17
18 // AttestationCollectionCreate is the builder for creating a AttestationCollection entity.
19 type AttestationCollectionCreate struct {
20 config
21 mutation *AttestationCollectionMutation
22 hooks []Hook
23 }
24
25 // SetName sets the "name" field.
26 func (_c *AttestationCollectionCreate) SetName(v string) *AttestationCollectionCreate {
27 _c.mutation.SetName(v)
28 return _c
29 }
30
31 // SetID sets the "id" field.
32 func (_c *AttestationCollectionCreate) SetID(v uuid.UUID) *AttestationCollectionCreate {
33 _c.mutation.SetID(v)
34 return _c
35 }
36
37 // SetNillableID sets the "id" field if the given value is not nil.
38 func (_c *AttestationCollectionCreate) SetNillableID(v *uuid.UUID) *AttestationCollectionCreate {
39 if v != nil {
40 _c.SetID(*v)
41 }
42 return _c
43 }
44
45 // AddAttestationIDs adds the "attestations" edge to the Attestation entity by IDs.
46 func (_c *AttestationCollectionCreate) AddAttestationIDs(ids ...uuid.UUID) *AttestationCollectionCreate {
47 _c.mutation.AddAttestationIDs(ids...)
48 return _c
49 }
50
51 // AddAttestations adds the "attestations" edges to the Attestation entity.
52 func (_c *AttestationCollectionCreate) AddAttestations(v ...*Attestation) *AttestationCollectionCreate {
53 ids := make([]uuid.UUID, len(v))
54 for i := range v {
55 ids[i] = v[i].ID
56 }
57 return _c.AddAttestationIDs(ids...)
58 }
59
60 // SetStatementID sets the "statement" edge to the Statement entity by ID.
61 func (_c *AttestationCollectionCreate) SetStatementID(id uuid.UUID) *AttestationCollectionCreate {
62 _c.mutation.SetStatementID(id)
63 return _c
64 }
65
66 // SetStatement sets the "statement" edge to the Statement entity.
67 func (_c *AttestationCollectionCreate) SetStatement(v *Statement) *AttestationCollectionCreate {
68 return _c.SetStatementID(v.ID)
69 }
70
71 // Mutation returns the AttestationCollectionMutation object of the builder.
72 func (_c *AttestationCollectionCreate) Mutation() *AttestationCollectionMutation {
73 return _c.mutation
74 }
75
76 // Save creates the AttestationCollection in the database.
77 func (_c *AttestationCollectionCreate) Save(ctx context.Context) (*AttestationCollection, error) {
78 _c.defaults()
79 return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
80 }
81
82 // SaveX calls Save and panics if Save returns an error.
83 func (_c *AttestationCollectionCreate) SaveX(ctx context.Context) *AttestationCollection {
84 v, err := _c.Save(ctx)
85 if err != nil {
86 panic(err)
87 }
88 return v
89 }
90
91 // Exec executes the query.
92 func (_c *AttestationCollectionCreate) Exec(ctx context.Context) error {
93 _, err := _c.Save(ctx)
94 return err
95 }
96
97 // ExecX is like Exec, but panics if an error occurs.
98 func (_c *AttestationCollectionCreate) ExecX(ctx context.Context) {
99 if err := _c.Exec(ctx); err != nil {
100 panic(err)
101 }
102 }
103
104 // defaults sets the default values of the builder before save.
105 func (_c *AttestationCollectionCreate) defaults() {
106 if _, ok := _c.mutation.ID(); !ok {
107 v := attestationcollection.DefaultID()
108 _c.mutation.SetID(v)
109 }
110 }
111
112 // check runs all checks and user-defined validators on the builder.
113 func (_c *AttestationCollectionCreate) check() error {
114 if _, ok := _c.mutation.Name(); !ok {
115 return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "AttestationCollection.name"`)}
116 }
117 if v, ok := _c.mutation.Name(); ok {
118 if err := attestationcollection.NameValidator(v); err != nil {
119 return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "AttestationCollection.name": %w`, err)}
120 }
121 }
122 if len(_c.mutation.StatementIDs()) == 0 {
123 return &ValidationError{Name: "statement", err: errors.New(`ent: missing required edge "AttestationCollection.statement"`)}
124 }
125 return nil
126 }
127
128 func (_c *AttestationCollectionCreate) sqlSave(ctx context.Context) (*AttestationCollection, error) {
129 if err := _c.check(); err != nil {
130 return nil, err
131 }
132 _node, _spec := _c.createSpec()
133 if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
134 if sqlgraph.IsConstraintError(err) {
135 err = &ConstraintError{msg: err.Error(), wrap: err}
136 }
137 return nil, err
138 }
139 if _spec.ID.Value != nil {
140 if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
141 _node.ID = *id
142 } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
143 return nil, err
144 }
145 }
146 _c.mutation.id = &_node.ID
147 _c.mutation.done = true
148 return _node, nil
149 }
150
151 func (_c *AttestationCollectionCreate) createSpec() (*AttestationCollection, *sqlgraph.CreateSpec) {
152 var (
153 _node = &AttestationCollection{config: _c.config}
154 _spec = sqlgraph.NewCreateSpec(attestationcollection.Table, sqlgraph.NewFieldSpec(attestationcollection.FieldID, field.TypeUUID))
155 )
156 if id, ok := _c.mutation.ID(); ok {
157 _node.ID = id
158 _spec.ID.Value = &id
159 }
160 if value, ok := _c.mutation.Name(); ok {
161 _spec.SetField(attestationcollection.FieldName, field.TypeString, value)
162 _node.Name = value
163 }
164 if nodes := _c.mutation.AttestationsIDs(); len(nodes) > 0 {
165 edge := &sqlgraph.EdgeSpec{
166 Rel: sqlgraph.O2M,
167 Inverse: false,
168 Table: attestationcollection.AttestationsTable,
169 Columns: []string{attestationcollection.AttestationsColumn},
170 Bidi: false,
171 Target: &sqlgraph.EdgeTarget{
172 IDSpec: sqlgraph.NewFieldSpec(attestation.FieldID, field.TypeUUID),
173 },
174 }
175 for _, k := range nodes {
176 edge.Target.Nodes = append(edge.Target.Nodes, k)
177 }
178 _spec.Edges = append(_spec.Edges, edge)
179 }
180 if nodes := _c.mutation.StatementIDs(); len(nodes) > 0 {
181 edge := &sqlgraph.EdgeSpec{
182 Rel: sqlgraph.O2O,
183 Inverse: true,
184 Table: attestationcollection.StatementTable,
185 Columns: []string{attestationcollection.StatementColumn},
186 Bidi: false,
187 Target: &sqlgraph.EdgeTarget{
188 IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID),
189 },
190 }
191 for _, k := range nodes {
192 edge.Target.Nodes = append(edge.Target.Nodes, k)
193 }
194 _node.statement_attestation_collections = &nodes[0]
195 _spec.Edges = append(_spec.Edges, edge)
196 }
197 return _node, _spec
198 }
199
200 // AttestationCollectionCreateBulk is the builder for creating many AttestationCollection entities in bulk.
201 type AttestationCollectionCreateBulk struct {
202 config
203 err error
204 builders []*AttestationCollectionCreate
205 }
206
207 // Save creates the AttestationCollection entities in the database.
208 func (_c *AttestationCollectionCreateBulk) Save(ctx context.Context) ([]*AttestationCollection, error) {
209 if _c.err != nil {
210 return nil, _c.err
211 }
212 specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
213 nodes := make([]*AttestationCollection, len(_c.builders))
214 mutators := make([]Mutator, len(_c.builders))
215 for i := range _c.builders {
216 func(i int, root context.Context) {
217 builder := _c.builders[i]
218 builder.defaults()
219 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
220 mutation, ok := m.(*AttestationCollectionMutation)
221 if !ok {
222 return nil, fmt.Errorf("unexpected mutation type %T", m)
223 }
224 if err := builder.check(); err != nil {
225 return nil, err
226 }
227 builder.mutation = mutation
228 var err error
229 nodes[i], specs[i] = builder.createSpec()
230 if i < len(mutators)-1 {
231 _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
232 } else {
233 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
234 // Invoke the actual operation on the latest mutation in the chain.
235 if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
236 if sqlgraph.IsConstraintError(err) {
237 err = &ConstraintError{msg: err.Error(), wrap: err}
238 }
239 }
240 }
241 if err != nil {
242 return nil, err
243 }
244 mutation.id = &nodes[i].ID
245 mutation.done = true
246 return nodes[i], nil
247 })
248 for i := len(builder.hooks) - 1; i >= 0; i-- {
249 mut = builder.hooks[i](mut)
250 }
251 mutators[i] = mut
252 }(i, ctx)
253 }
254 if len(mutators) > 0 {
255 if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
256 return nil, err
257 }
258 }
259 return nodes, nil
260 }
261
262 // SaveX is like Save, but panics if an error occurs.
263 func (_c *AttestationCollectionCreateBulk) SaveX(ctx context.Context) []*AttestationCollection {
264 v, err := _c.Save(ctx)
265 if err != nil {
266 panic(err)
267 }
268 return v
269 }
270
271 // Exec executes the query.
272 func (_c *AttestationCollectionCreateBulk) Exec(ctx context.Context) error {
273 _, err := _c.Save(ctx)
274 return err
275 }
276
277 // ExecX is like Exec, but panics if an error occurs.
278 func (_c *AttestationCollectionCreateBulk) ExecX(ctx context.Context) {
279 if err := _c.Exec(ctx); err != nil {
280 panic(err)
281 }
282 }