// Copyright 2019-present Facebook Inc. All rights reserved. // This source code is licensed under the Apache 2.0 license found // in the LICENSE file in the root directory of this source tree. // Code generated by entc, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/entc/integration/ent/schema" "entgo.io/ent/entc/integration/ent/task" "entgo.io/ent/schema/field" ) // TaskCreate is the builder for creating a Task entity. type TaskCreate struct { config mutation *TaskMutation hooks []Hook } // SetPriority sets the "priority" field. func (tc *TaskCreate) SetPriority(s schema.Priority) *TaskCreate { tc.mutation.SetPriority(s) return tc } // SetNillablePriority sets the "priority" field if the given value is not nil. func (tc *TaskCreate) SetNillablePriority(s *schema.Priority) *TaskCreate { if s != nil { tc.SetPriority(*s) } return tc } // Mutation returns the TaskMutation object of the builder. func (tc *TaskCreate) Mutation() *TaskMutation { return tc.mutation } // Save creates the Task in the database. func (tc *TaskCreate) Save(ctx context.Context) (*Task, error) { var ( err error node *Task ) tc.defaults() if len(tc.hooks) == 0 { if err = tc.check(); err != nil { return nil, err } node, err = tc.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*TaskMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = tc.check(); err != nil { return nil, err } tc.mutation = mutation if node, err = tc.sqlSave(ctx); err != nil { return nil, err } mutation.id = &node.ID mutation.done = true return node, err }) for i := len(tc.hooks) - 1; i >= 0; i-- { mut = tc.hooks[i](mut) } if _, err := mut.Mutate(ctx, tc.mutation); err != nil { return nil, err } } return node, err } // SaveX calls Save and panics if Save returns an error. func (tc *TaskCreate) SaveX(ctx context.Context) *Task { v, err := tc.Save(ctx) if err != nil { panic(err) } return v } // defaults sets the default values of the builder before save. func (tc *TaskCreate) defaults() { if _, ok := tc.mutation.Priority(); !ok { v := task.DefaultPriority tc.mutation.SetPriority(v) } } // check runs all checks and user-defined validators on the builder. func (tc *TaskCreate) check() error { if _, ok := tc.mutation.Priority(); !ok { return &ValidationError{Name: "priority", err: errors.New("ent: missing required field \"priority\"")} } if v, ok := tc.mutation.Priority(); ok { if err := task.PriorityValidator(int(v)); err != nil { return &ValidationError{Name: "priority", err: fmt.Errorf("ent: validator failed for field \"priority\": %w", err)} } } return nil } func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) { _node, _spec := tc.createSpec() if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) return _node, nil } func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) { var ( _node = &Task{config: tc.config} _spec = &sqlgraph.CreateSpec{ Table: task.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: task.FieldID, }, } ) if value, ok := tc.mutation.Priority(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, Column: task.FieldPriority, }) _node.Priority = value } return _node, _spec } // TaskCreateBulk is the builder for creating many Task entities in bulk. type TaskCreateBulk struct { config builders []*TaskCreate } // Save creates the Task entities in the database. func (tcb *TaskCreateBulk) Save(ctx context.Context) ([]*Task, error) { specs := make([]*sqlgraph.CreateSpec, len(tcb.builders)) nodes := make([]*Task, len(tcb.builders)) mutators := make([]Mutator, len(tcb.builders)) for i := range tcb.builders { func(i int, root context.Context) { builder := tcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*TaskMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation nodes[i], specs[i] = builder.createSpec() var err error if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, tcb.builders[i+1].mutation) } else { // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, tcb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID mutation.done = true id := specs[i].ID.Value.(int64) nodes[i].ID = int(id) 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, tcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (tcb *TaskCreateBulk) SaveX(ctx context.Context) []*Task { v, err := tcb.Save(ctx) if err != nil { panic(err) } return v }