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

@@ -148,6 +148,22 @@ func (c *Client) Use(hooks ...Hook) {
c.User.Use(hooks...)
}
// Mutate implements the ent.Mutator interface.
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
switch m := m.(type) {
case *FriendshipMutation:
return c.Friendship.mutate(ctx, m)
case *GroupMutation:
return c.Group.mutate(ctx, m)
case *PetMutation:
return c.Pet.mutate(ctx, m)
case *UserMutation:
return c.User.mutate(ctx, m)
default:
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
}
}
// FriendshipClient is a client for the Friendship schema.
type FriendshipClient struct {
config
@@ -276,6 +292,21 @@ func (c *FriendshipClient) Hooks() []Hook {
return c.hooks.Friendship
}
func (c *FriendshipClient) mutate(ctx context.Context, m *FriendshipMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&FriendshipCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&FriendshipUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&FriendshipUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&FriendshipDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Friendship mutation op: %q", m.Op())
}
}
// GroupClient is a client for the Group schema.
type GroupClient struct {
config
@@ -385,6 +416,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("ent: unknown Group mutation op: %q", m.Op())
}
}
// PetClient is a client for the Pet schema.
type PetClient struct {
config
@@ -494,6 +540,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("ent: unknown Pet mutation op: %q", m.Op())
}
}
// UserClient is a client for the User schema.
type UserClient struct {
config
@@ -659,3 +720,18 @@ func (c *UserClient) QueryFriendships(u *User) *FriendshipQuery {
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("ent: unknown User mutation op: %q", m.Op())
}
}

View File

@@ -380,6 +380,11 @@ func (m *FriendshipMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *FriendshipMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Friendship).
func (m *FriendshipMutation) Type() string {
return m.typ
@@ -858,6 +863,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
@@ -1282,6 +1292,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
@@ -1876,6 +1891,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