Debian Codegraph @

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