From 71e3db291fcd8ad774787431883c808dc7266c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9E=97=E7=91=9E?= <30717069+OldSmokeGun@users.noreply.github.com> Date: Mon, 27 Nov 2023 15:05:48 +0800 Subject: [PATCH] refactor: update ent generated code --- go.sum | 1 + internal/app/pkg/ent/ent/client.go | 77 ++++++++++++++++++- internal/app/pkg/ent/ent/permission/where.go | 23 +----- internal/app/pkg/ent/ent/permission_create.go | 4 + internal/app/pkg/ent/ent/permission_update.go | 16 ++++ internal/app/pkg/ent/ent/product/where.go | 23 +----- internal/app/pkg/ent/ent/product_create.go | 4 + internal/app/pkg/ent/ent/role/where.go | 23 +----- internal/app/pkg/ent/ent/role_create.go | 4 + internal/app/pkg/ent/ent/role_update.go | 16 ++++ internal/app/pkg/ent/ent/runtime/runtime.go | 4 +- internal/app/pkg/ent/ent/user/where.go | 23 +----- internal/app/pkg/ent/ent/user_create.go | 4 + 13 files changed, 136 insertions(+), 86 deletions(-) diff --git a/go.sum b/go.sum index 51b985f3..b9a21fa8 100644 --- a/go.sum +++ b/go.sum @@ -355,6 +355,7 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/internal/app/pkg/ent/ent/client.go b/internal/app/pkg/ent/ent/client.go index 5e499880..f390f811 100644 --- a/internal/app/pkg/ent/ent/client.go +++ b/internal/app/pkg/ent/ent/client.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "log" + "reflect" "go-scaffold/internal/app/pkg/ent/ent/migrate" @@ -39,9 +40,7 @@ type Client struct { // NewClient creates a new client configured with the given options. func NewClient(opts ...Option) *Client { - cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}} - cfg.options(opts...) - client := &Client{config: cfg} + client := &Client{config: newConfig(opts...)} client.init() return client } @@ -72,6 +71,13 @@ type ( Option func(*config) ) +// newConfig creates a new config for the client. +func newConfig(opts ...Option) config { + cfg := config{log: log.Println, hooks: &hooks{}, inters: &inters{}} + cfg.options(opts...) + return cfg +} + // options applies the options on the config object. func (c *config) options(opts ...Option) { for _, opt := range opts { @@ -119,11 +125,14 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error) } } +// ErrTxStarted is returned when trying to start a new transaction from a transactional client. +var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction") + // Tx returns a new transactional client. The provided context // is used until the transaction is committed or rolled back. func (c *Client) Tx(ctx context.Context) (*Tx, error) { if _, ok := c.driver.(*txDriver); ok { - return nil, errors.New("ent: cannot start a transaction within a transaction") + return nil, ErrTxStarted } tx, err := newTx(ctx, c.driver) if err != nil { @@ -253,6 +262,21 @@ func (c *PermissionClient) CreateBulk(builders ...*PermissionCreate) *Permission return &PermissionCreateBulk{config: c.config, builders: builders} } +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *PermissionClient) MapCreateBulk(slice any, setFunc func(*PermissionCreate, int)) *PermissionCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &PermissionCreateBulk{err: fmt.Errorf("calling to PermissionClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*PermissionCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &PermissionCreateBulk{config: c.config, builders: builders} +} + // Update returns an update builder for Permission. func (c *PermissionClient) Update() *PermissionUpdate { mutation := newPermissionMutation(c.config, OpUpdate) @@ -373,6 +397,21 @@ func (c *ProductClient) CreateBulk(builders ...*ProductCreate) *ProductCreateBul return &ProductCreateBulk{config: c.config, builders: builders} } +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *ProductClient) MapCreateBulk(slice any, setFunc func(*ProductCreate, int)) *ProductCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &ProductCreateBulk{err: fmt.Errorf("calling to ProductClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*ProductCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &ProductCreateBulk{config: c.config, builders: builders} +} + // Update returns an update builder for Product. func (c *ProductClient) Update() *ProductUpdate { mutation := newProductMutation(c.config, OpUpdate) @@ -493,6 +532,21 @@ func (c *RoleClient) CreateBulk(builders ...*RoleCreate) *RoleCreateBulk { return &RoleCreateBulk{config: c.config, builders: builders} } +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *RoleClient) MapCreateBulk(slice any, setFunc func(*RoleCreate, int)) *RoleCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &RoleCreateBulk{err: fmt.Errorf("calling to RoleClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*RoleCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &RoleCreateBulk{config: c.config, builders: builders} +} + // Update returns an update builder for Role. func (c *RoleClient) Update() *RoleUpdate { mutation := newRoleMutation(c.config, OpUpdate) @@ -613,6 +667,21 @@ func (c *UserClient) CreateBulk(builders ...*UserCreate) *UserCreateBulk { return &UserCreateBulk{config: c.config, builders: builders} } +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *UserClient) MapCreateBulk(slice any, setFunc func(*UserCreate, int)) *UserCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &UserCreateBulk{err: fmt.Errorf("calling to UserClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*UserCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &UserCreateBulk{config: c.config, builders: builders} +} + // Update returns an update builder for User. func (c *UserClient) Update() *UserUpdate { mutation := newUserMutation(c.config, OpUpdate) diff --git a/internal/app/pkg/ent/ent/permission/where.go b/internal/app/pkg/ent/ent/permission/where.go index 809dc399..95cc8ed4 100644 --- a/internal/app/pkg/ent/ent/permission/where.go +++ b/internal/app/pkg/ent/ent/permission/where.go @@ -456,32 +456,15 @@ func ParentIDLTE(v int64) predicate.Permission { // And groups predicates with the AND operator between them. func And(predicates ...predicate.Permission) predicate.Permission { - return predicate.Permission(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for _, p := range predicates { - p(s1) - } - s.Where(s1.P()) - }) + return predicate.Permission(sql.AndPredicates(predicates...)) } // Or groups predicates with the OR operator between them. func Or(predicates ...predicate.Permission) predicate.Permission { - return predicate.Permission(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for i, p := range predicates { - if i > 0 { - s1.Or() - } - p(s1) - } - s.Where(s1.P()) - }) + return predicate.Permission(sql.OrPredicates(predicates...)) } // Not applies the not operator on the given predicate. func Not(p predicate.Permission) predicate.Permission { - return predicate.Permission(func(s *sql.Selector) { - p(s.Not()) - }) + return predicate.Permission(sql.NotPredicates(p)) } diff --git a/internal/app/pkg/ent/ent/permission_create.go b/internal/app/pkg/ent/ent/permission_create.go index af59b93b..a9f02bf4 100644 --- a/internal/app/pkg/ent/ent/permission_create.go +++ b/internal/app/pkg/ent/ent/permission_create.go @@ -283,11 +283,15 @@ func (pc *PermissionCreate) createSpec() (*Permission, *sqlgraph.CreateSpec) { // PermissionCreateBulk is the builder for creating many Permission entities in bulk. type PermissionCreateBulk struct { config + err error builders []*PermissionCreate } // Save creates the Permission entities in the database. func (pcb *PermissionCreateBulk) Save(ctx context.Context) ([]*Permission, error) { + if pcb.err != nil { + return nil, pcb.err + } specs := make([]*sqlgraph.CreateSpec, len(pcb.builders)) nodes := make([]*Permission, len(pcb.builders)) mutators := make([]Mutator, len(pcb.builders)) diff --git a/internal/app/pkg/ent/ent/permission_update.go b/internal/app/pkg/ent/ent/permission_update.go index 092da640..7fc3a5ed 100644 --- a/internal/app/pkg/ent/ent/permission_update.go +++ b/internal/app/pkg/ent/ent/permission_update.go @@ -55,6 +55,14 @@ func (pu *PermissionUpdate) SetKey(s string) *PermissionUpdate { return pu } +// SetNillableKey sets the "key" field if the given value is not nil. +func (pu *PermissionUpdate) SetNillableKey(s *string) *PermissionUpdate { + if s != nil { + pu.SetKey(*s) + } + return pu +} + // SetName sets the "name" field. func (pu *PermissionUpdate) SetName(s string) *PermissionUpdate { pu.mutation.SetName(s) @@ -261,6 +269,14 @@ func (puo *PermissionUpdateOne) SetKey(s string) *PermissionUpdateOne { return puo } +// SetNillableKey sets the "key" field if the given value is not nil. +func (puo *PermissionUpdateOne) SetNillableKey(s *string) *PermissionUpdateOne { + if s != nil { + puo.SetKey(*s) + } + return puo +} + // SetName sets the "name" field. func (puo *PermissionUpdateOne) SetName(s string) *PermissionUpdateOne { puo.mutation.SetName(s) diff --git a/internal/app/pkg/ent/ent/product/where.go b/internal/app/pkg/ent/ent/product/where.go index cde8a9d8..a9106c9b 100644 --- a/internal/app/pkg/ent/ent/product/where.go +++ b/internal/app/pkg/ent/ent/product/where.go @@ -386,32 +386,15 @@ func PriceLTE(v int) predicate.Product { // And groups predicates with the AND operator between them. func And(predicates ...predicate.Product) predicate.Product { - return predicate.Product(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for _, p := range predicates { - p(s1) - } - s.Where(s1.P()) - }) + return predicate.Product(sql.AndPredicates(predicates...)) } // Or groups predicates with the OR operator between them. func Or(predicates ...predicate.Product) predicate.Product { - return predicate.Product(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for i, p := range predicates { - if i > 0 { - s1.Or() - } - p(s1) - } - s.Where(s1.P()) - }) + return predicate.Product(sql.OrPredicates(predicates...)) } // Not applies the not operator on the given predicate. func Not(p predicate.Product) predicate.Product { - return predicate.Product(func(s *sql.Selector) { - p(s.Not()) - }) + return predicate.Product(sql.NotPredicates(p)) } diff --git a/internal/app/pkg/ent/ent/product_create.go b/internal/app/pkg/ent/ent/product_create.go index 20b8fec4..808b5e0e 100644 --- a/internal/app/pkg/ent/ent/product_create.go +++ b/internal/app/pkg/ent/ent/product_create.go @@ -255,11 +255,15 @@ func (pc *ProductCreate) createSpec() (*Product, *sqlgraph.CreateSpec) { // ProductCreateBulk is the builder for creating many Product entities in bulk. type ProductCreateBulk struct { config + err error builders []*ProductCreate } // Save creates the Product entities in the database. func (pcb *ProductCreateBulk) Save(ctx context.Context) ([]*Product, error) { + if pcb.err != nil { + return nil, pcb.err + } specs := make([]*sqlgraph.CreateSpec, len(pcb.builders)) nodes := make([]*Product, len(pcb.builders)) mutators := make([]Mutator, len(pcb.builders)) diff --git a/internal/app/pkg/ent/ent/role/where.go b/internal/app/pkg/ent/ent/role/where.go index ef651dd3..501ea989 100644 --- a/internal/app/pkg/ent/ent/role/where.go +++ b/internal/app/pkg/ent/ent/role/where.go @@ -271,32 +271,15 @@ func NameContainsFold(v string) predicate.Role { // And groups predicates with the AND operator between them. func And(predicates ...predicate.Role) predicate.Role { - return predicate.Role(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for _, p := range predicates { - p(s1) - } - s.Where(s1.P()) - }) + return predicate.Role(sql.AndPredicates(predicates...)) } // Or groups predicates with the OR operator between them. func Or(predicates ...predicate.Role) predicate.Role { - return predicate.Role(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for i, p := range predicates { - if i > 0 { - s1.Or() - } - p(s1) - } - s.Where(s1.P()) - }) + return predicate.Role(sql.OrPredicates(predicates...)) } // Not applies the not operator on the given predicate. func Not(p predicate.Role) predicate.Role { - return predicate.Role(func(s *sql.Selector) { - p(s.Not()) - }) + return predicate.Role(sql.NotPredicates(p)) } diff --git a/internal/app/pkg/ent/ent/role_create.go b/internal/app/pkg/ent/ent/role_create.go index f490e80a..fb7c20ce 100644 --- a/internal/app/pkg/ent/ent/role_create.go +++ b/internal/app/pkg/ent/ent/role_create.go @@ -198,11 +198,15 @@ func (rc *RoleCreate) createSpec() (*Role, *sqlgraph.CreateSpec) { // RoleCreateBulk is the builder for creating many Role entities in bulk. type RoleCreateBulk struct { config + err error builders []*RoleCreate } // Save creates the Role entities in the database. func (rcb *RoleCreateBulk) Save(ctx context.Context) ([]*Role, error) { + if rcb.err != nil { + return nil, rcb.err + } specs := make([]*sqlgraph.CreateSpec, len(rcb.builders)) nodes := make([]*Role, len(rcb.builders)) mutators := make([]Mutator, len(rcb.builders)) diff --git a/internal/app/pkg/ent/ent/role_update.go b/internal/app/pkg/ent/ent/role_update.go index c98afd6d..c5e331a6 100644 --- a/internal/app/pkg/ent/ent/role_update.go +++ b/internal/app/pkg/ent/ent/role_update.go @@ -55,6 +55,14 @@ func (ru *RoleUpdate) SetName(s string) *RoleUpdate { return ru } +// SetNillableName sets the "name" field if the given value is not nil. +func (ru *RoleUpdate) SetNillableName(s *string) *RoleUpdate { + if s != nil { + ru.SetName(*s) + } + return ru +} + // Mutation returns the RoleMutation object of the builder. func (ru *RoleUpdate) Mutation() *RoleMutation { return ru.mutation @@ -190,6 +198,14 @@ func (ruo *RoleUpdateOne) SetName(s string) *RoleUpdateOne { return ruo } +// SetNillableName sets the "name" field if the given value is not nil. +func (ruo *RoleUpdateOne) SetNillableName(s *string) *RoleUpdateOne { + if s != nil { + ruo.SetName(*s) + } + return ruo +} + // Mutation returns the RoleMutation object of the builder. func (ruo *RoleUpdateOne) Mutation() *RoleMutation { return ruo.mutation diff --git a/internal/app/pkg/ent/ent/runtime/runtime.go b/internal/app/pkg/ent/ent/runtime/runtime.go index 4ff4c2f4..12cf3407 100644 --- a/internal/app/pkg/ent/ent/runtime/runtime.go +++ b/internal/app/pkg/ent/ent/runtime/runtime.go @@ -150,6 +150,6 @@ func init() { } const ( - Version = "v0.12.3" // Version of ent codegen. - Sum = "h1:N5lO2EOrHpCH5HYfiMOCHYbo+oh5M8GjT0/cx5x6xkk=" // Sum of ent codegen. + Version = "v0.12.5" // Version of ent codegen. + Sum = "h1:KREM5E4CSoej4zeGa88Ou/gfturAnpUv0mzAjch1sj4=" // Sum of ent codegen. ) diff --git a/internal/app/pkg/ent/ent/user/where.go b/internal/app/pkg/ent/ent/user/where.go index 8ec589f0..c5c8d0c8 100644 --- a/internal/app/pkg/ent/ent/user/where.go +++ b/internal/app/pkg/ent/ent/user/where.go @@ -551,32 +551,15 @@ func SaltContainsFold(v string) predicate.User { // And groups predicates with the AND operator between them. func And(predicates ...predicate.User) predicate.User { - return predicate.User(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for _, p := range predicates { - p(s1) - } - s.Where(s1.P()) - }) + return predicate.User(sql.AndPredicates(predicates...)) } // Or groups predicates with the OR operator between them. func Or(predicates ...predicate.User) predicate.User { - return predicate.User(func(s *sql.Selector) { - s1 := s.Clone().SetP(nil) - for i, p := range predicates { - if i > 0 { - s1.Or() - } - p(s1) - } - s.Where(s1.P()) - }) + return predicate.User(sql.OrPredicates(predicates...)) } // Not applies the not operator on the given predicate. func Not(p predicate.User) predicate.User { - return predicate.User(func(s *sql.Selector) { - p(s.Not()) - }) + return predicate.User(sql.NotPredicates(p)) } diff --git a/internal/app/pkg/ent/ent/user_create.go b/internal/app/pkg/ent/ent/user_create.go index bc0884c8..6216743d 100644 --- a/internal/app/pkg/ent/ent/user_create.go +++ b/internal/app/pkg/ent/ent/user_create.go @@ -305,11 +305,15 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { // UserCreateBulk is the builder for creating many User entities in bulk. type UserCreateBulk struct { config + err error builders []*UserCreate } // Save creates the User entities in the database. func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { + if ucb.err != nil { + return nil, ucb.err + } specs := make([]*sqlgraph.CreateSpec, len(ucb.builders)) nodes := make([]*User, len(ucb.builders)) mutators := make([]Mutator, len(ucb.builders))