mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: make generated client to implement the ent.Mutator interface (#3161)
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user