entc/gen: allow setting required fields as nillable

This commit is contained in:
Ariel Mashraki
2022-08-13 18:12:53 +03:00
committed by Ariel Mashraki
parent c8fc927f0d
commit b207429f15
17 changed files with 441 additions and 9 deletions

View File

@@ -10,6 +10,7 @@ import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
@@ -47,6 +48,20 @@ func (tc *TaskCreate) SetPriorities(m map[string]task.Priority) *TaskCreate {
return tc
}
// SetCreatedAt sets the "created_at" field.
func (tc *TaskCreate) SetCreatedAt(t time.Time) *TaskCreate {
tc.mutation.SetCreatedAt(t)
return tc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (tc *TaskCreate) SetNillableCreatedAt(t *time.Time) *TaskCreate {
if t != nil {
tc.SetCreatedAt(*t)
}
return tc
}
// Mutation returns the TaskMutation object of the builder.
func (tc *TaskCreate) Mutation() *TaskMutation {
return tc.mutation
@@ -128,6 +143,10 @@ func (tc *TaskCreate) defaults() {
v := enttask.DefaultPriority
tc.mutation.SetPriority(v)
}
if _, ok := tc.mutation.CreatedAt(); !ok {
v := enttask.DefaultCreatedAt()
tc.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
@@ -140,6 +159,9 @@ func (tc *TaskCreate) check() error {
return &ValidationError{Name: "priority", err: fmt.Errorf(`ent: validator failed for field "Task.priority": %w`, err)}
}
}
if _, ok := tc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Task.created_at"`)}
}
return nil
}
@@ -184,6 +206,14 @@ func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) {
})
_node.Priorities = value
}
if value, ok := tc.mutation.CreatedAt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: enttask.FieldCreatedAt,
})
_node.CreatedAt = &value
}
return _node, _spec
}
@@ -272,6 +302,18 @@ func (u *TaskUpsert) ClearPriorities() *TaskUpsert {
return u
}
// SetCreatedAt sets the "created_at" field.
func (u *TaskUpsert) SetCreatedAt(v time.Time) *TaskUpsert {
u.Set(enttask.FieldCreatedAt, v)
return u
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TaskUpsert) UpdateCreatedAt() *TaskUpsert {
u.SetExcluded(enttask.FieldCreatedAt)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
@@ -282,6 +324,11 @@ func (u *TaskUpsert) ClearPriorities() *TaskUpsert {
// Exec(ctx)
func (u *TaskUpsertOne) UpdateNewValues() *TaskUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(enttask.FieldCreatedAt)
}
}))
return u
}
@@ -354,6 +401,20 @@ func (u *TaskUpsertOne) ClearPriorities() *TaskUpsertOne {
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TaskUpsertOne) SetCreatedAt(v time.Time) *TaskUpsertOne {
return u.Update(func(s *TaskUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TaskUpsertOne) UpdateCreatedAt() *TaskUpsertOne {
return u.Update(func(s *TaskUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TaskUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
@@ -524,6 +585,13 @@ type TaskUpsertBulk struct {
// Exec(ctx)
func (u *TaskUpsertBulk) UpdateNewValues() *TaskUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
for _, b := range u.create.builders {
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(enttask.FieldCreatedAt)
}
}
}))
return u
}
@@ -596,6 +664,20 @@ func (u *TaskUpsertBulk) ClearPriorities() *TaskUpsertBulk {
})
}
// SetCreatedAt sets the "created_at" field.
func (u *TaskUpsertBulk) SetCreatedAt(v time.Time) *TaskUpsertBulk {
return u.Update(func(s *TaskUpsert) {
s.SetCreatedAt(v)
})
}
// UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
func (u *TaskUpsertBulk) UpdateCreatedAt() *TaskUpsertBulk {
return u.Update(func(s *TaskUpsert) {
s.UpdateCreatedAt()
})
}
// Exec executes the query.
func (u *TaskUpsertBulk) Exec(ctx context.Context) error {
for i, b := range u.create.builders {