entc/gen: make generated client to implement the ent.Mutator interface (#3161)

This commit is contained in:
Ariel Mashraki
2022-12-09 21:18:25 +02:00
committed by GitHub
parent 3f4916ff8b
commit d0c5afa705
85 changed files with 3522 additions and 22 deletions

View File

@@ -183,6 +183,32 @@ func (c *Client) Use(hooks ...Hook) {
c.Zoo.Use(hooks...)
}
// Mutate implements the ent.Mutator interface.
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
switch m := m.(type) {
case *BlogMutation:
return c.Blog.mutate(ctx, m)
case *CarMutation:
return c.Car.mutate(ctx, m)
case *ConversionMutation:
return c.Conversion.mutate(ctx, m)
case *CustomTypeMutation:
return c.CustomType.mutate(ctx, m)
case *GroupMutation:
return c.Group.mutate(ctx, m)
case *MediaMutation:
return c.Media.mutate(ctx, m)
case *PetMutation:
return c.Pet.mutate(ctx, m)
case *UserMutation:
return c.User.mutate(ctx, m)
case *ZooMutation:
return c.Zoo.mutate(ctx, m)
default:
return nil, fmt.Errorf("entv2: unknown mutation type %T", m)
}
}
// BlogClient is a client for the Blog schema.
type BlogClient struct {
config
@@ -289,6 +315,21 @@ func (c *BlogClient) Hooks() []Hook {
return c.hooks.Blog
}
func (c *BlogClient) mutate(ctx context.Context, m *BlogMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&BlogCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&BlogUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&BlogUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&BlogDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Blog mutation op: %q", m.Op())
}
}
// CarClient is a client for the Car schema.
type CarClient struct {
config
@@ -395,6 +436,21 @@ func (c *CarClient) Hooks() []Hook {
return c.hooks.Car
}
func (c *CarClient) mutate(ctx context.Context, m *CarMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&CarCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&CarUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&CarUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&CarDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Car mutation op: %q", m.Op())
}
}
// ConversionClient is a client for the Conversion schema.
type ConversionClient struct {
config
@@ -485,6 +541,21 @@ func (c *ConversionClient) Hooks() []Hook {
return c.hooks.Conversion
}
func (c *ConversionClient) mutate(ctx context.Context, m *ConversionMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&ConversionCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&ConversionUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&ConversionUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&ConversionDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Conversion mutation op: %q", m.Op())
}
}
// CustomTypeClient is a client for the CustomType schema.
type CustomTypeClient struct {
config
@@ -575,6 +646,21 @@ func (c *CustomTypeClient) Hooks() []Hook {
return c.hooks.CustomType
}
func (c *CustomTypeClient) mutate(ctx context.Context, m *CustomTypeMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&CustomTypeCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&CustomTypeUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&CustomTypeUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&CustomTypeDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown CustomType mutation op: %q", m.Op())
}
}
// GroupClient is a client for the Group schema.
type GroupClient struct {
config
@@ -665,6 +751,21 @@ func (c *GroupClient) Hooks() []Hook {
return c.hooks.Group
}
func (c *GroupClient) mutate(ctx context.Context, m *GroupMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&GroupCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&GroupUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&GroupUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&GroupDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Group mutation op: %q", m.Op())
}
}
// MediaClient is a client for the Media schema.
type MediaClient struct {
config
@@ -755,6 +856,21 @@ func (c *MediaClient) Hooks() []Hook {
return c.hooks.Media
}
func (c *MediaClient) mutate(ctx context.Context, m *MediaMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&MediaCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&MediaUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&MediaUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&MediaDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Media mutation op: %q", m.Op())
}
}
// PetClient is a client for the Pet schema.
type PetClient struct {
config
@@ -861,6 +977,21 @@ func (c *PetClient) Hooks() []Hook {
return c.hooks.Pet
}
func (c *PetClient) mutate(ctx context.Context, m *PetMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&PetCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&PetUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&PetUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&PetDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Pet mutation op: %q", m.Op())
}
}
// UserClient is a client for the User schema.
type UserClient struct {
config
@@ -999,6 +1130,21 @@ func (c *UserClient) Hooks() []Hook {
return c.hooks.User
}
func (c *UserClient) mutate(ctx context.Context, m *UserMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&UserCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&UserUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&UserUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&UserDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown User mutation op: %q", m.Op())
}
}
// ZooClient is a client for the Zoo schema.
type ZooClient struct {
config
@@ -1088,3 +1234,18 @@ func (c *ZooClient) GetX(ctx context.Context, id int) *Zoo {
func (c *ZooClient) Hooks() []Hook {
return c.hooks.Zoo
}
func (c *ZooClient) mutate(ctx context.Context, m *ZooMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&ZooCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&ZooUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&ZooUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&ZooDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("entv2: unknown Zoo mutation op: %q", m.Op())
}
}

View File

@@ -228,6 +228,11 @@ func (m *BlogMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *BlogMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Blog).
func (m *BlogMutation) Type() string {
return m.typ
@@ -604,6 +609,11 @@ func (m *CarMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *CarMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Car).
func (m *CarMutation) Type() string {
return m.typ
@@ -1363,6 +1373,11 @@ func (m *ConversionMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *ConversionMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Conversion).
func (m *ConversionMutation) Type() string {
return m.typ
@@ -1980,6 +1995,11 @@ func (m *CustomTypeMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *CustomTypeMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (CustomType).
func (m *CustomTypeMutation) Type() string {
return m.typ
@@ -2309,6 +2329,11 @@ func (m *GroupMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *GroupMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Group).
func (m *GroupMutation) Type() string {
return m.typ
@@ -2708,6 +2733,11 @@ func (m *MediaMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *MediaMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Media).
func (m *MediaMutation) Type() string {
return m.typ
@@ -3128,6 +3158,11 @@ func (m *PetMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *PetMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Pet).
func (m *PetMutation) Type() string {
return m.typ
@@ -4543,6 +4578,11 @@ func (m *UserMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *UserMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (User).
func (m *UserMutation) Type() string {
return m.typ
@@ -5321,6 +5361,11 @@ func (m *ZooMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *ZooMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Zoo).
func (m *ZooMutation) Type() string {
return m.typ