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

@@ -127,6 +127,16 @@ func (c *Client) Use(hooks ...Hook) {
c.Node.Use(hooks...)
}
// Mutate implements the ent.Mutator interface.
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
switch m := m.(type) {
case *NodeMutation:
return c.Node.mutate(ctx, m)
default:
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
}
}
// NodeClient is a client for the Node schema.
type NodeClient struct {
config
@@ -248,3 +258,18 @@ func (c *NodeClient) QueryChildren(n *Node) *NodeQuery {
func (c *NodeClient) Hooks() []Hook {
return c.hooks.Node
}
func (c *NodeClient) mutate(ctx context.Context, m *NodeMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&NodeCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&NodeUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&NodeUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&NodeDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Node mutation op: %q", m.Op())
}
}

View File

@@ -306,6 +306,11 @@ func (m *NodeMutation) Op() Op {
return m.op
}
// SetOp allows setting the mutation operation.
func (m *NodeMutation) SetOp(op Op) {
m.op = op
}
// Type returns the node type of this mutation (Node).
func (m *NodeMutation) Type() string {
return m.typ