| 1 |
// Code generated by ent, DO NOT EDIT. |
| 2 |
|
| 3 |
package ent |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"time" |
| 10 |
|
| 11 |
"entgo.io/ent/dialect/sql/sqlgraph" |
| 12 |
"entgo.io/ent/schema/field" |
| 13 |
"github.com/google/uuid" |
| 14 |
"github.com/in-toto/archivista/ent/dsse" |
| 15 |
"github.com/in-toto/archivista/ent/payloaddigest" |
| 16 |
"github.com/in-toto/archivista/ent/signature" |
| 17 |
"github.com/in-toto/archivista/ent/statement" |
| 18 |
) |
| 19 |
|
| 20 |
// DsseCreate is the builder for creating a Dsse entity. |
| 21 |
type DsseCreate struct { |
| 22 |
config |
| 23 |
mutation *DsseMutation |
| 24 |
hooks []Hook |
| 25 |
} |
| 26 |
|
| 27 |
// SetCreatedAt sets the "created_at" field. |
| 28 |
func (_c *DsseCreate) SetCreatedAt(v time.Time) *DsseCreate { |
| 29 |
_c.mutation.SetCreatedAt(v) |
| 30 |
return _c |
| 31 |
} |
| 32 |
|
| 33 |
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil. |
| 34 |
func (_c *DsseCreate) SetNillableCreatedAt(v *time.Time) *DsseCreate { |
| 35 |
if v != nil { |
| 36 |
_c.SetCreatedAt(*v) |
| 37 |
} |
| 38 |
return _c |
| 39 |
} |
| 40 |
|
| 41 |
// SetGitoidSha256 sets the "gitoid_sha256" field. |
| 42 |
func (_c *DsseCreate) SetGitoidSha256(v string) *DsseCreate { |
| 43 |
_c.mutation.SetGitoidSha256(v) |
| 44 |
return _c |
| 45 |
} |
| 46 |
|
| 47 |
// SetPayloadType sets the "payload_type" field. |
| 48 |
func (_c *DsseCreate) SetPayloadType(v string) *DsseCreate { |
| 49 |
_c.mutation.SetPayloadType(v) |
| 50 |
return _c |
| 51 |
} |
| 52 |
|
| 53 |
// SetID sets the "id" field. |
| 54 |
func (_c *DsseCreate) SetID(v uuid.UUID) *DsseCreate { |
| 55 |
_c.mutation.SetID(v) |
| 56 |
return _c |
| 57 |
} |
| 58 |
|
| 59 |
// SetNillableID sets the "id" field if the given value is not nil. |
| 60 |
func (_c *DsseCreate) SetNillableID(v *uuid.UUID) *DsseCreate { |
| 61 |
if v != nil { |
| 62 |
_c.SetID(*v) |
| 63 |
} |
| 64 |
return _c |
| 65 |
} |
| 66 |
|
| 67 |
// SetStatementID sets the "statement" edge to the Statement entity by ID. |
| 68 |
func (_c *DsseCreate) SetStatementID(id uuid.UUID) *DsseCreate { |
| 69 |
_c.mutation.SetStatementID(id) |
| 70 |
return _c |
| 71 |
} |
| 72 |
|
| 73 |
// SetNillableStatementID sets the "statement" edge to the Statement entity by ID if the given value is not nil. |
| 74 |
func (_c *DsseCreate) SetNillableStatementID(id *uuid.UUID) *DsseCreate { |
| 75 |
if id != nil { |
| 76 |
_c = _c.SetStatementID(*id) |
| 77 |
} |
| 78 |
return _c |
| 79 |
} |
| 80 |
|
| 81 |
// SetStatement sets the "statement" edge to the Statement entity. |
| 82 |
func (_c *DsseCreate) SetStatement(v *Statement) *DsseCreate { |
| 83 |
return _c.SetStatementID(v.ID) |
| 84 |
} |
| 85 |
|
| 86 |
// AddSignatureIDs adds the "signatures" edge to the Signature entity by IDs. |
| 87 |
func (_c *DsseCreate) AddSignatureIDs(ids ...uuid.UUID) *DsseCreate { |
| 88 |
_c.mutation.AddSignatureIDs(ids...) |
| 89 |
return _c |
| 90 |
} |
| 91 |
|
| 92 |
// AddSignatures adds the "signatures" edges to the Signature entity. |
| 93 |
func (_c *DsseCreate) AddSignatures(v ...*Signature) *DsseCreate { |
| 94 |
ids := make([]uuid.UUID, len(v)) |
| 95 |
for i := range v { |
| 96 |
ids[i] = v[i].ID |
| 97 |
} |
| 98 |
return _c.AddSignatureIDs(ids...) |
| 99 |
} |
| 100 |
|
| 101 |
// AddPayloadDigestIDs adds the "payload_digests" edge to the PayloadDigest entity by IDs. |
| 102 |
func (_c *DsseCreate) AddPayloadDigestIDs(ids ...uuid.UUID) *DsseCreate { |
| 103 |
_c.mutation.AddPayloadDigestIDs(ids...) |
| 104 |
return _c |
| 105 |
} |
| 106 |
|
| 107 |
// AddPayloadDigests adds the "payload_digests" edges to the PayloadDigest entity. |
| 108 |
func (_c *DsseCreate) AddPayloadDigests(v ...*PayloadDigest) *DsseCreate { |
| 109 |
ids := make([]uuid.UUID, len(v)) |
| 110 |
for i := range v { |
| 111 |
ids[i] = v[i].ID |
| 112 |
} |
| 113 |
return _c.AddPayloadDigestIDs(ids...) |
| 114 |
} |
| 115 |
|
| 116 |
// Mutation returns the DsseMutation object of the builder. |
| 117 |
func (_c *DsseCreate) Mutation() *DsseMutation { |
| 118 |
return _c.mutation |
| 119 |
} |
| 120 |
|
| 121 |
// Save creates the Dsse in the database. |
| 122 |
func (_c *DsseCreate) Save(ctx context.Context) (*Dsse, error) { |
| 123 |
_c.defaults() |
| 124 |
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) |
| 125 |
} |
| 126 |
|
| 127 |
// SaveX calls Save and panics if Save returns an error. |
| 128 |
func (_c *DsseCreate) SaveX(ctx context.Context) *Dsse { |
| 129 |
v, err := _c.Save(ctx) |
| 130 |
if err != nil { |
| 131 |
panic(err) |
| 132 |
} |
| 133 |
return v |
| 134 |
} |
| 135 |
|
| 136 |
// Exec executes the query. |
| 137 |
func (_c *DsseCreate) Exec(ctx context.Context) error { |
| 138 |
_, err := _c.Save(ctx) |
| 139 |
return err |
| 140 |
} |
| 141 |
|
| 142 |
// ExecX is like Exec, but panics if an error occurs. |
| 143 |
func (_c *DsseCreate) ExecX(ctx context.Context) { |
| 144 |
if err := _c.Exec(ctx); err != nil { |
| 145 |
panic(err) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// defaults sets the default values of the builder before save. |
| 150 |
func (_c *DsseCreate) defaults() { |
| 151 |
if _, ok := _c.mutation.CreatedAt(); !ok { |
| 152 |
v := dsse.DefaultCreatedAt() |
| 153 |
_c.mutation.SetCreatedAt(v) |
| 154 |
} |
| 155 |
if _, ok := _c.mutation.ID(); !ok { |
| 156 |
v := dsse.DefaultID() |
| 157 |
_c.mutation.SetID(v) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// check runs all checks and user-defined validators on the builder. |
| 162 |
func (_c *DsseCreate) check() error { |
| 163 |
if _, ok := _c.mutation.GitoidSha256(); !ok { |
| 164 |
return &ValidationError{Name: "gitoid_sha256", err: errors.New(`ent: missing required field "Dsse.gitoid_sha256"`)} |
| 165 |
} |
| 166 |
if v, ok := _c.mutation.GitoidSha256(); ok { |
| 167 |
if err := dsse.GitoidSha256Validator(v); err != nil { |
| 168 |
return &ValidationError{Name: "gitoid_sha256", err: fmt.Errorf(`ent: validator failed for field "Dsse.gitoid_sha256": %w`, err)} |
| 169 |
} |
| 170 |
} |
| 171 |
if _, ok := _c.mutation.PayloadType(); !ok { |
| 172 |
return &ValidationError{Name: "payload_type", err: errors.New(`ent: missing required field "Dsse.payload_type"`)} |
| 173 |
} |
| 174 |
if v, ok := _c.mutation.PayloadType(); ok { |
| 175 |
if err := dsse.PayloadTypeValidator(v); err != nil { |
| 176 |
return &ValidationError{Name: "payload_type", err: fmt.Errorf(`ent: validator failed for field "Dsse.payload_type": %w`, err)} |
| 177 |
} |
| 178 |
} |
| 179 |
return nil |
| 180 |
} |
| 181 |
|
| 182 |
func (_c *DsseCreate) sqlSave(ctx context.Context) (*Dsse, error) { |
| 183 |
if err := _c.check(); err != nil { |
| 184 |
return nil, err |
| 185 |
} |
| 186 |
_node, _spec := _c.createSpec() |
| 187 |
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { |
| 188 |
if sqlgraph.IsConstraintError(err) { |
| 189 |
err = &ConstraintError{msg: err.Error(), wrap: err} |
| 190 |
} |
| 191 |
return nil, err |
| 192 |
} |
| 193 |
if _spec.ID.Value != nil { |
| 194 |
if id, ok := _spec.ID.Value.(*uuid.UUID); ok { |
| 195 |
_node.ID = *id |
| 196 |
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil { |
| 197 |
return nil, err |
| 198 |
} |
| 199 |
} |
| 200 |
_c.mutation.id = &_node.ID |
| 201 |
_c.mutation.done = true |
| 202 |
return _node, nil |
| 203 |
} |
| 204 |
|
| 205 |
func (_c *DsseCreate) createSpec() (*Dsse, *sqlgraph.CreateSpec) { |
| 206 |
var ( |
| 207 |
_node = &Dsse{config: _c.config} |
| 208 |
_spec = sqlgraph.NewCreateSpec(dsse.Table, sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID)) |
| 209 |
) |
| 210 |
if id, ok := _c.mutation.ID(); ok { |
| 211 |
_node.ID = id |
| 212 |
_spec.ID.Value = &id |
| 213 |
} |
| 214 |
if value, ok := _c.mutation.CreatedAt(); ok { |
| 215 |
_spec.SetField(dsse.FieldCreatedAt, field.TypeTime, value) |
| 216 |
_node.CreatedAt = &value |
| 217 |
} |
| 218 |
if value, ok := _c.mutation.GitoidSha256(); ok { |
| 219 |
_spec.SetField(dsse.FieldGitoidSha256, field.TypeString, value) |
| 220 |
_node.GitoidSha256 = value |
| 221 |
} |
| 222 |
if value, ok := _c.mutation.PayloadType(); ok { |
| 223 |
_spec.SetField(dsse.FieldPayloadType, field.TypeString, value) |
| 224 |
_node.PayloadType = value |
| 225 |
} |
| 226 |
if nodes := _c.mutation.StatementIDs(); len(nodes) > 0 { |
| 227 |
edge := &sqlgraph.EdgeSpec{ |
| 228 |
Rel: sqlgraph.M2O, |
| 229 |
Inverse: false, |
| 230 |
Table: dsse.StatementTable, |
| 231 |
Columns: []string{dsse.StatementColumn}, |
| 232 |
Bidi: false, |
| 233 |
Target: &sqlgraph.EdgeTarget{ |
| 234 |
IDSpec: sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID), |
| 235 |
}, |
| 236 |
} |
| 237 |
for _, k := range nodes { |
| 238 |
edge.Target.Nodes = append(edge.Target.Nodes, k) |
| 239 |
} |
| 240 |
_node.dsse_statement = &nodes[0] |
| 241 |
_spec.Edges = append(_spec.Edges, edge) |
| 242 |
} |
| 243 |
if nodes := _c.mutation.SignaturesIDs(); len(nodes) > 0 { |
| 244 |
edge := &sqlgraph.EdgeSpec{ |
| 245 |
Rel: sqlgraph.O2M, |
| 246 |
Inverse: false, |
| 247 |
Table: dsse.SignaturesTable, |
| 248 |
Columns: []string{dsse.SignaturesColumn}, |
| 249 |
Bidi: false, |
| 250 |
Target: &sqlgraph.EdgeTarget{ |
| 251 |
IDSpec: sqlgraph.NewFieldSpec(signature.FieldID, field.TypeUUID), |
| 252 |
}, |
| 253 |
} |
| 254 |
for _, k := range nodes { |
| 255 |
edge.Target.Nodes = append(edge.Target.Nodes, k) |
| 256 |
} |
| 257 |
_spec.Edges = append(_spec.Edges, edge) |
| 258 |
} |
| 259 |
if nodes := _c.mutation.PayloadDigestsIDs(); len(nodes) > 0 { |
| 260 |
edge := &sqlgraph.EdgeSpec{ |
| 261 |
Rel: sqlgraph.O2M, |
| 262 |
Inverse: false, |
| 263 |
Table: dsse.PayloadDigestsTable, |
| 264 |
Columns: []string{dsse.PayloadDigestsColumn}, |
| 265 |
Bidi: false, |
| 266 |
Target: &sqlgraph.EdgeTarget{ |
| 267 |
IDSpec: sqlgraph.NewFieldSpec(payloaddigest.FieldID, field.TypeUUID), |
| 268 |
}, |
| 269 |
} |
| 270 |
for _, k := range nodes { |
| 271 |
edge.Target.Nodes = append(edge.Target.Nodes, k) |
| 272 |
} |
| 273 |
_spec.Edges = append(_spec.Edges, edge) |
| 274 |
} |
| 275 |
return _node, _spec |
| 276 |
} |
| 277 |
|
| 278 |
// DsseCreateBulk is the builder for creating many Dsse entities in bulk. |
| 279 |
type DsseCreateBulk struct { |
| 280 |
config |
| 281 |
err error |
| 282 |
builders []*DsseCreate |
| 283 |
} |
| 284 |
|
| 285 |
// Save creates the Dsse entities in the database. |
| 286 |
func (_c *DsseCreateBulk) Save(ctx context.Context) ([]*Dsse, error) { |
| 287 |
if _c.err != nil { |
| 288 |
return nil, _c.err |
| 289 |
} |
| 290 |
specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) |
| 291 |
nodes := make([]*Dsse, len(_c.builders)) |
| 292 |
mutators := make([]Mutator, len(_c.builders)) |
| 293 |
for i := range _c.builders { |
| 294 |
func(i int, root context.Context) { |
| 295 |
builder := _c.builders[i] |
| 296 |
builder.defaults() |
| 297 |
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 298 |
mutation, ok := m.(*DsseMutation) |
| 299 |
if !ok { |
| 300 |
return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 301 |
} |
| 302 |
if err := builder.check(); err != nil { |
| 303 |
return nil, err |
| 304 |
} |
| 305 |
builder.mutation = mutation |
| 306 |
var err error |
| 307 |
nodes[i], specs[i] = builder.createSpec() |
| 308 |
if i < len(mutators)-1 { |
| 309 |
_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) |
| 310 |
} else { |
| 311 |
spec := &sqlgraph.BatchCreateSpec{Nodes: specs} |
| 312 |
// Invoke the actual operation on the latest mutation in the chain. |
| 313 |
if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil { |
| 314 |
if sqlgraph.IsConstraintError(err) { |
| 315 |
err = &ConstraintError{msg: err.Error(), wrap: err} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
if err != nil { |
| 320 |
return nil, err |
| 321 |
} |
| 322 |
mutation.id = &nodes[i].ID |
| 323 |
mutation.done = true |
| 324 |
return nodes[i], nil |
| 325 |
}) |
| 326 |
for i := len(builder.hooks) - 1; i >= 0; i-- { |
| 327 |
mut = builder.hooks[i](mut) |
| 328 |
} |
| 329 |
mutators[i] = mut |
| 330 |
}(i, ctx) |
| 331 |
} |
| 332 |
if len(mutators) > 0 { |
| 333 |
if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil { |
| 334 |
return nil, err |
| 335 |
} |
| 336 |
} |
| 337 |
return nodes, nil |
| 338 |
} |
| 339 |
|
| 340 |
// SaveX is like Save, but panics if an error occurs. |
| 341 |
func (_c *DsseCreateBulk) SaveX(ctx context.Context) []*Dsse { |
| 342 |
v, err := _c.Save(ctx) |
| 343 |
if err != nil { |
| 344 |
panic(err) |
| 345 |
} |
| 346 |
return v |
| 347 |
} |
| 348 |
|
| 349 |
// Exec executes the query. |
| 350 |
func (_c *DsseCreateBulk) Exec(ctx context.Context) error { |
| 351 |
_, err := _c.Save(ctx) |
| 352 |
return err |
| 353 |
} |
| 354 |
|
| 355 |
// ExecX is like Exec, but panics if an error occurs. |
| 356 |
func (_c *DsseCreateBulk) ExecX(ctx context.Context) { |
| 357 |
if err := _c.Exec(ctx); err != nil { |
| 358 |
panic(err) |
| 359 |
} |
| 360 |
} |