From 7e3785f10551a9c7b6decfadfafe6b413c83bdd3 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Thu, 12 Aug 2021 20:51:56 +0300 Subject: [PATCH] entc/integ: add generated-id upsert example --- entc/integration/ent/item/item.go | 2 ++ entc/integration/ent/item_create.go | 18 ++++++++++++++++++ entc/integration/ent/runtime.go | 2 ++ entc/integration/ent/schema/item.go | 3 +++ entc/integration/gremlin/ent/item/item.go | 2 ++ entc/integration/gremlin/ent/item_create.go | 17 +++++++++++++++++ entc/integration/gremlin/ent/runtime.go | 2 ++ entc/integration/integration_test.go | 12 ++++++++++-- 8 files changed, 56 insertions(+), 2 deletions(-) diff --git a/entc/integration/ent/item/item.go b/entc/integration/ent/item/item.go index 8615d76df..82871efaa 100644 --- a/entc/integration/ent/item/item.go +++ b/entc/integration/ent/item/item.go @@ -31,6 +31,8 @@ func ValidColumn(column string) bool { } var ( + // DefaultID holds the default value on creation for the "id" field. + DefaultID func() string // IDValidator is a validator for the "id" field. It is called by the builders before save. IDValidator func(string) error ) diff --git a/entc/integration/ent/item_create.go b/entc/integration/ent/item_create.go index 5caa22799..3be899afd 100644 --- a/entc/integration/ent/item_create.go +++ b/entc/integration/ent/item_create.go @@ -31,6 +31,14 @@ func (ic *ItemCreate) SetID(s string) *ItemCreate { return ic } +// SetNillableID sets the "id" field if the given value is not nil. +func (ic *ItemCreate) SetNillableID(s *string) *ItemCreate { + if s != nil { + ic.SetID(*s) + } + return ic +} + // Mutation returns the ItemMutation object of the builder. func (ic *ItemCreate) Mutation() *ItemMutation { return ic.mutation @@ -42,6 +50,7 @@ func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) { err error node *Item ) + ic.defaults() if len(ic.hooks) == 0 { if err = ic.check(); err != nil { return nil, err @@ -99,6 +108,14 @@ func (ic *ItemCreate) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (ic *ItemCreate) defaults() { + if _, ok := ic.mutation.ID(); !ok { + v := item.DefaultID() + ic.mutation.SetID(v) + } +} + // check runs all checks and user-defined validators on the builder. func (ic *ItemCreate) check() error { if v, ok := ic.mutation.ID(); ok { @@ -272,6 +289,7 @@ func (icb *ItemCreateBulk) Save(ctx context.Context) ([]*Item, error) { for i := range icb.builders { func(i int, root context.Context) { builder := icb.builders[i] + builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*ItemMutation) if !ok { diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index 3cf412f72..bb2f34ba9 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -179,6 +179,8 @@ func init() { _ = itemFields // itemDescID is the schema descriptor for id field. itemDescID := itemFields[0].Descriptor() + // item.DefaultID holds the default value on creation for the id field. + item.DefaultID = itemDescID.Default.(func() string) // item.IDValidator is a validator for the "id" field. It is called by the builders before save. item.IDValidator = itemDescID.Validators[0].(func(string) error) petFields := schema.Pet{}.Fields() diff --git a/entc/integration/ent/schema/item.go b/entc/integration/ent/schema/item.go index 0b2e7d8a2..b60214e64 100644 --- a/entc/integration/ent/schema/item.go +++ b/entc/integration/ent/schema/item.go @@ -7,6 +7,8 @@ package schema import ( "entgo.io/ent" "entgo.io/ent/schema/field" + + "github.com/google/uuid" ) // Item holds the schema definition for the Item entity. @@ -18,6 +20,7 @@ type Item struct { func (Item) Fields() []ent.Field { return []ent.Field{ field.String("id"). + DefaultFunc(uuid.NewString). MaxLen(64), } } diff --git a/entc/integration/gremlin/ent/item/item.go b/entc/integration/gremlin/ent/item/item.go index 1a8bbc728..e40bf77c2 100644 --- a/entc/integration/gremlin/ent/item/item.go +++ b/entc/integration/gremlin/ent/item/item.go @@ -14,6 +14,8 @@ const ( ) var ( + // DefaultID holds the default value on creation for the "id" field. + DefaultID func() string // IDValidator is a validator for the "id" field. It is called by the builders before save. IDValidator func(string) error ) diff --git a/entc/integration/gremlin/ent/item_create.go b/entc/integration/gremlin/ent/item_create.go index a7bae357d..31dc5bfe7 100644 --- a/entc/integration/gremlin/ent/item_create.go +++ b/entc/integration/gremlin/ent/item_create.go @@ -29,6 +29,14 @@ func (ic *ItemCreate) SetID(s string) *ItemCreate { return ic } +// SetNillableID sets the "id" field if the given value is not nil. +func (ic *ItemCreate) SetNillableID(s *string) *ItemCreate { + if s != nil { + ic.SetID(*s) + } + return ic +} + // Mutation returns the ItemMutation object of the builder. func (ic *ItemCreate) Mutation() *ItemMutation { return ic.mutation @@ -40,6 +48,7 @@ func (ic *ItemCreate) Save(ctx context.Context) (*Item, error) { err error node *Item ) + ic.defaults() if len(ic.hooks) == 0 { if err = ic.check(); err != nil { return nil, err @@ -97,6 +106,14 @@ func (ic *ItemCreate) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (ic *ItemCreate) defaults() { + if _, ok := ic.mutation.ID(); !ok { + v := item.DefaultID() + ic.mutation.SetID(v) + } +} + // check runs all checks and user-defined validators on the builder. func (ic *ItemCreate) check() error { if v, ok := ic.mutation.ID(); ok { diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 9904058a0..ddb9ed8a1 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -179,6 +179,8 @@ func init() { _ = itemFields // itemDescID is the schema descriptor for id field. itemDescID := itemFields[0].Descriptor() + // item.DefaultID holds the default value on creation for the id field. + item.DefaultID = itemDescID.Default.(func() string) // item.IDValidator is a validator for the "id" field. It is called by the builders before save. item.IDValidator = itemDescID.Validators[0].(func(string) error) petFields := schema.Pet{}.Fields() diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index 1a6ac9a37..d7ec6bd9c 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -308,8 +308,16 @@ func Upsert(t *testing.T, client *ent.Client) { require.Equal(t, "B", users[1].Name) // Setting primary key. - client.Item.Create().SetID("A").SaveX(ctx) - client.Item.Create().SetID("A").OnConflict(sql.ConflictColumns(item.FieldID)).Ignore().ExecX(ctx) + a := client.Item.Create().SetID("A").SaveX(ctx) + require.Equal(t, "A", a.ID) + aid := client.Item.Create().SetID("A").OnConflict(sql.ConflictColumns(item.FieldID)).Ignore().IDX(ctx) + require.Equal(t, a.ID, aid) + + // Primary key is generated. + b := client.Item.Create().SaveX(ctx) + require.NotZero(t, b.ID) + bid := client.Item.Create().SetID(b.ID).OnConflictColumns(item.FieldID).Ignore().IDX(ctx) + require.Equal(t, b.ID, bid) } func Clone(t *testing.T, client *ent.Client) {