mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
This introduces the concept of validation errors, where we have a high
level validation error which wraps a more detailed error message.
The higher level `ValidationError` is set up in the generated files, much
like the `NotFoundError` and `ConstraintError` and is accompanied by an
`IsValidationError` check method. Thus, it can be used as follows:
```go
t, err := tx.Team.Create().SetName(input.Name).Save(ctx)
if ent.IsValidationError(err) {
// handle validation error response
}
```
192 lines
4.7 KiB
Go
192 lines
4.7 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"
|
|
"fmt"
|
|
|
|
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebookincubator/ent/examples/m2mrecur/ent/user"
|
|
"github.com/facebookincubator/ent/schema/field"
|
|
)
|
|
|
|
// UserCreate is the builder for creating a User entity.
|
|
type UserCreate struct {
|
|
config
|
|
mutation *UserMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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...)
|
|
}
|
|
|
|
// 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 _, ok := uc.mutation.Age(); !ok {
|
|
return nil, &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
|
|
}
|
|
if _, ok := uc.mutation.Name(); !ok {
|
|
return nil, &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
|
}
|
|
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) 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, 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 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 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
|
|
}
|