Files
ent/entc/integration/ent/task_create.go
2020-08-30 13:33:10 +03:00

199 lines
5.0 KiB
Go

// 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"
"fmt"
"github.com/facebook/ent/dialect/sql/sqlgraph"
"github.com/facebook/ent/entc/integration/ent/schema"
"github.com/facebook/ent/entc/integration/ent/task"
"github.com/facebook/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) {
if err := tc.preSave(); err != nil {
return nil, err
}
var (
err error
node *Task
)
if len(tc.hooks) == 0 {
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)
}
tc.mutation = mutation
node, err = tc.sqlSave(ctx)
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
}
func (tc *TaskCreate) preSave() error {
if _, ok := tc.mutation.Priority(); !ok {
v := task.DefaultPriority
tc.mutation.SetPriority(v)
}
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) {
t, _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)
t.ID = int(id)
return t, nil
}
func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) {
var (
t = &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,
})
t.Priority = value
}
return t, _spec
}
// TaskCreateBulk is the builder for creating a bulk of Task entities.
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]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
if err := builder.preSave(); err != nil {
return nil, err
}
mutation, ok := m.(*TaskMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
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
}
}
}
mutation.done = true
if err != nil {
return nil, err
}
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 calls Save and panics if Save returns an error.
func (tcb *TaskCreateBulk) SaveX(ctx context.Context) []*Task {
v, err := tcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}