Debian Codegraph @

ent/attestation_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 )
16
17 // AttestationCreate is the builder for creating a Attestation entity.
18 type AttestationCreate struct {
19 config
20 mutation *AttestationMutation
21 hooks []Hook
22 }
23
24 // SetType sets the "type" field.
25 func (_c *AttestationCreate) SetType(v string) *AttestationCreate {
26 _c.mutation.SetType(v)
27 return _c
28 }
29
30 // SetID sets the "id" field.
31 func (_c *AttestationCreate) SetID(v uuid.UUID) *AttestationCreate {
32 _c.mutation.SetID(v)
33 return _c
34 }
35
36 // SetNillableID sets the "id" field if the given value is not nil.
37 func (_c *AttestationCreate) SetNillableID(v *uuid.UUID) *AttestationCreate {
38 if v != nil {
39 _c.SetID(*v)
40 }
41 return _c
42 }
43
44 // SetAttestationCollectionID sets the "attestation_collection" edge to the AttestationCollection entity by ID.
45 func (_c *AttestationCreate) SetAttestationCollectionID(id uuid.UUID) *AttestationCreate {
46 _c.mutation.SetAttestationCollectionID(id)
47 return _c
48 }
49
50 // SetAttestationCollection sets the "attestation_collection" edge to the AttestationCollection entity.
51 func (_c *AttestationCreate) SetAttestationCollection(v *AttestationCollection) *AttestationCreate {
52 return _c.SetAttestationCollectionID(v.ID)
53 }
54
55 // Mutation returns the AttestationMutation object of the builder.
56 func (_c *AttestationCreate) Mutation() *AttestationMutation {
57 return _c.mutation
58 }
59
60 // Save creates the Attestation in the database.
61 func (_c *AttestationCreate) Save(ctx context.Context) (*Attestation, error) {
62 _c.defaults()
63 return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
64 }
65
66 // SaveX calls Save and panics if Save returns an error.
67 func (_c *AttestationCreate) SaveX(ctx context.Context) *Attestation {
68 v, err := _c.Save(ctx)
69 if err != nil {
70 panic(err)
71 }
72 return v
73 }
74
75 // Exec executes the query.
76 func (_c *AttestationCreate) Exec(ctx context.Context) error {
77 _, err := _c.Save(ctx)
78 return err
79 }
80
81 // ExecX is like Exec, but panics if an error occurs.
82 func (_c *AttestationCreate) ExecX(ctx context.Context) {
83 if err := _c.Exec(ctx); err != nil {
84 panic(err)
85 }
86 }
87
88 // defaults sets the default values of the builder before save.
89 func (_c *AttestationCreate) defaults() {
90 if _, ok := _c.mutation.ID(); !ok {
91 v := attestation.DefaultID()
92 _c.mutation.SetID(v)
93 }
94 }
95
96 // check runs all checks and user-defined validators on the builder.
97 func (_c *AttestationCreate) check() error {
98 if _, ok := _c.mutation.GetType(); !ok {
99 return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Attestation.type"`)}
100 }
101 if v, ok := _c.mutation.GetType(); ok {
102 if err := attestation.TypeValidator(v); err != nil {
103 return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Attestation.type": %w`, err)}
104 }
105 }
106 if len(_c.mutation.AttestationCollectionIDs()) == 0 {
107 return &ValidationError{Name: "attestation_collection", err: errors.New(`ent: missing required edge "Attestation.attestation_collection"`)}
108 }
109 return nil
110 }
111
112 func (_c *AttestationCreate) sqlSave(ctx context.Context) (*Attestation, error) {
113 if err := _c.check(); err != nil {
114 return nil, err
115 }
116 _node, _spec := _c.createSpec()
117 if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
118 if sqlgraph.IsConstraintError(err) {
119 err = &ConstraintError{msg: err.Error(), wrap: err}
120 }
121 return nil, err
122 }
123 if _spec.ID.Value != nil {
124 if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
125 _node.ID = *id
126 } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
127 return nil, err
128 }
129 }
130 _c.mutation.id = &_node.ID
131 _c.mutation.done = true
132 return _node, nil
133 }
134
135 func (_c *AttestationCreate) createSpec() (*Attestation, *sqlgraph.CreateSpec) {
136 var (
137 _node = &Attestation{config: _c.config}
138 _spec = sqlgraph.NewCreateSpec(attestation.Table, sqlgraph.NewFieldSpec(attestation.FieldID, field.TypeUUID))
139 )
140 if id, ok := _c.mutation.ID(); ok {
141 _node.ID = id
142 _spec.ID.Value = &id
143 }
144 if value, ok := _c.mutation.GetType(); ok {
145 _spec.SetField(attestation.FieldType, field.TypeString, value)
146 _node.Type = value
147 }
148 if nodes := _c.mutation.AttestationCollectionIDs(); len(nodes) > 0 {
149 edge := &sqlgraph.EdgeSpec{
150 Rel: sqlgraph.M2O,
151 Inverse: true,
152 Table: attestation.AttestationCollectionTable,
153 Columns: []string{attestation.AttestationCollectionColumn},
154 Bidi: false,
155 Target: &sqlgraph.EdgeTarget{
156 IDSpec: sqlgraph.NewFieldSpec(attestationcollection.FieldID, field.TypeUUID),
157 },
158 }
159 for _, k := range nodes {
160 edge.Target.Nodes = append(edge.Target.Nodes, k)
161 }
162 _node.attestation_collection_attestations = &nodes[0]
163 _spec.Edges = append(_spec.Edges, edge)
164 }
165 return _node, _spec
166 }
167
168 // AttestationCreateBulk is the builder for creating many Attestation entities in bulk.
169 type AttestationCreateBulk struct {
170 config
171 err error
172 builders []*AttestationCreate
173 }
174
175 // Save creates the Attestation entities in the database.
176 func (_c *AttestationCreateBulk) Save(ctx context.Context) ([]*Attestation, error) {
177 if _c.err != nil {
178 return nil, _c.err
179 }
180 specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
181 nodes := make([]*Attestation, len(_c.builders))
182 mutators := make([]Mutator, len(_c.builders))
183 for i := range _c.builders {
184 func(i int, root context.Context) {
185 builder := _c.builders[i]
186 builder.defaults()
187 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
188 mutation, ok := m.(*AttestationMutation)
189 if !ok {
190 return nil, fmt.Errorf("unexpected mutation type %T", m)
191 }
192 if err := builder.check(); err != nil {
193 return nil, err
194 }
195 builder.mutation = mutation
196 var err error
197 nodes[i], specs[i] = builder.createSpec()
198 if i < len(mutators)-1 {
199 _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
200 } else {
201 spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
202 // Invoke the actual operation on the latest mutation in the chain.
203 if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
204 if sqlgraph.IsConstraintError(err) {
205 err = &ConstraintError{msg: err.Error(), wrap: err}
206 }
207 }
208 }
209 if err != nil {
210 return nil, err
211 }
212 mutation.id = &nodes[i].ID
213 mutation.done = true
214 return nodes[i], nil
215 })
216 for i := len(builder.hooks) - 1; i >= 0; i-- {
217 mut = builder.hooks[i](mut)
218 }
219 mutators[i] = mut
220 }(i, ctx)
221 }
222 if len(mutators) > 0 {
223 if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
224 return nil, err
225 }
226 }
227 return nodes, nil
228 }
229
230 // SaveX is like Save, but panics if an error occurs.
231 func (_c *AttestationCreateBulk) SaveX(ctx context.Context) []*Attestation {
232 v, err := _c.Save(ctx)
233 if err != nil {
234 panic(err)
235 }
236 return v
237 }
238
239 // Exec executes the query.
240 func (_c *AttestationCreateBulk) Exec(ctx context.Context) error {
241 _, err := _c.Save(ctx)
242 return err
243 }
244
245 // ExecX is like Exec, but panics if an error occurs.
246 func (_c *AttestationCreateBulk) ExecX(ctx context.Context) {
247 if err := _c.Exec(ctx); err != nil {
248 panic(err)
249 }
250 }