examples/entcpkg: add codegen-hook example (#1356)

This commit is contained in:
Ariel Mashraki
2021-03-19 12:29:51 +02:00
committed by GitHub
parent 4e60827cc0
commit aee6785cba
13 changed files with 717 additions and 11 deletions

View File

@@ -39,6 +39,7 @@ func main() {
Funcs(template.FuncMap{"byName": byName}).
ParseFiles("template/debug.tmpl")),
},
Hooks: []gen.Hook{TagFields("json")},
}, opts...)
if err != nil {
log.Fatalf("running ent codegen: %v", err)
@@ -54,3 +55,17 @@ func byName(g *gen.Graph, name string) (*gen.Type, error) {
}
return nil, fmt.Errorf("node %q was not found in the graph", name)
}
// TagFields tags all fields defined in the schema with the given struct-tag.
func TagFields(name string) gen.Hook {
return func(next gen.Generator) gen.Generator {
return gen.GenerateFunc(func(g *gen.Graph) error {
for _, node := range g.Nodes {
for _, field := range node.Fields {
field.StructTag = fmt.Sprintf("%s:%q", name, field.Name)
}
}
return next.Generate(g)
})
}
}

View File

@@ -4,4 +4,4 @@
package ent
//go:generate go run entc.go
//go:generate go run -mod=mod entc.go

View File

@@ -15,6 +15,8 @@ var (
// UsersColumns holds the columns for the "users" table.
UsersColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "name", Type: field.TypeString, Nullable: true},
{Name: "age", Type: field.TypeInt, Nullable: true},
}
// UsersTable holds the schema information for the "users" table.
UsersTable = &schema.Table{

View File

@@ -11,8 +11,10 @@ import (
"fmt"
"sync"
"entgo.io/ent"
"entgo.io/ent/examples/entcpkg/ent/predicate"
"entgo.io/ent/examples/entcpkg/ent/user"
"entgo.io/ent"
)
const (
@@ -33,6 +35,9 @@ type UserMutation struct {
op Op
typ string
id *int
name *string
age *int
addage *int
clearedFields map[string]struct{}
done bool
oldValue func(context.Context) (*User, error)
@@ -118,6 +123,125 @@ func (m *UserMutation) ID() (id int, exists bool) {
return *m.id, true
}
// SetName sets the "name" field.
func (m *UserMutation) SetName(s string) {
m.name = &s
}
// Name returns the value of the "name" field in the mutation.
func (m *UserMutation) Name() (r string, exists bool) {
v := m.name
if v == nil {
return
}
return *v, true
}
// OldName returns the old "name" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldName(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldName is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldName requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldName: %w", err)
}
return oldValue.Name, nil
}
// ClearName clears the value of the "name" field.
func (m *UserMutation) ClearName() {
m.name = nil
m.clearedFields[user.FieldName] = struct{}{}
}
// NameCleared returns if the "name" field was cleared in this mutation.
func (m *UserMutation) NameCleared() bool {
_, ok := m.clearedFields[user.FieldName]
return ok
}
// ResetName resets all changes to the "name" field.
func (m *UserMutation) ResetName() {
m.name = nil
delete(m.clearedFields, user.FieldName)
}
// SetAge sets the "age" field.
func (m *UserMutation) SetAge(i int) {
m.age = &i
m.addage = nil
}
// Age returns the value of the "age" field in the mutation.
func (m *UserMutation) Age() (r int, exists bool) {
v := m.age
if v == nil {
return
}
return *v, true
}
// OldAge returns the old "age" field's value of the User entity.
// If the User object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UserMutation) OldAge(ctx context.Context) (v int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, fmt.Errorf("OldAge is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, fmt.Errorf("OldAge requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldAge: %w", err)
}
return oldValue.Age, nil
}
// AddAge adds i to the "age" field.
func (m *UserMutation) AddAge(i int) {
if m.addage != nil {
*m.addage += i
} else {
m.addage = &i
}
}
// AddedAge returns the value that was added to the "age" field in this mutation.
func (m *UserMutation) AddedAge() (r int, exists bool) {
v := m.addage
if v == nil {
return
}
return *v, true
}
// ClearAge clears the value of the "age" field.
func (m *UserMutation) ClearAge() {
m.age = nil
m.addage = nil
m.clearedFields[user.FieldAge] = struct{}{}
}
// AgeCleared returns if the "age" field was cleared in this mutation.
func (m *UserMutation) AgeCleared() bool {
_, ok := m.clearedFields[user.FieldAge]
return ok
}
// ResetAge resets all changes to the "age" field.
func (m *UserMutation) ResetAge() {
m.age = nil
m.addage = nil
delete(m.clearedFields, user.FieldAge)
}
// Op returns the operation name.
func (m *UserMutation) Op() Op {
return m.op
@@ -132,7 +256,13 @@ func (m *UserMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *UserMutation) Fields() []string {
fields := make([]string, 0, 0)
fields := make([]string, 0, 2)
if m.name != nil {
fields = append(fields, user.FieldName)
}
if m.age != nil {
fields = append(fields, user.FieldAge)
}
return fields
}
@@ -140,6 +270,12 @@ func (m *UserMutation) Fields() []string {
// return value indicates that this field was not set, or was not defined in the
// schema.
func (m *UserMutation) Field(name string) (ent.Value, bool) {
switch name {
case user.FieldName:
return m.Name()
case user.FieldAge:
return m.Age()
}
return nil, false
}
@@ -147,6 +283,12 @@ func (m *UserMutation) Field(name string) (ent.Value, bool) {
// returned if the mutation operation is not UpdateOne, or the query to the
// database failed.
func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
switch name {
case user.FieldName:
return m.OldName(ctx)
case user.FieldAge:
return m.OldAge(ctx)
}
return nil, fmt.Errorf("unknown User field %s", name)
}
@@ -155,6 +297,20 @@ func (m *UserMutation) OldField(ctx context.Context, name string) (ent.Value, er
// type.
func (m *UserMutation) SetField(name string, value ent.Value) error {
switch name {
case user.FieldName:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetName(v)
return nil
case user.FieldAge:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetAge(v)
return nil
}
return fmt.Errorf("unknown User field %s", name)
}
@@ -162,13 +318,21 @@ func (m *UserMutation) SetField(name string, value ent.Value) error {
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *UserMutation) AddedFields() []string {
return nil
var fields []string
if m.addage != nil {
fields = append(fields, user.FieldAge)
}
return fields
}
// AddedField returns the numeric value that was incremented/decremented on a field
// with the given name. The second boolean return value indicates that this field
// was not set, or was not defined in the schema.
func (m *UserMutation) AddedField(name string) (ent.Value, bool) {
switch name {
case user.FieldAge:
return m.AddedAge()
}
return nil, false
}
@@ -176,13 +340,29 @@ func (m *UserMutation) AddedField(name string) (ent.Value, bool) {
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *UserMutation) AddField(name string, value ent.Value) error {
switch name {
case user.FieldAge:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.AddAge(v)
return nil
}
return fmt.Errorf("unknown User numeric field %s", name)
}
// ClearedFields returns all nullable fields that were cleared during this
// mutation.
func (m *UserMutation) ClearedFields() []string {
return nil
var fields []string
if m.FieldCleared(user.FieldName) {
fields = append(fields, user.FieldName)
}
if m.FieldCleared(user.FieldAge) {
fields = append(fields, user.FieldAge)
}
return fields
}
// FieldCleared returns a boolean indicating if a field with the given name was
@@ -195,12 +375,28 @@ func (m *UserMutation) FieldCleared(name string) bool {
// ClearField clears the value of the field with the given name. It returns an
// error if the field is not defined in the schema.
func (m *UserMutation) ClearField(name string) error {
switch name {
case user.FieldName:
m.ClearName()
return nil
case user.FieldAge:
m.ClearAge()
return nil
}
return fmt.Errorf("unknown User nullable field %s", name)
}
// ResetField resets all changes in the mutation for the field with the given name.
// It returns an error if the field is not defined in the schema.
func (m *UserMutation) ResetField(name string) error {
switch name {
case user.FieldName:
m.ResetName()
return nil
case user.FieldAge:
m.ResetAge()
return nil
}
return fmt.Errorf("unknown User field %s", name)
}

View File

@@ -4,7 +4,10 @@
package schema
import "entgo.io/ent"
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
)
// User holds the schema definition for the User entity.
type User struct {
@@ -13,7 +16,12 @@ type User struct {
// Fields of the User.
func (User) Fields() []ent.Field {
return nil
return []ent.Field{
field.String("name").
Optional(),
field.Int("age").
Optional(),
}
}
// Edges of the User.

View File

@@ -17,9 +17,13 @@ import (
// User is the model entity for the User schema.
type User struct {
config
config `json:"-"`
// ID of the ent.
ID int `json:"id,omitempty"`
// Name holds the value of the "name" field.
Name string `json:"name"`
// Age holds the value of the "age" field.
Age int `json:"age"`
// StaticField defined by templates (titled STATICFIELD).
StaticField string `json:"static_field,omitempty"`
@@ -30,8 +34,10 @@ func (*User) scanValues(columns []string) ([]interface{}, error) {
values := make([]interface{}, len(columns))
for i := range columns {
switch columns[i] {
case user.FieldID:
case user.FieldID, user.FieldAge:
values[i] = &sql.NullInt64{}
case user.FieldName:
values[i] = &sql.NullString{}
default:
return nil, fmt.Errorf("unexpected column %q for type User", columns[i])
}
@@ -53,6 +59,18 @@ func (u *User) assignValues(columns []string, values []interface{}) error {
return fmt.Errorf("unexpected type %T for field id", value)
}
u.ID = int(value.Int64)
case user.FieldName:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field name", values[i])
} else if value.Valid {
u.Name = value.String
}
case user.FieldAge:
if value, ok := values[i].(*sql.NullInt64); !ok {
return fmt.Errorf("unexpected type %T for field age", values[i])
} else if value.Valid {
u.Age = int(value.Int64)
}
}
}
return nil
@@ -81,6 +99,10 @@ func (u *User) String() string {
var builder strings.Builder
builder.WriteString("User(")
builder.WriteString(fmt.Sprintf("id=%v", u.ID))
builder.WriteString(", name=")
builder.WriteString(u.Name)
builder.WriteString(", age=")
builder.WriteString(fmt.Sprintf("%v", u.Age))
builder.WriteByte(')')
return builder.String()
}

View File

@@ -11,6 +11,10 @@ const (
Label = "user"
// FieldID holds the string denoting the id field in the database.
FieldID = "id"
// FieldName holds the string denoting the name field in the database.
FieldName = "name"
// FieldAge holds the string denoting the age field in the database.
FieldAge = "age"
// Table holds the table name of the user in the database.
Table = "users"
)
@@ -18,6 +22,8 @@ const (
// Columns holds all SQL columns for user fields.
var Columns = []string{
FieldID,
FieldName,
FieldAge,
}
// ValidColumn reports if the column name is valid (part of the table columns).

View File

@@ -94,6 +94,235 @@ func IDLTE(id int) predicate.User {
})
}
// Name applies equality check predicate on the "name" field. It's identical to NameEQ.
func Name(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldName), v))
})
}
// Age applies equality check predicate on the "age" field. It's identical to AgeEQ.
func Age(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldAge), v))
})
}
// NameEQ applies the EQ predicate on the "name" field.
func NameEQ(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldName), v))
})
}
// NameNEQ applies the NEQ predicate on the "name" field.
func NameNEQ(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NEQ(s.C(FieldName), v))
})
}
// NameIn applies the In predicate on the "name" field.
func NameIn(vs ...string) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.In(s.C(FieldName), v...))
})
}
// NameNotIn applies the NotIn predicate on the "name" field.
func NameNotIn(vs ...string) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.NotIn(s.C(FieldName), v...))
})
}
// NameGT applies the GT predicate on the "name" field.
func NameGT(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.GT(s.C(FieldName), v))
})
}
// NameGTE applies the GTE predicate on the "name" field.
func NameGTE(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.GTE(s.C(FieldName), v))
})
}
// NameLT applies the LT predicate on the "name" field.
func NameLT(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.LT(s.C(FieldName), v))
})
}
// NameLTE applies the LTE predicate on the "name" field.
func NameLTE(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.LTE(s.C(FieldName), v))
})
}
// NameContains applies the Contains predicate on the "name" field.
func NameContains(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.Contains(s.C(FieldName), v))
})
}
// NameHasPrefix applies the HasPrefix predicate on the "name" field.
func NameHasPrefix(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.HasPrefix(s.C(FieldName), v))
})
}
// NameHasSuffix applies the HasSuffix predicate on the "name" field.
func NameHasSuffix(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.HasSuffix(s.C(FieldName), v))
})
}
// NameIsNil applies the IsNil predicate on the "name" field.
func NameIsNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.IsNull(s.C(FieldName)))
})
}
// NameNotNil applies the NotNil predicate on the "name" field.
func NameNotNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NotNull(s.C(FieldName)))
})
}
// NameEqualFold applies the EqualFold predicate on the "name" field.
func NameEqualFold(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EqualFold(s.C(FieldName), v))
})
}
// NameContainsFold applies the ContainsFold predicate on the "name" field.
func NameContainsFold(v string) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.ContainsFold(s.C(FieldName), v))
})
}
// AgeEQ applies the EQ predicate on the "age" field.
func AgeEQ(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldAge), v))
})
}
// AgeNEQ applies the NEQ predicate on the "age" field.
func AgeNEQ(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NEQ(s.C(FieldAge), v))
})
}
// AgeIn applies the In predicate on the "age" field.
func AgeIn(vs ...int) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.In(s.C(FieldAge), v...))
})
}
// AgeNotIn applies the NotIn predicate on the "age" field.
func AgeNotIn(vs ...int) predicate.User {
v := make([]interface{}, len(vs))
for i := range v {
v[i] = vs[i]
}
return predicate.User(func(s *sql.Selector) {
// if not arguments were provided, append the FALSE constants,
// since we can't apply "IN ()". This will make this predicate falsy.
if len(v) == 0 {
s.Where(sql.False())
return
}
s.Where(sql.NotIn(s.C(FieldAge), v...))
})
}
// AgeGT applies the GT predicate on the "age" field.
func AgeGT(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.GT(s.C(FieldAge), v))
})
}
// AgeGTE applies the GTE predicate on the "age" field.
func AgeGTE(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.GTE(s.C(FieldAge), v))
})
}
// AgeLT applies the LT predicate on the "age" field.
func AgeLT(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.LT(s.C(FieldAge), v))
})
}
// AgeLTE applies the LTE predicate on the "age" field.
func AgeLTE(v int) predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.LTE(s.C(FieldAge), v))
})
}
// AgeIsNil applies the IsNil predicate on the "age" field.
func AgeIsNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.IsNull(s.C(FieldAge)))
})
}
// AgeNotNil applies the NotNil predicate on the "age" field.
func AgeNotNil() predicate.User {
return predicate.User(func(s *sql.Selector) {
s.Where(sql.NotNull(s.C(FieldAge)))
})
}
// And groups predicates with the AND operator between them.
func And(predicates ...predicate.User) predicate.User {
return predicate.User(func(s *sql.Selector) {

View File

@@ -22,6 +22,34 @@ type UserCreate struct {
hooks []Hook
}
// SetName sets the "name" field.
func (uc *UserCreate) SetName(s string) *UserCreate {
uc.mutation.SetName(s)
return uc
}
// SetNillableName sets the "name" field if the given value is not nil.
func (uc *UserCreate) SetNillableName(s *string) *UserCreate {
if s != nil {
uc.SetName(*s)
}
return uc
}
// SetAge sets the "age" field.
func (uc *UserCreate) SetAge(i int) *UserCreate {
uc.mutation.SetAge(i)
return uc
}
// SetNillableAge sets the "age" field if the given value is not nil.
func (uc *UserCreate) SetNillableAge(i *int) *UserCreate {
if i != nil {
uc.SetAge(*i)
}
return uc
}
// Mutation returns the UserMutation object of the builder.
func (uc *UserCreate) Mutation() *UserMutation {
return uc.mutation
@@ -100,6 +128,22 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
},
}
)
if value, ok := uc.mutation.Name(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldName,
})
_node.Name = value
}
if value, ok := uc.mutation.Age(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldAge,
})
_node.Age = value
}
return _node, _spec
}

View File

@@ -245,6 +245,19 @@ func (uq *UserQuery) Clone() *UserQuery {
// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
// var v []struct {
// Name string `json:"name"`
// Count int `json:"count,omitempty"`
// }
//
// client.User.Query().
// GroupBy(user.FieldName).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
//
func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
group := &UserGroupBy{config: uq.config}
group.fields = append([]string{field}, fields...)
@@ -259,6 +272,17 @@ func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
// var v []struct {
// Name string `json:"name"`
// }
//
// client.User.Query().
// Select(user.FieldName).
// Scan(ctx, &v)
//
func (uq *UserQuery) Select(field string, fields ...string) *UserSelect {
uq.fields = append([]string{field}, fields...)
return &UserSelect{UserQuery: uq}

View File

@@ -30,6 +30,53 @@ func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
return uu
}
// SetName sets the "name" field.
func (uu *UserUpdate) SetName(s string) *UserUpdate {
uu.mutation.SetName(s)
return uu
}
// SetNillableName sets the "name" field if the given value is not nil.
func (uu *UserUpdate) SetNillableName(s *string) *UserUpdate {
if s != nil {
uu.SetName(*s)
}
return uu
}
// ClearName clears the value of the "name" field.
func (uu *UserUpdate) ClearName() *UserUpdate {
uu.mutation.ClearName()
return uu
}
// SetAge sets the "age" field.
func (uu *UserUpdate) SetAge(i int) *UserUpdate {
uu.mutation.ResetAge()
uu.mutation.SetAge(i)
return uu
}
// SetNillableAge sets the "age" field if the given value is not nil.
func (uu *UserUpdate) SetNillableAge(i *int) *UserUpdate {
if i != nil {
uu.SetAge(*i)
}
return uu
}
// AddAge adds i to the "age" field.
func (uu *UserUpdate) AddAge(i int) *UserUpdate {
uu.mutation.AddAge(i)
return uu
}
// ClearAge clears the value of the "age" field.
func (uu *UserUpdate) ClearAge() *UserUpdate {
uu.mutation.ClearAge()
return uu
}
// Mutation returns the UserMutation object of the builder.
func (uu *UserUpdate) Mutation() *UserMutation {
return uu.mutation
@@ -104,6 +151,39 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
}
}
}
if value, ok := uu.mutation.Name(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldName,
})
}
if uu.mutation.NameCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldName,
})
}
if value, ok := uu.mutation.Age(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldAge,
})
}
if value, ok := uu.mutation.AddedAge(); ok {
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldAge,
})
}
if uu.mutation.AgeCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldAge,
})
}
if n, err = sqlgraph.UpdateNodes(ctx, uu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{user.Label}
@@ -123,6 +203,53 @@ type UserUpdateOne struct {
mutation *UserMutation
}
// SetName sets the "name" field.
func (uuo *UserUpdateOne) SetName(s string) *UserUpdateOne {
uuo.mutation.SetName(s)
return uuo
}
// SetNillableName sets the "name" field if the given value is not nil.
func (uuo *UserUpdateOne) SetNillableName(s *string) *UserUpdateOne {
if s != nil {
uuo.SetName(*s)
}
return uuo
}
// ClearName clears the value of the "name" field.
func (uuo *UserUpdateOne) ClearName() *UserUpdateOne {
uuo.mutation.ClearName()
return uuo
}
// SetAge sets the "age" field.
func (uuo *UserUpdateOne) SetAge(i int) *UserUpdateOne {
uuo.mutation.ResetAge()
uuo.mutation.SetAge(i)
return uuo
}
// SetNillableAge sets the "age" field if the given value is not nil.
func (uuo *UserUpdateOne) SetNillableAge(i *int) *UserUpdateOne {
if i != nil {
uuo.SetAge(*i)
}
return uuo
}
// AddAge adds i to the "age" field.
func (uuo *UserUpdateOne) AddAge(i int) *UserUpdateOne {
uuo.mutation.AddAge(i)
return uuo
}
// ClearAge clears the value of the "age" field.
func (uuo *UserUpdateOne) ClearAge() *UserUpdateOne {
uuo.mutation.ClearAge()
return uuo
}
// Mutation returns the UserMutation object of the builder.
func (uuo *UserUpdateOne) Mutation() *UserMutation {
return uuo.mutation
@@ -221,6 +348,39 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error)
}
}
}
if value, ok := uuo.mutation.Name(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: user.FieldName,
})
}
if uuo.mutation.NameCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeString,
Column: user.FieldName,
})
}
if value, ok := uuo.mutation.Age(); ok {
_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldAge,
})
}
if value, ok := uuo.mutation.AddedAge(); ok {
_spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: user.FieldAge,
})
}
if uuo.mutation.AgeCleared() {
_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: user.FieldAge,
})
}
_node = &User{config: uuo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues

View File

@@ -28,5 +28,5 @@ func Example_EntcPkg() {
usr := client.User.Create().SaveX(ctx)
fmt.Println("boring user:", usr)
// Output: boring user: User(id=1)
// Output: boring user: User(id=1, name=, age=0)
}