mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
256 lines
5.9 KiB
Go
256 lines
5.9 KiB
Go
// Copyright (c) Facebook, Inc. and its affiliates. 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"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebookincubator/ent/examples/traversal/ent/group"
|
|
"github.com/facebookincubator/ent/examples/traversal/ent/pet"
|
|
"github.com/facebookincubator/ent/examples/traversal/ent/user"
|
|
"github.com/facebookincubator/ent/schema/field"
|
|
)
|
|
|
|
// UserCreate is the builder for creating a User entity.
|
|
type UserCreate struct {
|
|
config
|
|
age *int
|
|
name *string
|
|
pets map[int]struct{}
|
|
friends map[int]struct{}
|
|
groups map[int]struct{}
|
|
manage map[int]struct{}
|
|
}
|
|
|
|
// SetAge sets the age field.
|
|
func (uc *UserCreate) SetAge(i int) *UserCreate {
|
|
uc.age = &i
|
|
return uc
|
|
}
|
|
|
|
// SetName sets the name field.
|
|
func (uc *UserCreate) SetName(s string) *UserCreate {
|
|
uc.name = &s
|
|
return uc
|
|
}
|
|
|
|
// AddPetIDs adds the pets edge to Pet by ids.
|
|
func (uc *UserCreate) AddPetIDs(ids ...int) *UserCreate {
|
|
if uc.pets == nil {
|
|
uc.pets = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uc.pets[ids[i]] = struct{}{}
|
|
}
|
|
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...)
|
|
}
|
|
|
|
// AddFriendIDs adds the friends edge to User by ids.
|
|
func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate {
|
|
if uc.friends == nil {
|
|
uc.friends = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uc.friends[ids[i]] = struct{}{}
|
|
}
|
|
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...)
|
|
}
|
|
|
|
// AddGroupIDs adds the groups edge to Group by ids.
|
|
func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate {
|
|
if uc.groups == nil {
|
|
uc.groups = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uc.groups[ids[i]] = struct{}{}
|
|
}
|
|
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...)
|
|
}
|
|
|
|
// AddManageIDs adds the manage edge to Group by ids.
|
|
func (uc *UserCreate) AddManageIDs(ids ...int) *UserCreate {
|
|
if uc.manage == nil {
|
|
uc.manage = make(map[int]struct{})
|
|
}
|
|
for i := range ids {
|
|
uc.manage[ids[i]] = struct{}{}
|
|
}
|
|
return uc
|
|
}
|
|
|
|
// AddManage adds the manage edges to Group.
|
|
func (uc *UserCreate) AddManage(g ...*Group) *UserCreate {
|
|
ids := make([]int, len(g))
|
|
for i := range g {
|
|
ids[i] = g[i].ID
|
|
}
|
|
return uc.AddManageIDs(ids...)
|
|
}
|
|
|
|
// Save creates the User in the database.
|
|
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
|
if uc.age == nil {
|
|
return nil, errors.New("ent: missing required field \"age\"")
|
|
}
|
|
if uc.name == nil {
|
|
return nil, errors.New("ent: missing required field \"name\"")
|
|
}
|
|
return uc.sqlSave(ctx)
|
|
}
|
|
|
|
// 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) sqlSave(ctx context.Context) (*User, error) {
|
|
var (
|
|
u = &User{config: uc.config}
|
|
spec = &sqlgraph.CreateSpec{
|
|
Table: user.Table,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: user.FieldID,
|
|
},
|
|
}
|
|
)
|
|
if value := uc.age; value != nil {
|
|
spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Value: *value,
|
|
Column: user.FieldAge,
|
|
})
|
|
u.Age = *value
|
|
}
|
|
if value := uc.name; value != nil {
|
|
spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: *value,
|
|
Column: user.FieldName,
|
|
})
|
|
u.Name = *value
|
|
}
|
|
if nodes := uc.pets; 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.friends; 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.groups; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
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.manage; len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: true,
|
|
Table: user.ManageTable,
|
|
Columns: []string{user.ManageColumn},
|
|
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 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
|
|
}
|