bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/bouncer_create.go (about)

     1  // Code generated by entc, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"time"
    10  
    11  	"entgo.io/ent/dialect/sql/sqlgraph"
    12  	"entgo.io/ent/schema/field"
    13  	"bitbucket.org/Aishee/synsec/pkg/database/ent/bouncer"
    14  )
    15  
    16  // BouncerCreate is the builder for creating a Bouncer entity.
    17  type BouncerCreate struct {
    18  	config
    19  	mutation *BouncerMutation
    20  	hooks    []Hook
    21  }
    22  
    23  // SetCreatedAt sets the "created_at" field.
    24  func (bc *BouncerCreate) SetCreatedAt(t time.Time) *BouncerCreate {
    25  	bc.mutation.SetCreatedAt(t)
    26  	return bc
    27  }
    28  
    29  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
    30  func (bc *BouncerCreate) SetNillableCreatedAt(t *time.Time) *BouncerCreate {
    31  	if t != nil {
    32  		bc.SetCreatedAt(*t)
    33  	}
    34  	return bc
    35  }
    36  
    37  // SetUpdatedAt sets the "updated_at" field.
    38  func (bc *BouncerCreate) SetUpdatedAt(t time.Time) *BouncerCreate {
    39  	bc.mutation.SetUpdatedAt(t)
    40  	return bc
    41  }
    42  
    43  // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
    44  func (bc *BouncerCreate) SetNillableUpdatedAt(t *time.Time) *BouncerCreate {
    45  	if t != nil {
    46  		bc.SetUpdatedAt(*t)
    47  	}
    48  	return bc
    49  }
    50  
    51  // SetName sets the "name" field.
    52  func (bc *BouncerCreate) SetName(s string) *BouncerCreate {
    53  	bc.mutation.SetName(s)
    54  	return bc
    55  }
    56  
    57  // SetAPIKey sets the "api_key" field.
    58  func (bc *BouncerCreate) SetAPIKey(s string) *BouncerCreate {
    59  	bc.mutation.SetAPIKey(s)
    60  	return bc
    61  }
    62  
    63  // SetRevoked sets the "revoked" field.
    64  func (bc *BouncerCreate) SetRevoked(b bool) *BouncerCreate {
    65  	bc.mutation.SetRevoked(b)
    66  	return bc
    67  }
    68  
    69  // SetIPAddress sets the "ip_address" field.
    70  func (bc *BouncerCreate) SetIPAddress(s string) *BouncerCreate {
    71  	bc.mutation.SetIPAddress(s)
    72  	return bc
    73  }
    74  
    75  // SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
    76  func (bc *BouncerCreate) SetNillableIPAddress(s *string) *BouncerCreate {
    77  	if s != nil {
    78  		bc.SetIPAddress(*s)
    79  	}
    80  	return bc
    81  }
    82  
    83  // SetType sets the "type" field.
    84  func (bc *BouncerCreate) SetType(s string) *BouncerCreate {
    85  	bc.mutation.SetType(s)
    86  	return bc
    87  }
    88  
    89  // SetNillableType sets the "type" field if the given value is not nil.
    90  func (bc *BouncerCreate) SetNillableType(s *string) *BouncerCreate {
    91  	if s != nil {
    92  		bc.SetType(*s)
    93  	}
    94  	return bc
    95  }
    96  
    97  // SetVersion sets the "version" field.
    98  func (bc *BouncerCreate) SetVersion(s string) *BouncerCreate {
    99  	bc.mutation.SetVersion(s)
   100  	return bc
   101  }
   102  
   103  // SetNillableVersion sets the "version" field if the given value is not nil.
   104  func (bc *BouncerCreate) SetNillableVersion(s *string) *BouncerCreate {
   105  	if s != nil {
   106  		bc.SetVersion(*s)
   107  	}
   108  	return bc
   109  }
   110  
   111  // SetUntil sets the "until" field.
   112  func (bc *BouncerCreate) SetUntil(t time.Time) *BouncerCreate {
   113  	bc.mutation.SetUntil(t)
   114  	return bc
   115  }
   116  
   117  // SetNillableUntil sets the "until" field if the given value is not nil.
   118  func (bc *BouncerCreate) SetNillableUntil(t *time.Time) *BouncerCreate {
   119  	if t != nil {
   120  		bc.SetUntil(*t)
   121  	}
   122  	return bc
   123  }
   124  
   125  // SetLastPull sets the "last_pull" field.
   126  func (bc *BouncerCreate) SetLastPull(t time.Time) *BouncerCreate {
   127  	bc.mutation.SetLastPull(t)
   128  	return bc
   129  }
   130  
   131  // SetNillableLastPull sets the "last_pull" field if the given value is not nil.
   132  func (bc *BouncerCreate) SetNillableLastPull(t *time.Time) *BouncerCreate {
   133  	if t != nil {
   134  		bc.SetLastPull(*t)
   135  	}
   136  	return bc
   137  }
   138  
   139  // Mutation returns the BouncerMutation object of the builder.
   140  func (bc *BouncerCreate) Mutation() *BouncerMutation {
   141  	return bc.mutation
   142  }
   143  
   144  // Save creates the Bouncer in the database.
   145  func (bc *BouncerCreate) Save(ctx context.Context) (*Bouncer, error) {
   146  	var (
   147  		err  error
   148  		node *Bouncer
   149  	)
   150  	bc.defaults()
   151  	if len(bc.hooks) == 0 {
   152  		if err = bc.check(); err != nil {
   153  			return nil, err
   154  		}
   155  		node, err = bc.sqlSave(ctx)
   156  	} else {
   157  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   158  			mutation, ok := m.(*BouncerMutation)
   159  			if !ok {
   160  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   161  			}
   162  			if err = bc.check(); err != nil {
   163  				return nil, err
   164  			}
   165  			bc.mutation = mutation
   166  			node, err = bc.sqlSave(ctx)
   167  			mutation.done = true
   168  			return node, err
   169  		})
   170  		for i := len(bc.hooks) - 1; i >= 0; i-- {
   171  			mut = bc.hooks[i](mut)
   172  		}
   173  		if _, err := mut.Mutate(ctx, bc.mutation); err != nil {
   174  			return nil, err
   175  		}
   176  	}
   177  	return node, err
   178  }
   179  
   180  // SaveX calls Save and panics if Save returns an error.
   181  func (bc *BouncerCreate) SaveX(ctx context.Context) *Bouncer {
   182  	v, err := bc.Save(ctx)
   183  	if err != nil {
   184  		panic(err)
   185  	}
   186  	return v
   187  }
   188  
   189  // defaults sets the default values of the builder before save.
   190  func (bc *BouncerCreate) defaults() {
   191  	if _, ok := bc.mutation.CreatedAt(); !ok {
   192  		v := bouncer.DefaultCreatedAt()
   193  		bc.mutation.SetCreatedAt(v)
   194  	}
   195  	if _, ok := bc.mutation.UpdatedAt(); !ok {
   196  		v := bouncer.DefaultUpdatedAt()
   197  		bc.mutation.SetUpdatedAt(v)
   198  	}
   199  	if _, ok := bc.mutation.IPAddress(); !ok {
   200  		v := bouncer.DefaultIPAddress
   201  		bc.mutation.SetIPAddress(v)
   202  	}
   203  	if _, ok := bc.mutation.Until(); !ok {
   204  		v := bouncer.DefaultUntil()
   205  		bc.mutation.SetUntil(v)
   206  	}
   207  	if _, ok := bc.mutation.LastPull(); !ok {
   208  		v := bouncer.DefaultLastPull()
   209  		bc.mutation.SetLastPull(v)
   210  	}
   211  }
   212  
   213  // check runs all checks and user-defined validators on the builder.
   214  func (bc *BouncerCreate) check() error {
   215  	if _, ok := bc.mutation.CreatedAt(); !ok {
   216  		return &ValidationError{Name: "created_at", err: errors.New("ent: missing required field \"created_at\"")}
   217  	}
   218  	if _, ok := bc.mutation.UpdatedAt(); !ok {
   219  		return &ValidationError{Name: "updated_at", err: errors.New("ent: missing required field \"updated_at\"")}
   220  	}
   221  	if _, ok := bc.mutation.Name(); !ok {
   222  		return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
   223  	}
   224  	if _, ok := bc.mutation.APIKey(); !ok {
   225  		return &ValidationError{Name: "api_key", err: errors.New("ent: missing required field \"api_key\"")}
   226  	}
   227  	if _, ok := bc.mutation.Revoked(); !ok {
   228  		return &ValidationError{Name: "revoked", err: errors.New("ent: missing required field \"revoked\"")}
   229  	}
   230  	if _, ok := bc.mutation.LastPull(); !ok {
   231  		return &ValidationError{Name: "last_pull", err: errors.New("ent: missing required field \"last_pull\"")}
   232  	}
   233  	return nil
   234  }
   235  
   236  func (bc *BouncerCreate) sqlSave(ctx context.Context) (*Bouncer, error) {
   237  	_node, _spec := bc.createSpec()
   238  	if err := sqlgraph.CreateNode(ctx, bc.driver, _spec); err != nil {
   239  		if cerr, ok := isSQLConstraintError(err); ok {
   240  			err = cerr
   241  		}
   242  		return nil, err
   243  	}
   244  	id := _spec.ID.Value.(int64)
   245  	_node.ID = int(id)
   246  	return _node, nil
   247  }
   248  
   249  func (bc *BouncerCreate) createSpec() (*Bouncer, *sqlgraph.CreateSpec) {
   250  	var (
   251  		_node = &Bouncer{config: bc.config}
   252  		_spec = &sqlgraph.CreateSpec{
   253  			Table: bouncer.Table,
   254  			ID: &sqlgraph.FieldSpec{
   255  				Type:   field.TypeInt,
   256  				Column: bouncer.FieldID,
   257  			},
   258  		}
   259  	)
   260  	if value, ok := bc.mutation.CreatedAt(); ok {
   261  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   262  			Type:   field.TypeTime,
   263  			Value:  value,
   264  			Column: bouncer.FieldCreatedAt,
   265  		})
   266  		_node.CreatedAt = value
   267  	}
   268  	if value, ok := bc.mutation.UpdatedAt(); ok {
   269  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   270  			Type:   field.TypeTime,
   271  			Value:  value,
   272  			Column: bouncer.FieldUpdatedAt,
   273  		})
   274  		_node.UpdatedAt = value
   275  	}
   276  	if value, ok := bc.mutation.Name(); ok {
   277  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   278  			Type:   field.TypeString,
   279  			Value:  value,
   280  			Column: bouncer.FieldName,
   281  		})
   282  		_node.Name = value
   283  	}
   284  	if value, ok := bc.mutation.APIKey(); ok {
   285  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   286  			Type:   field.TypeString,
   287  			Value:  value,
   288  			Column: bouncer.FieldAPIKey,
   289  		})
   290  		_node.APIKey = value
   291  	}
   292  	if value, ok := bc.mutation.Revoked(); ok {
   293  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   294  			Type:   field.TypeBool,
   295  			Value:  value,
   296  			Column: bouncer.FieldRevoked,
   297  		})
   298  		_node.Revoked = value
   299  	}
   300  	if value, ok := bc.mutation.IPAddress(); ok {
   301  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   302  			Type:   field.TypeString,
   303  			Value:  value,
   304  			Column: bouncer.FieldIPAddress,
   305  		})
   306  		_node.IPAddress = value
   307  	}
   308  	if value, ok := bc.mutation.GetType(); ok {
   309  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   310  			Type:   field.TypeString,
   311  			Value:  value,
   312  			Column: bouncer.FieldType,
   313  		})
   314  		_node.Type = value
   315  	}
   316  	if value, ok := bc.mutation.Version(); ok {
   317  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   318  			Type:   field.TypeString,
   319  			Value:  value,
   320  			Column: bouncer.FieldVersion,
   321  		})
   322  		_node.Version = value
   323  	}
   324  	if value, ok := bc.mutation.Until(); ok {
   325  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   326  			Type:   field.TypeTime,
   327  			Value:  value,
   328  			Column: bouncer.FieldUntil,
   329  		})
   330  		_node.Until = value
   331  	}
   332  	if value, ok := bc.mutation.LastPull(); ok {
   333  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   334  			Type:   field.TypeTime,
   335  			Value:  value,
   336  			Column: bouncer.FieldLastPull,
   337  		})
   338  		_node.LastPull = value
   339  	}
   340  	return _node, _spec
   341  }
   342  
   343  // BouncerCreateBulk is the builder for creating many Bouncer entities in bulk.
   344  type BouncerCreateBulk struct {
   345  	config
   346  	builders []*BouncerCreate
   347  }
   348  
   349  // Save creates the Bouncer entities in the database.
   350  func (bcb *BouncerCreateBulk) Save(ctx context.Context) ([]*Bouncer, error) {
   351  	specs := make([]*sqlgraph.CreateSpec, len(bcb.builders))
   352  	nodes := make([]*Bouncer, len(bcb.builders))
   353  	mutators := make([]Mutator, len(bcb.builders))
   354  	for i := range bcb.builders {
   355  		func(i int, root context.Context) {
   356  			builder := bcb.builders[i]
   357  			builder.defaults()
   358  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   359  				mutation, ok := m.(*BouncerMutation)
   360  				if !ok {
   361  					return nil, fmt.Errorf("unexpected mutation type %T", m)
   362  				}
   363  				if err := builder.check(); err != nil {
   364  					return nil, err
   365  				}
   366  				builder.mutation = mutation
   367  				nodes[i], specs[i] = builder.createSpec()
   368  				var err error
   369  				if i < len(mutators)-1 {
   370  					_, err = mutators[i+1].Mutate(root, bcb.builders[i+1].mutation)
   371  				} else {
   372  					// Invoke the actual operation on the latest mutation in the chain.
   373  					if err = sqlgraph.BatchCreate(ctx, bcb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
   374  						if cerr, ok := isSQLConstraintError(err); ok {
   375  							err = cerr
   376  						}
   377  					}
   378  				}
   379  				mutation.done = true
   380  				if err != nil {
   381  					return nil, err
   382  				}
   383  				id := specs[i].ID.Value.(int64)
   384  				nodes[i].ID = int(id)
   385  				return nodes[i], nil
   386  			})
   387  			for i := len(builder.hooks) - 1; i >= 0; i-- {
   388  				mut = builder.hooks[i](mut)
   389  			}
   390  			mutators[i] = mut
   391  		}(i, ctx)
   392  	}
   393  	if len(mutators) > 0 {
   394  		if _, err := mutators[0].Mutate(ctx, bcb.builders[0].mutation); err != nil {
   395  			return nil, err
   396  		}
   397  	}
   398  	return nodes, nil
   399  }
   400  
   401  // SaveX is like Save, but panics if an error occurs.
   402  func (bcb *BouncerCreateBulk) SaveX(ctx context.Context) []*Bouncer {
   403  	v, err := bcb.Save(ctx)
   404  	if err != nil {
   405  		panic(err)
   406  	}
   407  	return v
   408  }