Files
ent/examples/fs/ent/file_create.go
Giau. Tran Minh 195be2d98d entc/gen: fixed unnamed field initialization (#2648)
* entc/gen: fixed ConstraintError fields name

* fix: run go generate

* entc/gen: fixed Filter fields name

* fix: run go generate again for entql
2022-06-14 12:32:46 +03:00

339 lines
8.4 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 ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/examples/fs/ent/file"
"entgo.io/ent/schema/field"
)
// FileCreate is the builder for creating a File entity.
type FileCreate struct {
config
mutation *FileMutation
hooks []Hook
}
// SetName sets the "name" field.
func (fc *FileCreate) SetName(s string) *FileCreate {
fc.mutation.SetName(s)
return fc
}
// SetDeleted sets the "deleted" field.
func (fc *FileCreate) SetDeleted(b bool) *FileCreate {
fc.mutation.SetDeleted(b)
return fc
}
// SetNillableDeleted sets the "deleted" field if the given value is not nil.
func (fc *FileCreate) SetNillableDeleted(b *bool) *FileCreate {
if b != nil {
fc.SetDeleted(*b)
}
return fc
}
// SetParentID sets the "parent_id" field.
func (fc *FileCreate) SetParentID(i int) *FileCreate {
fc.mutation.SetParentID(i)
return fc
}
// SetNillableParentID sets the "parent_id" field if the given value is not nil.
func (fc *FileCreate) SetNillableParentID(i *int) *FileCreate {
if i != nil {
fc.SetParentID(*i)
}
return fc
}
// SetParent sets the "parent" edge to the File entity.
func (fc *FileCreate) SetParent(f *File) *FileCreate {
return fc.SetParentID(f.ID)
}
// AddChildIDs adds the "children" edge to the File entity by IDs.
func (fc *FileCreate) AddChildIDs(ids ...int) *FileCreate {
fc.mutation.AddChildIDs(ids...)
return fc
}
// AddChildren adds the "children" edges to the File entity.
func (fc *FileCreate) AddChildren(f ...*File) *FileCreate {
ids := make([]int, len(f))
for i := range f {
ids[i] = f[i].ID
}
return fc.AddChildIDs(ids...)
}
// Mutation returns the FileMutation object of the builder.
func (fc *FileCreate) Mutation() *FileMutation {
return fc.mutation
}
// Save creates the File in the database.
func (fc *FileCreate) Save(ctx context.Context) (*File, error) {
var (
err error
node *File
)
fc.defaults()
if len(fc.hooks) == 0 {
if err = fc.check(); err != nil {
return nil, err
}
node, err = fc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*FileMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = fc.check(); err != nil {
return nil, err
}
fc.mutation = mutation
if node, err = fc.sqlSave(ctx); err != nil {
return nil, err
}
mutation.id = &node.ID
mutation.done = true
return node, err
})
for i := len(fc.hooks) - 1; i >= 0; i-- {
if fc.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = fc.hooks[i](mut)
}
v, err := mut.Mutate(ctx, fc.mutation)
if err != nil {
return nil, err
}
nv, ok := v.(*File)
if !ok {
return nil, fmt.Errorf("unexpected node type %T returned from FileMutation", v)
}
node = nv
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (fc *FileCreate) SaveX(ctx context.Context) *File {
v, err := fc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (fc *FileCreate) Exec(ctx context.Context) error {
_, err := fc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (fc *FileCreate) ExecX(ctx context.Context) {
if err := fc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (fc *FileCreate) defaults() {
if _, ok := fc.mutation.Deleted(); !ok {
v := file.DefaultDeleted
fc.mutation.SetDeleted(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (fc *FileCreate) check() error {
if _, ok := fc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "File.name"`)}
}
if _, ok := fc.mutation.Deleted(); !ok {
return &ValidationError{Name: "deleted", err: errors.New(`ent: missing required field "File.deleted"`)}
}
return nil
}
func (fc *FileCreate) sqlSave(ctx context.Context) (*File, error) {
_node, _spec := fc.createSpec()
if err := sqlgraph.CreateNode(ctx, fc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
return _node, nil
}
func (fc *FileCreate) createSpec() (*File, *sqlgraph.CreateSpec) {
var (
_node = &File{config: fc.config}
_spec = &sqlgraph.CreateSpec{
Table: file.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: file.FieldID,
},
}
)
if value, ok := fc.mutation.Name(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: file.FieldName,
})
_node.Name = value
}
if value, ok := fc.mutation.Deleted(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBool,
Value: value,
Column: file.FieldDeleted,
})
_node.Deleted = value
}
if nodes := fc.mutation.ParentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: file.ParentTable,
Columns: []string{file.ParentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: file.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.ParentID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := fc.mutation.ChildrenIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: file.ChildrenTable,
Columns: []string{file.ChildrenColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: file.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// FileCreateBulk is the builder for creating many File entities in bulk.
type FileCreateBulk struct {
config
builders []*FileCreate
}
// Save creates the File entities in the database.
func (fcb *FileCreateBulk) Save(ctx context.Context) ([]*File, error) {
specs := make([]*sqlgraph.CreateSpec, len(fcb.builders))
nodes := make([]*File, len(fcb.builders))
mutators := make([]Mutator, len(fcb.builders))
for i := range fcb.builders {
func(i int, root context.Context) {
builder := fcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*FileMutation)
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, fcb.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, fcb.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
if specs[i].ID.Value != nil {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(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, fcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (fcb *FileCreateBulk) SaveX(ctx context.Context) []*File {
v, err := fcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (fcb *FileCreateBulk) Exec(ctx context.Context) error {
_, err := fcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (fcb *FileCreateBulk) ExecX(ctx context.Context) {
if err := fcb.Exec(ctx); err != nil {
panic(err)
}
}