// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/predicate"
	"github.com/anzhiyu-c/anheyu-app/ent/storagepolicy"
	"github.com/anzhiyu-c/anheyu-app/pkg/domain/model"
)

// StoragePolicyUpdate is the builder for updating StoragePolicy entities.
type StoragePolicyUpdate struct {
	config
	hooks     []Hook
	mutation  *StoragePolicyMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the StoragePolicyUpdate builder.
func (_u *StoragePolicyUpdate) Where(ps ...predicate.StoragePolicy) *StoragePolicyUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *StoragePolicyUpdate) SetDeletedAt(v time.Time) *StoragePolicyUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableDeletedAt(v *time.Time) *StoragePolicyUpdate {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *StoragePolicyUpdate) ClearDeletedAt() *StoragePolicyUpdate {
	_u.mutation.ClearDeletedAt()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *StoragePolicyUpdate) SetUpdatedAt(v time.Time) *StoragePolicyUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetName sets the "name" field.
func (_u *StoragePolicyUpdate) SetName(v string) *StoragePolicyUpdate {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableName(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetType sets the "type" field.
func (_u *StoragePolicyUpdate) SetType(v string) *StoragePolicyUpdate {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableType(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetFlag sets the "flag" field.
func (_u *StoragePolicyUpdate) SetFlag(v string) *StoragePolicyUpdate {
	_u.mutation.SetFlag(v)
	return _u
}

// SetNillableFlag sets the "flag" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableFlag(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetFlag(*v)
	}
	return _u
}

// ClearFlag clears the value of the "flag" field.
func (_u *StoragePolicyUpdate) ClearFlag() *StoragePolicyUpdate {
	_u.mutation.ClearFlag()
	return _u
}

// SetServer sets the "server" field.
func (_u *StoragePolicyUpdate) SetServer(v string) *StoragePolicyUpdate {
	_u.mutation.SetServer(v)
	return _u
}

// SetNillableServer sets the "server" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableServer(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetServer(*v)
	}
	return _u
}

// ClearServer clears the value of the "server" field.
func (_u *StoragePolicyUpdate) ClearServer() *StoragePolicyUpdate {
	_u.mutation.ClearServer()
	return _u
}

// SetBucketName sets the "bucket_name" field.
func (_u *StoragePolicyUpdate) SetBucketName(v string) *StoragePolicyUpdate {
	_u.mutation.SetBucketName(v)
	return _u
}

// SetNillableBucketName sets the "bucket_name" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableBucketName(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetBucketName(*v)
	}
	return _u
}

// ClearBucketName clears the value of the "bucket_name" field.
func (_u *StoragePolicyUpdate) ClearBucketName() *StoragePolicyUpdate {
	_u.mutation.ClearBucketName()
	return _u
}

// SetIsPrivate sets the "is_private" field.
func (_u *StoragePolicyUpdate) SetIsPrivate(v bool) *StoragePolicyUpdate {
	_u.mutation.SetIsPrivate(v)
	return _u
}

// SetNillableIsPrivate sets the "is_private" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableIsPrivate(v *bool) *StoragePolicyUpdate {
	if v != nil {
		_u.SetIsPrivate(*v)
	}
	return _u
}

// ClearIsPrivate clears the value of the "is_private" field.
func (_u *StoragePolicyUpdate) ClearIsPrivate() *StoragePolicyUpdate {
	_u.mutation.ClearIsPrivate()
	return _u
}

// SetAccessKey sets the "access_key" field.
func (_u *StoragePolicyUpdate) SetAccessKey(v string) *StoragePolicyUpdate {
	_u.mutation.SetAccessKey(v)
	return _u
}

// SetNillableAccessKey sets the "access_key" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableAccessKey(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetAccessKey(*v)
	}
	return _u
}

// ClearAccessKey clears the value of the "access_key" field.
func (_u *StoragePolicyUpdate) ClearAccessKey() *StoragePolicyUpdate {
	_u.mutation.ClearAccessKey()
	return _u
}

// SetSecretKey sets the "secret_key" field.
func (_u *StoragePolicyUpdate) SetSecretKey(v string) *StoragePolicyUpdate {
	_u.mutation.SetSecretKey(v)
	return _u
}

// SetNillableSecretKey sets the "secret_key" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableSecretKey(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetSecretKey(*v)
	}
	return _u
}

// ClearSecretKey clears the value of the "secret_key" field.
func (_u *StoragePolicyUpdate) ClearSecretKey() *StoragePolicyUpdate {
	_u.mutation.ClearSecretKey()
	return _u
}

// SetMaxSize sets the "max_size" field.
func (_u *StoragePolicyUpdate) SetMaxSize(v int64) *StoragePolicyUpdate {
	_u.mutation.ResetMaxSize()
	_u.mutation.SetMaxSize(v)
	return _u
}

// SetNillableMaxSize sets the "max_size" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableMaxSize(v *int64) *StoragePolicyUpdate {
	if v != nil {
		_u.SetMaxSize(*v)
	}
	return _u
}

// AddMaxSize adds value to the "max_size" field.
func (_u *StoragePolicyUpdate) AddMaxSize(v int64) *StoragePolicyUpdate {
	_u.mutation.AddMaxSize(v)
	return _u
}

// ClearMaxSize clears the value of the "max_size" field.
func (_u *StoragePolicyUpdate) ClearMaxSize() *StoragePolicyUpdate {
	_u.mutation.ClearMaxSize()
	return _u
}

// SetBasePath sets the "base_path" field.
func (_u *StoragePolicyUpdate) SetBasePath(v string) *StoragePolicyUpdate {
	_u.mutation.SetBasePath(v)
	return _u
}

// SetNillableBasePath sets the "base_path" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableBasePath(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetBasePath(*v)
	}
	return _u
}

// ClearBasePath clears the value of the "base_path" field.
func (_u *StoragePolicyUpdate) ClearBasePath() *StoragePolicyUpdate {
	_u.mutation.ClearBasePath()
	return _u
}

// SetVirtualPath sets the "virtual_path" field.
func (_u *StoragePolicyUpdate) SetVirtualPath(v string) *StoragePolicyUpdate {
	_u.mutation.SetVirtualPath(v)
	return _u
}

// SetNillableVirtualPath sets the "virtual_path" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableVirtualPath(v *string) *StoragePolicyUpdate {
	if v != nil {
		_u.SetVirtualPath(*v)
	}
	return _u
}

// ClearVirtualPath clears the value of the "virtual_path" field.
func (_u *StoragePolicyUpdate) ClearVirtualPath() *StoragePolicyUpdate {
	_u.mutation.ClearVirtualPath()
	return _u
}

// SetSettings sets the "settings" field.
func (_u *StoragePolicyUpdate) SetSettings(v model.StoragePolicySettings) *StoragePolicyUpdate {
	_u.mutation.SetSettings(v)
	return _u
}

// ClearSettings clears the value of the "settings" field.
func (_u *StoragePolicyUpdate) ClearSettings() *StoragePolicyUpdate {
	_u.mutation.ClearSettings()
	return _u
}

// SetNodeID sets the "node_id" field.
func (_u *StoragePolicyUpdate) SetNodeID(v uint) *StoragePolicyUpdate {
	_u.mutation.ResetNodeID()
	_u.mutation.SetNodeID(v)
	return _u
}

// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (_u *StoragePolicyUpdate) SetNillableNodeID(v *uint) *StoragePolicyUpdate {
	if v != nil {
		_u.SetNodeID(*v)
	}
	return _u
}

// AddNodeID adds value to the "node_id" field.
func (_u *StoragePolicyUpdate) AddNodeID(v int) *StoragePolicyUpdate {
	_u.mutation.AddNodeID(v)
	return _u
}

// ClearNodeID clears the value of the "node_id" field.
func (_u *StoragePolicyUpdate) ClearNodeID() *StoragePolicyUpdate {
	_u.mutation.ClearNodeID()
	return _u
}

// Mutation returns the StoragePolicyMutation object of the builder.
func (_u *StoragePolicyUpdate) Mutation() *StoragePolicyMutation {
	return _u.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *StoragePolicyUpdate) Save(ctx context.Context) (int, error) {
	if err := _u.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *StoragePolicyUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *StoragePolicyUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *StoragePolicyUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *StoragePolicyUpdate) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if storagepolicy.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized storagepolicy.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := storagepolicy.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *StoragePolicyUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := storagepolicy.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.GetType(); ok {
		if err := storagepolicy.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Flag(); ok {
		if err := storagepolicy.FlagValidator(v); err != nil {
			return &ValidationError{Name: "flag", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.flag": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Server(); ok {
		if err := storagepolicy.ServerValidator(v); err != nil {
			return &ValidationError{Name: "server", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.server": %w`, err)}
		}
	}
	if v, ok := _u.mutation.BucketName(); ok {
		if err := storagepolicy.BucketNameValidator(v); err != nil {
			return &ValidationError{Name: "bucket_name", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.bucket_name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.BasePath(); ok {
		if err := storagepolicy.BasePathValidator(v); err != nil {
			return &ValidationError{Name: "base_path", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.base_path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.VirtualPath(); ok {
		if err := storagepolicy.VirtualPathValidator(v); err != nil {
			return &ValidationError{Name: "virtual_path", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.virtual_path": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *StoragePolicyUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *StoragePolicyUpdate {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *StoragePolicyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(storagepolicy.Table, storagepolicy.Columns, sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeUint))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(storagepolicy.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(storagepolicy.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(storagepolicy.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(storagepolicy.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(storagepolicy.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Flag(); ok {
		_spec.SetField(storagepolicy.FieldFlag, field.TypeString, value)
	}
	if _u.mutation.FlagCleared() {
		_spec.ClearField(storagepolicy.FieldFlag, field.TypeString)
	}
	if value, ok := _u.mutation.Server(); ok {
		_spec.SetField(storagepolicy.FieldServer, field.TypeString, value)
	}
	if _u.mutation.ServerCleared() {
		_spec.ClearField(storagepolicy.FieldServer, field.TypeString)
	}
	if value, ok := _u.mutation.BucketName(); ok {
		_spec.SetField(storagepolicy.FieldBucketName, field.TypeString, value)
	}
	if _u.mutation.BucketNameCleared() {
		_spec.ClearField(storagepolicy.FieldBucketName, field.TypeString)
	}
	if value, ok := _u.mutation.IsPrivate(); ok {
		_spec.SetField(storagepolicy.FieldIsPrivate, field.TypeBool, value)
	}
	if _u.mutation.IsPrivateCleared() {
		_spec.ClearField(storagepolicy.FieldIsPrivate, field.TypeBool)
	}
	if value, ok := _u.mutation.AccessKey(); ok {
		_spec.SetField(storagepolicy.FieldAccessKey, field.TypeString, value)
	}
	if _u.mutation.AccessKeyCleared() {
		_spec.ClearField(storagepolicy.FieldAccessKey, field.TypeString)
	}
	if value, ok := _u.mutation.SecretKey(); ok {
		_spec.SetField(storagepolicy.FieldSecretKey, field.TypeString, value)
	}
	if _u.mutation.SecretKeyCleared() {
		_spec.ClearField(storagepolicy.FieldSecretKey, field.TypeString)
	}
	if value, ok := _u.mutation.MaxSize(); ok {
		_spec.SetField(storagepolicy.FieldMaxSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedMaxSize(); ok {
		_spec.AddField(storagepolicy.FieldMaxSize, field.TypeInt64, value)
	}
	if _u.mutation.MaxSizeCleared() {
		_spec.ClearField(storagepolicy.FieldMaxSize, field.TypeInt64)
	}
	if value, ok := _u.mutation.BasePath(); ok {
		_spec.SetField(storagepolicy.FieldBasePath, field.TypeString, value)
	}
	if _u.mutation.BasePathCleared() {
		_spec.ClearField(storagepolicy.FieldBasePath, field.TypeString)
	}
	if value, ok := _u.mutation.VirtualPath(); ok {
		_spec.SetField(storagepolicy.FieldVirtualPath, field.TypeString, value)
	}
	if _u.mutation.VirtualPathCleared() {
		_spec.ClearField(storagepolicy.FieldVirtualPath, field.TypeString)
	}
	if value, ok := _u.mutation.Settings(); ok {
		_spec.SetField(storagepolicy.FieldSettings, field.TypeOther, value)
	}
	if _u.mutation.SettingsCleared() {
		_spec.ClearField(storagepolicy.FieldSettings, field.TypeOther)
	}
	if value, ok := _u.mutation.NodeID(); ok {
		_spec.SetField(storagepolicy.FieldNodeID, field.TypeUint, value)
	}
	if value, ok := _u.mutation.AddedNodeID(); ok {
		_spec.AddField(storagepolicy.FieldNodeID, field.TypeUint, value)
	}
	if _u.mutation.NodeIDCleared() {
		_spec.ClearField(storagepolicy.FieldNodeID, field.TypeUint)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{storagepolicy.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// StoragePolicyUpdateOne is the builder for updating a single StoragePolicy entity.
type StoragePolicyUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *StoragePolicyMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *StoragePolicyUpdateOne) SetDeletedAt(v time.Time) *StoragePolicyUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableDeletedAt(v *time.Time) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *StoragePolicyUpdateOne) ClearDeletedAt() *StoragePolicyUpdateOne {
	_u.mutation.ClearDeletedAt()
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *StoragePolicyUpdateOne) SetUpdatedAt(v time.Time) *StoragePolicyUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetName sets the "name" field.
func (_u *StoragePolicyUpdateOne) SetName(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableName(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetType sets the "type" field.
func (_u *StoragePolicyUpdateOne) SetType(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableType(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetFlag sets the "flag" field.
func (_u *StoragePolicyUpdateOne) SetFlag(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetFlag(v)
	return _u
}

// SetNillableFlag sets the "flag" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableFlag(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetFlag(*v)
	}
	return _u
}

// ClearFlag clears the value of the "flag" field.
func (_u *StoragePolicyUpdateOne) ClearFlag() *StoragePolicyUpdateOne {
	_u.mutation.ClearFlag()
	return _u
}

// SetServer sets the "server" field.
func (_u *StoragePolicyUpdateOne) SetServer(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetServer(v)
	return _u
}

// SetNillableServer sets the "server" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableServer(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetServer(*v)
	}
	return _u
}

// ClearServer clears the value of the "server" field.
func (_u *StoragePolicyUpdateOne) ClearServer() *StoragePolicyUpdateOne {
	_u.mutation.ClearServer()
	return _u
}

// SetBucketName sets the "bucket_name" field.
func (_u *StoragePolicyUpdateOne) SetBucketName(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetBucketName(v)
	return _u
}

// SetNillableBucketName sets the "bucket_name" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableBucketName(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetBucketName(*v)
	}
	return _u
}

// ClearBucketName clears the value of the "bucket_name" field.
func (_u *StoragePolicyUpdateOne) ClearBucketName() *StoragePolicyUpdateOne {
	_u.mutation.ClearBucketName()
	return _u
}

// SetIsPrivate sets the "is_private" field.
func (_u *StoragePolicyUpdateOne) SetIsPrivate(v bool) *StoragePolicyUpdateOne {
	_u.mutation.SetIsPrivate(v)
	return _u
}

// SetNillableIsPrivate sets the "is_private" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableIsPrivate(v *bool) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetIsPrivate(*v)
	}
	return _u
}

// ClearIsPrivate clears the value of the "is_private" field.
func (_u *StoragePolicyUpdateOne) ClearIsPrivate() *StoragePolicyUpdateOne {
	_u.mutation.ClearIsPrivate()
	return _u
}

// SetAccessKey sets the "access_key" field.
func (_u *StoragePolicyUpdateOne) SetAccessKey(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetAccessKey(v)
	return _u
}

// SetNillableAccessKey sets the "access_key" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableAccessKey(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetAccessKey(*v)
	}
	return _u
}

// ClearAccessKey clears the value of the "access_key" field.
func (_u *StoragePolicyUpdateOne) ClearAccessKey() *StoragePolicyUpdateOne {
	_u.mutation.ClearAccessKey()
	return _u
}

// SetSecretKey sets the "secret_key" field.
func (_u *StoragePolicyUpdateOne) SetSecretKey(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetSecretKey(v)
	return _u
}

// SetNillableSecretKey sets the "secret_key" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableSecretKey(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetSecretKey(*v)
	}
	return _u
}

// ClearSecretKey clears the value of the "secret_key" field.
func (_u *StoragePolicyUpdateOne) ClearSecretKey() *StoragePolicyUpdateOne {
	_u.mutation.ClearSecretKey()
	return _u
}

// SetMaxSize sets the "max_size" field.
func (_u *StoragePolicyUpdateOne) SetMaxSize(v int64) *StoragePolicyUpdateOne {
	_u.mutation.ResetMaxSize()
	_u.mutation.SetMaxSize(v)
	return _u
}

// SetNillableMaxSize sets the "max_size" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableMaxSize(v *int64) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetMaxSize(*v)
	}
	return _u
}

// AddMaxSize adds value to the "max_size" field.
func (_u *StoragePolicyUpdateOne) AddMaxSize(v int64) *StoragePolicyUpdateOne {
	_u.mutation.AddMaxSize(v)
	return _u
}

// ClearMaxSize clears the value of the "max_size" field.
func (_u *StoragePolicyUpdateOne) ClearMaxSize() *StoragePolicyUpdateOne {
	_u.mutation.ClearMaxSize()
	return _u
}

// SetBasePath sets the "base_path" field.
func (_u *StoragePolicyUpdateOne) SetBasePath(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetBasePath(v)
	return _u
}

// SetNillableBasePath sets the "base_path" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableBasePath(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetBasePath(*v)
	}
	return _u
}

// ClearBasePath clears the value of the "base_path" field.
func (_u *StoragePolicyUpdateOne) ClearBasePath() *StoragePolicyUpdateOne {
	_u.mutation.ClearBasePath()
	return _u
}

// SetVirtualPath sets the "virtual_path" field.
func (_u *StoragePolicyUpdateOne) SetVirtualPath(v string) *StoragePolicyUpdateOne {
	_u.mutation.SetVirtualPath(v)
	return _u
}

// SetNillableVirtualPath sets the "virtual_path" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableVirtualPath(v *string) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetVirtualPath(*v)
	}
	return _u
}

// ClearVirtualPath clears the value of the "virtual_path" field.
func (_u *StoragePolicyUpdateOne) ClearVirtualPath() *StoragePolicyUpdateOne {
	_u.mutation.ClearVirtualPath()
	return _u
}

// SetSettings sets the "settings" field.
func (_u *StoragePolicyUpdateOne) SetSettings(v model.StoragePolicySettings) *StoragePolicyUpdateOne {
	_u.mutation.SetSettings(v)
	return _u
}

// ClearSettings clears the value of the "settings" field.
func (_u *StoragePolicyUpdateOne) ClearSettings() *StoragePolicyUpdateOne {
	_u.mutation.ClearSettings()
	return _u
}

// SetNodeID sets the "node_id" field.
func (_u *StoragePolicyUpdateOne) SetNodeID(v uint) *StoragePolicyUpdateOne {
	_u.mutation.ResetNodeID()
	_u.mutation.SetNodeID(v)
	return _u
}

// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (_u *StoragePolicyUpdateOne) SetNillableNodeID(v *uint) *StoragePolicyUpdateOne {
	if v != nil {
		_u.SetNodeID(*v)
	}
	return _u
}

// AddNodeID adds value to the "node_id" field.
func (_u *StoragePolicyUpdateOne) AddNodeID(v int) *StoragePolicyUpdateOne {
	_u.mutation.AddNodeID(v)
	return _u
}

// ClearNodeID clears the value of the "node_id" field.
func (_u *StoragePolicyUpdateOne) ClearNodeID() *StoragePolicyUpdateOne {
	_u.mutation.ClearNodeID()
	return _u
}

// Mutation returns the StoragePolicyMutation object of the builder.
func (_u *StoragePolicyUpdateOne) Mutation() *StoragePolicyMutation {
	return _u.mutation
}

// Where appends a list predicates to the StoragePolicyUpdate builder.
func (_u *StoragePolicyUpdateOne) Where(ps ...predicate.StoragePolicy) *StoragePolicyUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *StoragePolicyUpdateOne) Select(field string, fields ...string) *StoragePolicyUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated StoragePolicy entity.
func (_u *StoragePolicyUpdateOne) Save(ctx context.Context) (*StoragePolicy, error) {
	if err := _u.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *StoragePolicyUpdateOne) SaveX(ctx context.Context) *StoragePolicy {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *StoragePolicyUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *StoragePolicyUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_u *StoragePolicyUpdateOne) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if storagepolicy.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized storagepolicy.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := storagepolicy.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *StoragePolicyUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := storagepolicy.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.GetType(); ok {
		if err := storagepolicy.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.type": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Flag(); ok {
		if err := storagepolicy.FlagValidator(v); err != nil {
			return &ValidationError{Name: "flag", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.flag": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Server(); ok {
		if err := storagepolicy.ServerValidator(v); err != nil {
			return &ValidationError{Name: "server", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.server": %w`, err)}
		}
	}
	if v, ok := _u.mutation.BucketName(); ok {
		if err := storagepolicy.BucketNameValidator(v); err != nil {
			return &ValidationError{Name: "bucket_name", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.bucket_name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.BasePath(); ok {
		if err := storagepolicy.BasePathValidator(v); err != nil {
			return &ValidationError{Name: "base_path", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.base_path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.VirtualPath(); ok {
		if err := storagepolicy.VirtualPathValidator(v); err != nil {
			return &ValidationError{Name: "virtual_path", err: fmt.Errorf(`ent: validator failed for field "StoragePolicy.virtual_path": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *StoragePolicyUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *StoragePolicyUpdateOne {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *StoragePolicyUpdateOne) sqlSave(ctx context.Context) (_node *StoragePolicy, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(storagepolicy.Table, storagepolicy.Columns, sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "StoragePolicy.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, storagepolicy.FieldID)
		for _, f := range fields {
			if !storagepolicy.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != storagepolicy.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(storagepolicy.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(storagepolicy.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(storagepolicy.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Name(); ok {
		_spec.SetField(storagepolicy.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(storagepolicy.FieldType, field.TypeString, value)
	}
	if value, ok := _u.mutation.Flag(); ok {
		_spec.SetField(storagepolicy.FieldFlag, field.TypeString, value)
	}
	if _u.mutation.FlagCleared() {
		_spec.ClearField(storagepolicy.FieldFlag, field.TypeString)
	}
	if value, ok := _u.mutation.Server(); ok {
		_spec.SetField(storagepolicy.FieldServer, field.TypeString, value)
	}
	if _u.mutation.ServerCleared() {
		_spec.ClearField(storagepolicy.FieldServer, field.TypeString)
	}
	if value, ok := _u.mutation.BucketName(); ok {
		_spec.SetField(storagepolicy.FieldBucketName, field.TypeString, value)
	}
	if _u.mutation.BucketNameCleared() {
		_spec.ClearField(storagepolicy.FieldBucketName, field.TypeString)
	}
	if value, ok := _u.mutation.IsPrivate(); ok {
		_spec.SetField(storagepolicy.FieldIsPrivate, field.TypeBool, value)
	}
	if _u.mutation.IsPrivateCleared() {
		_spec.ClearField(storagepolicy.FieldIsPrivate, field.TypeBool)
	}
	if value, ok := _u.mutation.AccessKey(); ok {
		_spec.SetField(storagepolicy.FieldAccessKey, field.TypeString, value)
	}
	if _u.mutation.AccessKeyCleared() {
		_spec.ClearField(storagepolicy.FieldAccessKey, field.TypeString)
	}
	if value, ok := _u.mutation.SecretKey(); ok {
		_spec.SetField(storagepolicy.FieldSecretKey, field.TypeString, value)
	}
	if _u.mutation.SecretKeyCleared() {
		_spec.ClearField(storagepolicy.FieldSecretKey, field.TypeString)
	}
	if value, ok := _u.mutation.MaxSize(); ok {
		_spec.SetField(storagepolicy.FieldMaxSize, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedMaxSize(); ok {
		_spec.AddField(storagepolicy.FieldMaxSize, field.TypeInt64, value)
	}
	if _u.mutation.MaxSizeCleared() {
		_spec.ClearField(storagepolicy.FieldMaxSize, field.TypeInt64)
	}
	if value, ok := _u.mutation.BasePath(); ok {
		_spec.SetField(storagepolicy.FieldBasePath, field.TypeString, value)
	}
	if _u.mutation.BasePathCleared() {
		_spec.ClearField(storagepolicy.FieldBasePath, field.TypeString)
	}
	if value, ok := _u.mutation.VirtualPath(); ok {
		_spec.SetField(storagepolicy.FieldVirtualPath, field.TypeString, value)
	}
	if _u.mutation.VirtualPathCleared() {
		_spec.ClearField(storagepolicy.FieldVirtualPath, field.TypeString)
	}
	if value, ok := _u.mutation.Settings(); ok {
		_spec.SetField(storagepolicy.FieldSettings, field.TypeOther, value)
	}
	if _u.mutation.SettingsCleared() {
		_spec.ClearField(storagepolicy.FieldSettings, field.TypeOther)
	}
	if value, ok := _u.mutation.NodeID(); ok {
		_spec.SetField(storagepolicy.FieldNodeID, field.TypeUint, value)
	}
	if value, ok := _u.mutation.AddedNodeID(); ok {
		_spec.AddField(storagepolicy.FieldNodeID, field.TypeUint, value)
	}
	if _u.mutation.NodeIDCleared() {
		_spec.ClearField(storagepolicy.FieldNodeID, field.TypeUint)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &StoragePolicy{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{storagepolicy.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
