entc/gen: skip checking required fields with db-based default values (#3204)

This commit is contained in:
Ariel Mashraki
2023-01-02 22:59:27 +02:00
committed by GitHub
parent 41bf915604
commit 542f36d4ab
12 changed files with 298 additions and 9 deletions

View File

@@ -52,6 +52,8 @@ type BlogMutation struct {
op Op
typ string
id *int
oid *int
addoid *int
clearedFields map[string]struct{}
admins map[int]struct{}
removedadmins map[int]struct{}
@@ -165,6 +167,62 @@ func (m *BlogMutation) IDs(ctx context.Context) ([]int, error) {
}
}
// SetOid sets the "oid" field.
func (m *BlogMutation) SetOid(i int) {
m.oid = &i
m.addoid = nil
}
// Oid returns the value of the "oid" field in the mutation.
func (m *BlogMutation) Oid() (r int, exists bool) {
v := m.oid
if v == nil {
return
}
return *v, true
}
// OldOid returns the old "oid" field's value of the Blog entity.
// If the Blog 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 *BlogMutation) OldOid(ctx context.Context) (v int, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldOid is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldOid requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldOid: %w", err)
}
return oldValue.Oid, nil
}
// AddOid adds i to the "oid" field.
func (m *BlogMutation) AddOid(i int) {
if m.addoid != nil {
*m.addoid += i
} else {
m.addoid = &i
}
}
// AddedOid returns the value that was added to the "oid" field in this mutation.
func (m *BlogMutation) AddedOid() (r int, exists bool) {
v := m.addoid
if v == nil {
return
}
return *v, true
}
// ResetOid resets all changes to the "oid" field.
func (m *BlogMutation) ResetOid() {
m.oid = nil
m.addoid = nil
}
// AddAdminIDs adds the "admins" edge to the User entity by ids.
func (m *BlogMutation) AddAdminIDs(ids ...int) {
if m.admins == nil {
@@ -253,7 +311,10 @@ func (m *BlogMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *BlogMutation) Fields() []string {
fields := make([]string, 0, 0)
fields := make([]string, 0, 1)
if m.oid != nil {
fields = append(fields, blog.FieldOid)
}
return fields
}
@@ -261,6 +322,10 @@ func (m *BlogMutation) Fields() []string {
// return value indicates that this field was not set, or was not defined in the
// schema.
func (m *BlogMutation) Field(name string) (ent.Value, bool) {
switch name {
case blog.FieldOid:
return m.Oid()
}
return nil, false
}
@@ -268,6 +333,10 @@ func (m *BlogMutation) Field(name string) (ent.Value, bool) {
// returned if the mutation operation is not UpdateOne, or the query to the
// database failed.
func (m *BlogMutation) OldField(ctx context.Context, name string) (ent.Value, error) {
switch name {
case blog.FieldOid:
return m.OldOid(ctx)
}
return nil, fmt.Errorf("unknown Blog field %s", name)
}
@@ -276,6 +345,13 @@ func (m *BlogMutation) OldField(ctx context.Context, name string) (ent.Value, er
// type.
func (m *BlogMutation) SetField(name string, value ent.Value) error {
switch name {
case blog.FieldOid:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetOid(v)
return nil
}
return fmt.Errorf("unknown Blog field %s", name)
}
@@ -283,13 +359,21 @@ func (m *BlogMutation) SetField(name string, value ent.Value) error {
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *BlogMutation) AddedFields() []string {
return nil
var fields []string
if m.addoid != nil {
fields = append(fields, blog.FieldOid)
}
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 *BlogMutation) AddedField(name string) (ent.Value, bool) {
switch name {
case blog.FieldOid:
return m.AddedOid()
}
return nil, false
}
@@ -297,6 +381,15 @@ func (m *BlogMutation) AddedField(name string) (ent.Value, bool) {
// the field is not defined in the schema, or if the type mismatched the field
// type.
func (m *BlogMutation) AddField(name string, value ent.Value) error {
switch name {
case blog.FieldOid:
v, ok := value.(int)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.AddOid(v)
return nil
}
return fmt.Errorf("unknown Blog numeric field %s", name)
}
@@ -322,6 +415,11 @@ func (m *BlogMutation) ClearField(name string) error {
// 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 *BlogMutation) ResetField(name string) error {
switch name {
case blog.FieldOid:
m.ResetOid()
return nil
}
return fmt.Errorf("unknown Blog field %s", name)
}