entc/gen: add indexes, edges and hooks to mixin (#431)

This commit is contained in:
Ariel Mashraki
2020-04-20 13:40:56 +03:00
committed by GitHub
parent cec1dd1edf
commit 1c49159d18
22 changed files with 454 additions and 249 deletions

View File

@@ -55,7 +55,7 @@ var ForeignKeys = []string{
// import _ "github.com/facebookincubator/ent/entc/integration/hooks/ent/runtime"
//
var (
Hooks [2]ent.Hook
Hooks [3]ent.Hook
// DefaultNumber holds the default value on creation for the number field.
DefaultNumber string
// NumberValidator is a validator for the "number" field. It is called by the builders before save.

View File

@@ -11,16 +11,20 @@ import (
"github.com/facebookincubator/ent/entc/integration/hooks/ent/card"
"github.com/facebookincubator/ent/entc/integration/hooks/ent/schema"
"github.com/facebookincubator/ent"
)
// The init function reads all schema descriptors with runtime
// code (default values, validators or hooks) and stitches it
// to their package variables.
func init() {
cardMixin := schema.Card{}.Mixin()
cardMixinHooks0 := cardMixin[0].(interface{ Hooks() []ent.Hook }).Hooks()
cardHooks := schema.Card{}.Hooks()
for i, h := range cardHooks {
card.Hooks[i] = h
}
card.Hooks[0] = cardMixinHooks0[0]
card.Hooks[1] = cardHooks[0]
card.Hooks[2] = cardHooks[1]
cardFields := schema.Card{}.Fields()
_ = cardFields
// cardDescNumber is the schema descriptor for number field.

View File

@@ -9,21 +9,38 @@ import (
"fmt"
"time"
"github.com/facebookincubator/ent/entc/integration/hooks/ent/card"
"github.com/facebookincubator/ent"
gen "github.com/facebookincubator/ent/entc/integration/hooks/ent"
"github.com/facebookincubator/ent/entc/integration/hooks/ent/card"
"github.com/facebookincubator/ent/entc/integration/hooks/ent/hook"
"github.com/facebookincubator/ent/schema/edge"
"github.com/facebookincubator/ent/schema/field"
"github.com/facebookincubator/ent/schema/mixin"
)
// RejectMany rejects all update operations
// that are not on a specific entity.
type RejectUpdate struct {
mixin.Schema
}
func (RejectUpdate) Hooks() []ent.Hook {
return []ent.Hook{
hook.Reject(ent.OpUpdate),
}
}
// Card holds the schema definition for the CreditCard entity.
type Card struct {
ent.Schema
}
func (Card) Mixin() []ent.Mixin {
return []ent.Mixin{
RejectUpdate{},
}
}
func (Card) Hooks() []ent.Hook {
return []ent.Hook{
hook.On(

View File

@@ -35,6 +35,8 @@ func TestSchemaHooks(t *testing.T) {
})
})
client.Card.Create().SetNumber("1234").SaveX(ctx)
_, err = client.Card.Update().Save(ctx)
require.EqualError(t, err, "OpUpdate operation is not allowed")
}
func TestRuntimeHooks(t *testing.T) {

View File

@@ -1,69 +0,0 @@
//+build tests
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/facebookincubator/ent/entc/integration/hooks/ent"
"github.com/facebookincubator/ent/entc/integration/hooks/ent/hook"
_ "github.com/facebookincubator/ent/entc/integration/hooks/ent/runtime"
_ "github.com/mattn/go-sqlite3"
)
func main() {
ctx := context.Background()
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
panic(err)
}
if err := client.Schema.Create(ctx); err != nil {
panic(err)
}
client.Use(func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
fmt.Println("start")
defer fmt.Println("end")
return next.Mutate(ctx, m)
})
})
client.Card.Use(func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
fmt.Printf("Before Hook\tOp: %s\tType: %s\tConcreteType: %T\n", m.Op(), m.Type(), m)
defer fmt.Println("Done!")
if ns, ok := m.(interface{ SetName(string) }); ok {
ns.SetName("hook name")
}
return next.Mutate(ctx, m)
})
})
client.Card.Use(func(next ent.Mutator) ent.Mutator {
return hook.CardFunc(func(ctx context.Context, m *ent.CardMutation) (ent.Value, error) {
fmt.Println("Concrete hook\t", m.Op())
return next.Mutate(ctx, m)
})
})
client.Use(hook.On(LogWithConfig(os.Stdout), ent.OpUpdate|ent.OpCreate))
u := client.Card.Create().SetNumber("A").SaveX(ctx)
u.Update().SetName("Boring2").SaveX(ctx)
client.Card.Update().SetName("foo").SaveX(ctx)
client.Card.DeleteOneID(u.ID).ExecX(ctx)
client.Card.Delete().ExecX(ctx)
}
func LogWithConfig(w io.Writer) ent.Hook {
if w == nil {
w = os.Stdout
}
return func(next ent.Mutator) ent.Mutator {
return hook.CardFunc(func(ctx context.Context, m *ent.CardMutation) (ent.Value, error) {
fmt.Fprintln(w, "Logging Hook:\t", m.Op())
return next.Mutate(ctx, m)
})
}
}