Debian Codegraph @

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