Files
ent/entc/integration/ent/user_create.go
Ariel Mashraki fd0a7f9f02 all: facebookincubator/ent => facebook/ent (#660)
ent repository is going to be migrated to facebook organization
2020-08-18 11:05:08 +03:00

768 lines
19 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"
"errors"
"fmt"
"github.com/facebook/ent/dialect/sql/sqlgraph"
"github.com/facebook/ent/entc/integration/ent/card"
"github.com/facebook/ent/entc/integration/ent/file"
"github.com/facebook/ent/entc/integration/ent/group"
"github.com/facebook/ent/entc/integration/ent/pet"
"github.com/facebook/ent/entc/integration/ent/user"
"github.com/facebook/ent/schema/field"
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
// SetOptionalInt sets the optional_int field.
func (uc *UserCreate) SetOptionalInt(i int) *UserCreate {
uc.mutation.SetOptionalInt(i)
return uc
}
// SetNillableOptionalInt sets the optional_int field if the given value is not nil.
func (uc *UserCreate) SetNillableOptionalInt(i *int) *UserCreate {
if i != nil {
uc.SetOptionalInt(*i)
}
return uc
}
// SetAge sets the age field.
func (uc *UserCreate) SetAge(i int) *UserCreate {
uc.mutation.SetAge(i)
return uc
}
// SetName sets the name field.
func (uc *UserCreate) SetName(s string) *UserCreate {
uc.mutation.SetName(s)
return uc
}
// SetLast sets the last field.
func (uc *UserCreate) SetLast(s string) *UserCreate {
uc.mutation.SetLast(s)
return uc
}
// SetNillableLast sets the last field if the given value is not nil.
func (uc *UserCreate) SetNillableLast(s *string) *UserCreate {
if s != nil {
uc.SetLast(*s)
}
return uc
}
// SetNickname sets the nickname field.
func (uc *UserCreate) SetNickname(s string) *UserCreate {
uc.mutation.SetNickname(s)
return uc
}
// SetNillableNickname sets the nickname field if the given value is not nil.
func (uc *UserCreate) SetNillableNickname(s *string) *UserCreate {
if s != nil {
uc.SetNickname(*s)
}
return uc
}
// SetPhone sets the phone field.
func (uc *UserCreate) SetPhone(s string) *UserCreate {
uc.mutation.SetPhone(s)
return uc
}
// SetNillablePhone sets the phone field if the given value is not nil.
func (uc *UserCreate) SetNillablePhone(s *string) *UserCreate {
if s != nil {
uc.SetPhone(*s)
}
return uc
}
// SetPassword sets the password field.
func (uc *UserCreate) SetPassword(s string) *UserCreate {
uc.mutation.SetPassword(s)
return uc
}
// SetNillablePassword sets the password field if the given value is not nil.
func (uc *UserCreate) SetNillablePassword(s *string) *UserCreate {
if s != nil {
uc.SetPassword(*s)
}
return uc
}
// SetRole sets the role field.
func (uc *UserCreate) SetRole(u user.Role) *UserCreate {
uc.mutation.SetRole(u)
return uc
}
// SetNillableRole sets the role field if the given value is not nil.
func (uc *UserCreate) SetNillableRole(u *user.Role) *UserCreate {
if u != nil {
uc.SetRole(*u)
}
return uc
}
// SetSSOCert sets the SSOCert field.
func (uc *UserCreate) SetSSOCert(s string) *UserCreate {
uc.mutation.SetSSOCert(s)
return uc
}
// SetNillableSSOCert sets the SSOCert field if the given value is not nil.
func (uc *UserCreate) SetNillableSSOCert(s *string) *UserCreate {
if s != nil {
uc.SetSSOCert(*s)
}
return uc
}
// SetCardID sets the card edge to Card by id.
func (uc *UserCreate) SetCardID(id int) *UserCreate {
uc.mutation.SetCardID(id)
return uc
}
// SetNillableCardID sets the card edge to Card by id if the given value is not nil.
func (uc *UserCreate) SetNillableCardID(id *int) *UserCreate {
if id != nil {
uc = uc.SetCardID(*id)
}
return uc
}
// SetCard sets the card edge to Card.
func (uc *UserCreate) SetCard(c *Card) *UserCreate {
return uc.SetCardID(c.ID)
}
// AddPetIDs adds the pets edge to Pet by ids.
func (uc *UserCreate) AddPetIDs(ids ...int) *UserCreate {
uc.mutation.AddPetIDs(ids...)
return uc
}
// AddPets adds the pets edges to Pet.
func (uc *UserCreate) AddPets(p ...*Pet) *UserCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return uc.AddPetIDs(ids...)
}
// AddFileIDs adds the files edge to File by ids.
func (uc *UserCreate) AddFileIDs(ids ...int) *UserCreate {
uc.mutation.AddFileIDs(ids...)
return uc
}
// AddFiles adds the files edges to File.
func (uc *UserCreate) AddFiles(f ...*File) *UserCreate {
ids := make([]int, len(f))
for i := range f {
ids[i] = f[i].ID
}
return uc.AddFileIDs(ids...)
}
// AddGroupIDs adds the groups edge to Group by ids.
func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate {
uc.mutation.AddGroupIDs(ids...)
return uc
}
// AddGroups adds the groups edges to Group.
func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate {
ids := make([]int, len(g))
for i := range g {
ids[i] = g[i].ID
}
return uc.AddGroupIDs(ids...)
}
// AddFriendIDs adds the friends edge to User by ids.
func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate {
uc.mutation.AddFriendIDs(ids...)
return uc
}
// AddFriends adds the friends edges to User.
func (uc *UserCreate) AddFriends(u ...*User) *UserCreate {
ids := make([]int, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFriendIDs(ids...)
}
// AddFollowerIDs adds the followers edge to User by ids.
func (uc *UserCreate) AddFollowerIDs(ids ...int) *UserCreate {
uc.mutation.AddFollowerIDs(ids...)
return uc
}
// AddFollowers adds the followers edges to User.
func (uc *UserCreate) AddFollowers(u ...*User) *UserCreate {
ids := make([]int, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFollowerIDs(ids...)
}
// AddFollowingIDs adds the following edge to User by ids.
func (uc *UserCreate) AddFollowingIDs(ids ...int) *UserCreate {
uc.mutation.AddFollowingIDs(ids...)
return uc
}
// AddFollowing adds the following edges to User.
func (uc *UserCreate) AddFollowing(u ...*User) *UserCreate {
ids := make([]int, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddFollowingIDs(ids...)
}
// SetTeamID sets the team edge to Pet by id.
func (uc *UserCreate) SetTeamID(id int) *UserCreate {
uc.mutation.SetTeamID(id)
return uc
}
// SetNillableTeamID sets the team edge to Pet by id if the given value is not nil.
func (uc *UserCreate) SetNillableTeamID(id *int) *UserCreate {
if id != nil {
uc = uc.SetTeamID(*id)
}
return uc
}
// SetTeam sets the team edge to Pet.
func (uc *UserCreate) SetTeam(p *Pet) *UserCreate {
return uc.SetTeamID(p.ID)
}
// SetSpouseID sets the spouse edge to User by id.
func (uc *UserCreate) SetSpouseID(id int) *UserCreate {
uc.mutation.SetSpouseID(id)
return uc
}
// SetNillableSpouseID sets the spouse edge to User by id if the given value is not nil.
func (uc *UserCreate) SetNillableSpouseID(id *int) *UserCreate {
if id != nil {
uc = uc.SetSpouseID(*id)
}
return uc
}
// SetSpouse sets the spouse edge to User.
func (uc *UserCreate) SetSpouse(u *User) *UserCreate {
return uc.SetSpouseID(u.ID)
}
// AddChildIDs adds the children edge to User by ids.
func (uc *UserCreate) AddChildIDs(ids ...int) *UserCreate {
uc.mutation.AddChildIDs(ids...)
return uc
}
// AddChildren adds the children edges to User.
func (uc *UserCreate) AddChildren(u ...*User) *UserCreate {
ids := make([]int, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddChildIDs(ids...)
}
// SetParentID sets the parent edge to User by id.
func (uc *UserCreate) SetParentID(id int) *UserCreate {
uc.mutation.SetParentID(id)
return uc
}
// SetNillableParentID sets the parent edge to User by id if the given value is not nil.
func (uc *UserCreate) SetNillableParentID(id *int) *UserCreate {
if id != nil {
uc = uc.SetParentID(*id)
}
return uc
}
// SetParent sets the parent edge to User.
func (uc *UserCreate) SetParent(u *User) *UserCreate {
return uc.SetParentID(u.ID)
}
// Mutation returns the UserMutation object of the builder.
func (uc *UserCreate) Mutation() *UserMutation {
return uc.mutation
}
// Save creates the User in the database.
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
if err := uc.preSave(); err != nil {
return nil, err
}
var (
err error
node *User
)
if len(uc.hooks) == 0 {
node, err = uc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*UserMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
uc.mutation = mutation
node, err = uc.sqlSave(ctx)
mutation.done = true
return node, err
})
for i := len(uc.hooks) - 1; i >= 0; i-- {
mut = uc.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, uc.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (uc *UserCreate) SaveX(ctx context.Context) *User {
v, err := uc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (uc *UserCreate) preSave() error {
if v, ok := uc.mutation.OptionalInt(); ok {
if err := user.OptionalIntValidator(v); err != nil {
return &ValidationError{Name: "optional_int", err: fmt.Errorf("ent: validator failed for field \"optional_int\": %w", err)}
}
}
if _, ok := uc.mutation.Age(); !ok {
return &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
}
if _, ok := uc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
}
if _, ok := uc.mutation.Last(); !ok {
v := user.DefaultLast
uc.mutation.SetLast(v)
}
if _, ok := uc.mutation.Role(); !ok {
v := user.DefaultRole
uc.mutation.SetRole(v)
}
if v, ok := uc.mutation.Role(); ok {
if err := user.RoleValidator(v); err != nil {
return &ValidationError{Name: "role", err: fmt.Errorf("ent: validator failed for field \"role\": %w", err)}
}
}
return nil
}
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
u, _spec := uc.createSpec()
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
u.ID = int(id)
return u, nil
}
func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
var (
u = &User{config: uc.config}
_spec = &sqlgraph.CreateSpec{
Table: user.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
}
)
if value, ok := uc.mutation.OptionalInt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldOptionalInt,
})
u.OptionalInt = value
}
if value, ok := uc.mutation.Age(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldAge,
})
u.Age = value
}
if value, ok := uc.mutation.Name(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldName,
})
u.Name = value
}
if value, ok := uc.mutation.Last(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldLast,
})
u.Last = value
}
if value, ok := uc.mutation.Nickname(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldNickname,
})
u.Nickname = value
}
if value, ok := uc.mutation.Phone(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldPhone,
})
u.Phone = value
}
if value, ok := uc.mutation.Password(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldPassword,
})
u.Password = value
}
if value, ok := uc.mutation.Role(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeEnum,
Value: value,
Column: user.FieldRole,
})
u.Role = value
}
if value, ok := uc.mutation.SSOCert(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldSSOCert,
})
u.SSOCert = value
}
if nodes := uc.mutation.CardIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: false,
Table: user.CardTable,
Columns: []string{user.CardColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: card.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.PetsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.PetsTable,
Columns: []string{user.PetsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: pet.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.FilesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.FilesTable,
Columns: []string{user.FilesColumn},
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)
}
if nodes := uc.mutation.GroupsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: user.GroupsTable,
Columns: user.GroupsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: group.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.FriendsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: user.FriendsTable,
Columns: user.FriendsPrimaryKey,
Bidi: true,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.FollowersIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: user.FollowersTable,
Columns: user.FollowersPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.FollowingIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: user.FollowingTable,
Columns: user.FollowingPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.TeamIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: false,
Table: user.TeamTable,
Columns: []string{user.TeamColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: pet.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.SpouseIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: false,
Table: user.SpouseTable,
Columns: []string{user.SpouseColumn},
Bidi: true,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.ChildrenIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: user.ChildrenTable,
Columns: []string{user.ChildrenColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.ParentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: user.ParentTable,
Columns: []string{user.ParentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return u, _spec
}
// UserCreateBulk is the builder for creating a bulk of User entities.
type UserCreateBulk struct {
config
builders []*UserCreate
}
// Save creates the User entities in the database.
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
nodes := make([]*User, len(ucb.builders))
mutators := make([]Mutator, len(ucb.builders))
for i := range ucb.builders {
func(i int, root context.Context) {
builder := ucb.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.(*UserMutation)
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, ucb.builders[i+1].mutation)
} else {
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, ucb.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, ucb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX calls Save and panics if Save returns an error.
func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
v, err := ucb.Save(ctx)
if err != nil {
panic(err)
}
return v
}