mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add indexes, edges and hooks to mixin (#431)
This commit is contained in:
@@ -16,8 +16,6 @@ import (
|
||||
"github.com/facebookincubator/ent/entc/integration/ent/groupinfo"
|
||||
"github.com/facebookincubator/ent/entc/integration/ent/schema"
|
||||
"github.com/facebookincubator/ent/entc/integration/ent/user"
|
||||
|
||||
"github.com/facebookincubator/ent"
|
||||
)
|
||||
|
||||
// The init function reads all schema descriptors with runtime
|
||||
@@ -25,17 +23,15 @@ import (
|
||||
// to their package variables.
|
||||
func init() {
|
||||
cardMixin := schema.Card{}.Mixin()
|
||||
cardMixinFields := [...][]ent.Field{
|
||||
cardMixin[0].Fields(),
|
||||
}
|
||||
cardMixinFields0 := cardMixin[0].Fields()
|
||||
cardFields := schema.Card{}.Fields()
|
||||
_ = cardFields
|
||||
// cardDescCreateTime is the schema descriptor for create_time field.
|
||||
cardDescCreateTime := cardMixinFields[0][0].Descriptor()
|
||||
cardDescCreateTime := cardMixinFields0[0].Descriptor()
|
||||
// card.DefaultCreateTime holds the default value on creation for the create_time field.
|
||||
card.DefaultCreateTime = cardDescCreateTime.Default.(func() time.Time)
|
||||
// cardDescUpdateTime is the schema descriptor for update_time field.
|
||||
cardDescUpdateTime := cardMixinFields[0][1].Descriptor()
|
||||
cardDescUpdateTime := cardMixinFields0[1].Descriptor()
|
||||
// card.DefaultUpdateTime holds the default value on creation for the update_time field.
|
||||
card.DefaultUpdateTime = cardDescUpdateTime.Default.(func() time.Time)
|
||||
// card.UpdateDefaultUpdateTime holds the default value on update for the update_time field.
|
||||
@@ -117,13 +113,11 @@ func init() {
|
||||
// groupinfo.DefaultMaxUsers holds the default value on creation for the max_users field.
|
||||
groupinfo.DefaultMaxUsers = groupinfoDescMaxUsers.Default.(int)
|
||||
userMixin := schema.User{}.Mixin()
|
||||
userMixinFields := [...][]ent.Field{
|
||||
userMixin[0].Fields(),
|
||||
}
|
||||
userMixinFields0 := userMixin[0].Fields()
|
||||
userFields := schema.User{}.Fields()
|
||||
_ = userFields
|
||||
// userDescOptionalInt is the schema descriptor for optional_int field.
|
||||
userDescOptionalInt := userMixinFields[0][0].Descriptor()
|
||||
userDescOptionalInt := userMixinFields0[0].Descriptor()
|
||||
// user.OptionalIntValidator is a validator for the "optional_int" field. It is called by the builders before save.
|
||||
user.OptionalIntValidator = userDescOptionalInt.Validators[0].(func(int) error)
|
||||
// userDescLast is the schema descriptor for last field.
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/facebookincubator/ent"
|
||||
"github.com/facebookincubator/ent/schema/edge"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
"github.com/facebookincubator/ent/schema/schemautil"
|
||||
"github.com/facebookincubator/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// Card holds the schema definition for the CreditCard entity.
|
||||
@@ -18,7 +18,7 @@ type Card struct {
|
||||
|
||||
func (Card) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
schemautil.TimeMixin{},
|
||||
mixin.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/facebookincubator/ent"
|
||||
"github.com/facebookincubator/ent/schema/edge"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
"github.com/facebookincubator/ent/schema/mixin"
|
||||
)
|
||||
|
||||
// User holds the schema for the user entity.
|
||||
@@ -63,7 +64,9 @@ func (User) Edges() []ent.Edge {
|
||||
}
|
||||
|
||||
// UserMixin composes create/update time mixin.
|
||||
type UserMixin struct{}
|
||||
type UserMixin struct {
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
// Fields of the time mixin.
|
||||
func (UserMixin) Fields() []ent.Field {
|
||||
|
||||
@@ -16,8 +16,6 @@ import (
|
||||
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/group"
|
||||
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/groupinfo"
|
||||
"github.com/facebookincubator/ent/entc/integration/gremlin/ent/user"
|
||||
|
||||
"github.com/facebookincubator/ent"
|
||||
)
|
||||
|
||||
// The init function reads all schema descriptors with runtime
|
||||
@@ -25,17 +23,15 @@ import (
|
||||
// to their package variables.
|
||||
func init() {
|
||||
cardMixin := schema.Card{}.Mixin()
|
||||
cardMixinFields := [...][]ent.Field{
|
||||
cardMixin[0].Fields(),
|
||||
}
|
||||
cardMixinFields0 := cardMixin[0].Fields()
|
||||
cardFields := schema.Card{}.Fields()
|
||||
_ = cardFields
|
||||
// cardDescCreateTime is the schema descriptor for create_time field.
|
||||
cardDescCreateTime := cardMixinFields[0][0].Descriptor()
|
||||
cardDescCreateTime := cardMixinFields0[0].Descriptor()
|
||||
// card.DefaultCreateTime holds the default value on creation for the create_time field.
|
||||
card.DefaultCreateTime = cardDescCreateTime.Default.(func() time.Time)
|
||||
// cardDescUpdateTime is the schema descriptor for update_time field.
|
||||
cardDescUpdateTime := cardMixinFields[0][1].Descriptor()
|
||||
cardDescUpdateTime := cardMixinFields0[1].Descriptor()
|
||||
// card.DefaultUpdateTime holds the default value on creation for the update_time field.
|
||||
card.DefaultUpdateTime = cardDescUpdateTime.Default.(func() time.Time)
|
||||
// card.UpdateDefaultUpdateTime holds the default value on update for the update_time field.
|
||||
@@ -117,13 +113,11 @@ func init() {
|
||||
// groupinfo.DefaultMaxUsers holds the default value on creation for the max_users field.
|
||||
groupinfo.DefaultMaxUsers = groupinfoDescMaxUsers.Default.(int)
|
||||
userMixin := schema.User{}.Mixin()
|
||||
userMixinFields := [...][]ent.Field{
|
||||
userMixin[0].Fields(),
|
||||
}
|
||||
userMixinFields0 := userMixin[0].Fields()
|
||||
userFields := schema.User{}.Fields()
|
||||
_ = userFields
|
||||
// userDescOptionalInt is the schema descriptor for optional_int field.
|
||||
userDescOptionalInt := userMixinFields[0][0].Descriptor()
|
||||
userDescOptionalInt := userMixinFields0[0].Descriptor()
|
||||
// user.OptionalIntValidator is a validator for the "optional_int" field. It is called by the builders before save.
|
||||
user.OptionalIntValidator = userDescOptionalInt.Validators[0].(func(int) error)
|
||||
// userDescLast is the schema descriptor for last field.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -45,9 +45,8 @@ func init() {
|
||||
})
|
||||
}
|
||||
planetHooks := schema.Planet{}.Hooks()
|
||||
for i, h := range planetHooks {
|
||||
planet.Hooks[i+1] = h
|
||||
}
|
||||
|
||||
planet.Hooks[1] = planetHooks[0]
|
||||
planetFields := schema.Planet{}.Fields()
|
||||
_ = planetFields
|
||||
// planetDescName is the schema descriptor for name field.
|
||||
|
||||
Reference in New Issue
Block a user