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