// 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/cloudreve/Cloudreve/v4/ent/node"
	"github.com/cloudreve/Cloudreve/v4/ent/predicate"
	"github.com/cloudreve/Cloudreve/v4/ent/storagepolicy"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
	"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
)

// NodeUpdate is the builder for updating Node entities.
type NodeUpdate struct {
	config
	hooks    []Hook
	mutation *NodeMutation
}

// Where appends a list predicates to the NodeUpdate builder.
func (nu *NodeUpdate) Where(ps ...predicate.Node) *NodeUpdate {
	nu.mutation.Where(ps...)
	return nu
}

// SetUpdatedAt sets the "updated_at" field.
func (nu *NodeUpdate) SetUpdatedAt(t time.Time) *NodeUpdate {
	nu.mutation.SetUpdatedAt(t)
	return nu
}

// SetDeletedAt sets the "deleted_at" field.
func (nu *NodeUpdate) SetDeletedAt(t time.Time) *NodeUpdate {
	nu.mutation.SetDeletedAt(t)
	return nu
}

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

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

// SetStatus sets the "status" field.
func (nu *NodeUpdate) SetStatus(n node.Status) *NodeUpdate {
	nu.mutation.SetStatus(n)
	return nu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (nu *NodeUpdate) SetNillableStatus(n *node.Status) *NodeUpdate {
	if n != nil {
		nu.SetStatus(*n)
	}
	return nu
}

// SetName sets the "name" field.
func (nu *NodeUpdate) SetName(s string) *NodeUpdate {
	nu.mutation.SetName(s)
	return nu
}

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

// SetType sets the "type" field.
func (nu *NodeUpdate) SetType(n node.Type) *NodeUpdate {
	nu.mutation.SetType(n)
	return nu
}

// SetNillableType sets the "type" field if the given value is not nil.
func (nu *NodeUpdate) SetNillableType(n *node.Type) *NodeUpdate {
	if n != nil {
		nu.SetType(*n)
	}
	return nu
}

// SetServer sets the "server" field.
func (nu *NodeUpdate) SetServer(s string) *NodeUpdate {
	nu.mutation.SetServer(s)
	return nu
}

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

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

// SetSlaveKey sets the "slave_key" field.
func (nu *NodeUpdate) SetSlaveKey(s string) *NodeUpdate {
	nu.mutation.SetSlaveKey(s)
	return nu
}

// SetNillableSlaveKey sets the "slave_key" field if the given value is not nil.
func (nu *NodeUpdate) SetNillableSlaveKey(s *string) *NodeUpdate {
	if s != nil {
		nu.SetSlaveKey(*s)
	}
	return nu
}

// ClearSlaveKey clears the value of the "slave_key" field.
func (nu *NodeUpdate) ClearSlaveKey() *NodeUpdate {
	nu.mutation.ClearSlaveKey()
	return nu
}

// SetCapabilities sets the "capabilities" field.
func (nu *NodeUpdate) SetCapabilities(bs *boolset.BooleanSet) *NodeUpdate {
	nu.mutation.SetCapabilities(bs)
	return nu
}

// SetSettings sets the "settings" field.
func (nu *NodeUpdate) SetSettings(ts *types.NodeSetting) *NodeUpdate {
	nu.mutation.SetSettings(ts)
	return nu
}

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

// SetWeight sets the "weight" field.
func (nu *NodeUpdate) SetWeight(i int) *NodeUpdate {
	nu.mutation.ResetWeight()
	nu.mutation.SetWeight(i)
	return nu
}

// SetNillableWeight sets the "weight" field if the given value is not nil.
func (nu *NodeUpdate) SetNillableWeight(i *int) *NodeUpdate {
	if i != nil {
		nu.SetWeight(*i)
	}
	return nu
}

// AddWeight adds i to the "weight" field.
func (nu *NodeUpdate) AddWeight(i int) *NodeUpdate {
	nu.mutation.AddWeight(i)
	return nu
}

// AddStoragePolicyIDs adds the "storage_policy" edge to the StoragePolicy entity by IDs.
func (nu *NodeUpdate) AddStoragePolicyIDs(ids ...int) *NodeUpdate {
	nu.mutation.AddStoragePolicyIDs(ids...)
	return nu
}

// AddStoragePolicy adds the "storage_policy" edges to the StoragePolicy entity.
func (nu *NodeUpdate) AddStoragePolicy(s ...*StoragePolicy) *NodeUpdate {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return nu.AddStoragePolicyIDs(ids...)
}

// Mutation returns the NodeMutation object of the builder.
func (nu *NodeUpdate) Mutation() *NodeMutation {
	return nu.mutation
}

// ClearStoragePolicy clears all "storage_policy" edges to the StoragePolicy entity.
func (nu *NodeUpdate) ClearStoragePolicy() *NodeUpdate {
	nu.mutation.ClearStoragePolicy()
	return nu
}

// RemoveStoragePolicyIDs removes the "storage_policy" edge to StoragePolicy entities by IDs.
func (nu *NodeUpdate) RemoveStoragePolicyIDs(ids ...int) *NodeUpdate {
	nu.mutation.RemoveStoragePolicyIDs(ids...)
	return nu
}

// RemoveStoragePolicy removes "storage_policy" edges to StoragePolicy entities.
func (nu *NodeUpdate) RemoveStoragePolicy(s ...*StoragePolicy) *NodeUpdate {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return nu.RemoveStoragePolicyIDs(ids...)
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (nu *NodeUpdate) check() error {
	if v, ok := nu.mutation.Status(); ok {
		if err := node.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Node.status": %w`, err)}
		}
	}
	if v, ok := nu.mutation.GetType(); ok {
		if err := node.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Node.type": %w`, err)}
		}
	}
	return nil
}

func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := nu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(node.Table, node.Columns, sqlgraph.NewFieldSpec(node.FieldID, field.TypeInt))
	if ps := nu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nu.mutation.UpdatedAt(); ok {
		_spec.SetField(node.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := nu.mutation.DeletedAt(); ok {
		_spec.SetField(node.FieldDeletedAt, field.TypeTime, value)
	}
	if nu.mutation.DeletedAtCleared() {
		_spec.ClearField(node.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := nu.mutation.Status(); ok {
		_spec.SetField(node.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := nu.mutation.Name(); ok {
		_spec.SetField(node.FieldName, field.TypeString, value)
	}
	if value, ok := nu.mutation.GetType(); ok {
		_spec.SetField(node.FieldType, field.TypeEnum, value)
	}
	if value, ok := nu.mutation.Server(); ok {
		_spec.SetField(node.FieldServer, field.TypeString, value)
	}
	if nu.mutation.ServerCleared() {
		_spec.ClearField(node.FieldServer, field.TypeString)
	}
	if value, ok := nu.mutation.SlaveKey(); ok {
		_spec.SetField(node.FieldSlaveKey, field.TypeString, value)
	}
	if nu.mutation.SlaveKeyCleared() {
		_spec.ClearField(node.FieldSlaveKey, field.TypeString)
	}
	if value, ok := nu.mutation.Capabilities(); ok {
		_spec.SetField(node.FieldCapabilities, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.Settings(); ok {
		_spec.SetField(node.FieldSettings, field.TypeJSON, value)
	}
	if nu.mutation.SettingsCleared() {
		_spec.ClearField(node.FieldSettings, field.TypeJSON)
	}
	if value, ok := nu.mutation.Weight(); ok {
		_spec.SetField(node.FieldWeight, field.TypeInt, value)
	}
	if value, ok := nu.mutation.AddedWeight(); ok {
		_spec.AddField(node.FieldWeight, field.TypeInt, value)
	}
	if nu.mutation.StoragePolicyCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nu.mutation.RemovedStoragePolicyIDs(); len(nodes) > 0 && !nu.mutation.StoragePolicyCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nu.mutation.StoragePolicyIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, nu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{node.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	nu.mutation.done = true
	return n, nil
}

// NodeUpdateOne is the builder for updating a single Node entity.
type NodeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *NodeMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (nuo *NodeUpdateOne) SetUpdatedAt(t time.Time) *NodeUpdateOne {
	nuo.mutation.SetUpdatedAt(t)
	return nuo
}

// SetDeletedAt sets the "deleted_at" field.
func (nuo *NodeUpdateOne) SetDeletedAt(t time.Time) *NodeUpdateOne {
	nuo.mutation.SetDeletedAt(t)
	return nuo
}

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

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

// SetStatus sets the "status" field.
func (nuo *NodeUpdateOne) SetStatus(n node.Status) *NodeUpdateOne {
	nuo.mutation.SetStatus(n)
	return nuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillableStatus(n *node.Status) *NodeUpdateOne {
	if n != nil {
		nuo.SetStatus(*n)
	}
	return nuo
}

// SetName sets the "name" field.
func (nuo *NodeUpdateOne) SetName(s string) *NodeUpdateOne {
	nuo.mutation.SetName(s)
	return nuo
}

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

// SetType sets the "type" field.
func (nuo *NodeUpdateOne) SetType(n node.Type) *NodeUpdateOne {
	nuo.mutation.SetType(n)
	return nuo
}

// SetNillableType sets the "type" field if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillableType(n *node.Type) *NodeUpdateOne {
	if n != nil {
		nuo.SetType(*n)
	}
	return nuo
}

// SetServer sets the "server" field.
func (nuo *NodeUpdateOne) SetServer(s string) *NodeUpdateOne {
	nuo.mutation.SetServer(s)
	return nuo
}

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

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

// SetSlaveKey sets the "slave_key" field.
func (nuo *NodeUpdateOne) SetSlaveKey(s string) *NodeUpdateOne {
	nuo.mutation.SetSlaveKey(s)
	return nuo
}

// SetNillableSlaveKey sets the "slave_key" field if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillableSlaveKey(s *string) *NodeUpdateOne {
	if s != nil {
		nuo.SetSlaveKey(*s)
	}
	return nuo
}

// ClearSlaveKey clears the value of the "slave_key" field.
func (nuo *NodeUpdateOne) ClearSlaveKey() *NodeUpdateOne {
	nuo.mutation.ClearSlaveKey()
	return nuo
}

// SetCapabilities sets the "capabilities" field.
func (nuo *NodeUpdateOne) SetCapabilities(bs *boolset.BooleanSet) *NodeUpdateOne {
	nuo.mutation.SetCapabilities(bs)
	return nuo
}

// SetSettings sets the "settings" field.
func (nuo *NodeUpdateOne) SetSettings(ts *types.NodeSetting) *NodeUpdateOne {
	nuo.mutation.SetSettings(ts)
	return nuo
}

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

// SetWeight sets the "weight" field.
func (nuo *NodeUpdateOne) SetWeight(i int) *NodeUpdateOne {
	nuo.mutation.ResetWeight()
	nuo.mutation.SetWeight(i)
	return nuo
}

// SetNillableWeight sets the "weight" field if the given value is not nil.
func (nuo *NodeUpdateOne) SetNillableWeight(i *int) *NodeUpdateOne {
	if i != nil {
		nuo.SetWeight(*i)
	}
	return nuo
}

// AddWeight adds i to the "weight" field.
func (nuo *NodeUpdateOne) AddWeight(i int) *NodeUpdateOne {
	nuo.mutation.AddWeight(i)
	return nuo
}

// AddStoragePolicyIDs adds the "storage_policy" edge to the StoragePolicy entity by IDs.
func (nuo *NodeUpdateOne) AddStoragePolicyIDs(ids ...int) *NodeUpdateOne {
	nuo.mutation.AddStoragePolicyIDs(ids...)
	return nuo
}

// AddStoragePolicy adds the "storage_policy" edges to the StoragePolicy entity.
func (nuo *NodeUpdateOne) AddStoragePolicy(s ...*StoragePolicy) *NodeUpdateOne {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return nuo.AddStoragePolicyIDs(ids...)
}

// Mutation returns the NodeMutation object of the builder.
func (nuo *NodeUpdateOne) Mutation() *NodeMutation {
	return nuo.mutation
}

// ClearStoragePolicy clears all "storage_policy" edges to the StoragePolicy entity.
func (nuo *NodeUpdateOne) ClearStoragePolicy() *NodeUpdateOne {
	nuo.mutation.ClearStoragePolicy()
	return nuo
}

// RemoveStoragePolicyIDs removes the "storage_policy" edge to StoragePolicy entities by IDs.
func (nuo *NodeUpdateOne) RemoveStoragePolicyIDs(ids ...int) *NodeUpdateOne {
	nuo.mutation.RemoveStoragePolicyIDs(ids...)
	return nuo
}

// RemoveStoragePolicy removes "storage_policy" edges to StoragePolicy entities.
func (nuo *NodeUpdateOne) RemoveStoragePolicy(s ...*StoragePolicy) *NodeUpdateOne {
	ids := make([]int, len(s))
	for i := range s {
		ids[i] = s[i].ID
	}
	return nuo.RemoveStoragePolicyIDs(ids...)
}

// Where appends a list predicates to the NodeUpdate builder.
func (nuo *NodeUpdateOne) Where(ps ...predicate.Node) *NodeUpdateOne {
	nuo.mutation.Where(ps...)
	return nuo
}

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

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (nuo *NodeUpdateOne) check() error {
	if v, ok := nuo.mutation.Status(); ok {
		if err := node.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Node.status": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.GetType(); ok {
		if err := node.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Node.type": %w`, err)}
		}
	}
	return nil
}

func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (_node *Node, err error) {
	if err := nuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(node.Table, node.Columns, sqlgraph.NewFieldSpec(node.FieldID, field.TypeInt))
	id, ok := nuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Node.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := nuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, node.FieldID)
		for _, f := range fields {
			if !node.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != node.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := nuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nuo.mutation.UpdatedAt(); ok {
		_spec.SetField(node.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := nuo.mutation.DeletedAt(); ok {
		_spec.SetField(node.FieldDeletedAt, field.TypeTime, value)
	}
	if nuo.mutation.DeletedAtCleared() {
		_spec.ClearField(node.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := nuo.mutation.Status(); ok {
		_spec.SetField(node.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := nuo.mutation.Name(); ok {
		_spec.SetField(node.FieldName, field.TypeString, value)
	}
	if value, ok := nuo.mutation.GetType(); ok {
		_spec.SetField(node.FieldType, field.TypeEnum, value)
	}
	if value, ok := nuo.mutation.Server(); ok {
		_spec.SetField(node.FieldServer, field.TypeString, value)
	}
	if nuo.mutation.ServerCleared() {
		_spec.ClearField(node.FieldServer, field.TypeString)
	}
	if value, ok := nuo.mutation.SlaveKey(); ok {
		_spec.SetField(node.FieldSlaveKey, field.TypeString, value)
	}
	if nuo.mutation.SlaveKeyCleared() {
		_spec.ClearField(node.FieldSlaveKey, field.TypeString)
	}
	if value, ok := nuo.mutation.Capabilities(); ok {
		_spec.SetField(node.FieldCapabilities, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.Settings(); ok {
		_spec.SetField(node.FieldSettings, field.TypeJSON, value)
	}
	if nuo.mutation.SettingsCleared() {
		_spec.ClearField(node.FieldSettings, field.TypeJSON)
	}
	if value, ok := nuo.mutation.Weight(); ok {
		_spec.SetField(node.FieldWeight, field.TypeInt, value)
	}
	if value, ok := nuo.mutation.AddedWeight(); ok {
		_spec.AddField(node.FieldWeight, field.TypeInt, value)
	}
	if nuo.mutation.StoragePolicyCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nuo.mutation.RemovedStoragePolicyIDs(); len(nodes) > 0 && !nuo.mutation.StoragePolicyCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nuo.mutation.StoragePolicyIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   node.StoragePolicyTable,
			Columns: []string{node.StoragePolicyColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(storagepolicy.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Node{config: nuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, nuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{node.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	nuo.mutation.done = true
	return _node, nil
}
