// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/google/uuid" "github.com/in-toto/archivista/ent/attestationcollection" "github.com/in-toto/archivista/ent/attestationpolicy" "github.com/in-toto/archivista/ent/dsse" "github.com/in-toto/archivista/ent/statement" "github.com/in-toto/archivista/ent/subject" ) // StatementCreate is the builder for creating a Statement entity. type StatementCreate struct { config mutation *StatementMutation hooks []Hook } // SetPredicate sets the "predicate" field. func (_c *StatementCreate) SetPredicate(v string) *StatementCreate { _c.mutation.SetPredicate(v) return _c } // SetID sets the "id" field. func (_c *StatementCreate) SetID(v uuid.UUID) *StatementCreate { _c.mutation.SetID(v) return _c } // SetNillableID sets the "id" field if the given value is not nil. func (_c *StatementCreate) SetNillableID(v *uuid.UUID) *StatementCreate { if v != nil { _c.SetID(*v) } return _c } // AddSubjectIDs adds the "subjects" edge to the Subject entity by IDs. func (_c *StatementCreate) AddSubjectIDs(ids ...uuid.UUID) *StatementCreate { _c.mutation.AddSubjectIDs(ids...) return _c } // AddSubjects adds the "subjects" edges to the Subject entity. func (_c *StatementCreate) AddSubjects(v ...*Subject) *StatementCreate { ids := make([]uuid.UUID, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddSubjectIDs(ids...) } // SetPolicyID sets the "policy" edge to the AttestationPolicy entity by ID. func (_c *StatementCreate) SetPolicyID(id uuid.UUID) *StatementCreate { _c.mutation.SetPolicyID(id) return _c } // SetNillablePolicyID sets the "policy" edge to the AttestationPolicy entity by ID if the given value is not nil. func (_c *StatementCreate) SetNillablePolicyID(id *uuid.UUID) *StatementCreate { if id != nil { _c = _c.SetPolicyID(*id) } return _c } // SetPolicy sets the "policy" edge to the AttestationPolicy entity. func (_c *StatementCreate) SetPolicy(v *AttestationPolicy) *StatementCreate { return _c.SetPolicyID(v.ID) } // SetAttestationCollectionsID sets the "attestation_collections" edge to the AttestationCollection entity by ID. func (_c *StatementCreate) SetAttestationCollectionsID(id uuid.UUID) *StatementCreate { _c.mutation.SetAttestationCollectionsID(id) return _c } // SetNillableAttestationCollectionsID sets the "attestation_collections" edge to the AttestationCollection entity by ID if the given value is not nil. func (_c *StatementCreate) SetNillableAttestationCollectionsID(id *uuid.UUID) *StatementCreate { if id != nil { _c = _c.SetAttestationCollectionsID(*id) } return _c } // SetAttestationCollections sets the "attestation_collections" edge to the AttestationCollection entity. func (_c *StatementCreate) SetAttestationCollections(v *AttestationCollection) *StatementCreate { return _c.SetAttestationCollectionsID(v.ID) } // AddDsseIDs adds the "dsse" edge to the Dsse entity by IDs. func (_c *StatementCreate) AddDsseIDs(ids ...uuid.UUID) *StatementCreate { _c.mutation.AddDsseIDs(ids...) return _c } // AddDsse adds the "dsse" edges to the Dsse entity. func (_c *StatementCreate) AddDsse(v ...*Dsse) *StatementCreate { ids := make([]uuid.UUID, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddDsseIDs(ids...) } // Mutation returns the StatementMutation object of the builder. func (_c *StatementCreate) Mutation() *StatementMutation { return _c.mutation } // Save creates the Statement in the database. func (_c *StatementCreate) Save(ctx context.Context) (*Statement, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *StatementCreate) SaveX(ctx context.Context) *Statement { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *StatementCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *StatementCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *StatementCreate) defaults() { if _, ok := _c.mutation.ID(); !ok { v := statement.DefaultID() _c.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (_c *StatementCreate) check() error { if _, ok := _c.mutation.Predicate(); !ok { return &ValidationError{Name: "predicate", err: errors.New(`ent: missing required field "Statement.predicate"`)} } if v, ok := _c.mutation.Predicate(); ok { if err := statement.PredicateValidator(v); err != nil { return &ValidationError{Name: "predicate", err: fmt.Errorf(`ent: validator failed for field "Statement.predicate": %w`, err)} } } return nil } func (_c *StatementCreate) sqlSave(ctx context.Context) (*Statement, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec := _c.createSpec() if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != nil { if id, ok := _spec.ID.Value.(*uuid.UUID); ok { _node.ID = *id } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { return nil, err } } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *StatementCreate) createSpec() (*Statement, *sqlgraph.CreateSpec) { var ( _node = &Statement{config: _c.config} _spec = sqlgraph.NewCreateSpec(statement.Table, sqlgraph.NewFieldSpec(statement.FieldID, field.TypeUUID)) ) if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = &id } if value, ok := _c.mutation.Predicate(); ok { _spec.SetField(statement.FieldPredicate, field.TypeString, value) _node.Predicate = value } if nodes := _c.mutation.SubjectsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: statement.SubjectsTable, Columns: []string{statement.SubjectsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(subject.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.PolicyIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: false, Table: statement.PolicyTable, Columns: []string{statement.PolicyColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(attestationpolicy.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.AttestationCollectionsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: false, Table: statement.AttestationCollectionsTable, Columns: []string{statement.AttestationCollectionsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(attestationcollection.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.DsseIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: true, Table: statement.DsseTable, Columns: []string{statement.DsseColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dsse.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // StatementCreateBulk is the builder for creating many Statement entities in bulk. type StatementCreateBulk struct { config err error builders []*StatementCreate } // Save creates the Statement entities in the database. func (_c *StatementCreateBulk) Save(ctx context.Context) ([]*Statement, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Statement, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*StatementMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation var err error nodes[i], specs[i] = builder.createSpec() if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID mutation.done = true return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *StatementCreateBulk) SaveX(ctx context.Context) []*Statement { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *StatementCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *StatementCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }