mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
ent/field: add default value for time
Reviewed By: alexsn Differential Revision: D16890453 fbshipit-source-id: 362ae9e9666c523bdcce16503441565b6279ff08
This commit is contained in:
committed by
Facebook Github Bot
parent
51a4dd4412
commit
079ba191e3
@@ -6,6 +6,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"fbc/ent/dialect/gremlin"
|
||||
"fbc/ent/dialect/sql"
|
||||
@@ -18,23 +19,28 @@ type Card struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
// Number holds the value of the "number" field.
|
||||
Number string `json:"number,omitempty"`
|
||||
// CreatedAt holds the value of the "created_at" field.
|
||||
CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
// FromRows scans the sql response data into Card.
|
||||
func (c *Card) FromRows(rows *sql.Rows) error {
|
||||
var vc struct {
|
||||
ID int
|
||||
Number string
|
||||
ID int
|
||||
Number string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
// the order here should be the same as in the `card.Columns`.
|
||||
if err := rows.Scan(
|
||||
&vc.ID,
|
||||
&vc.Number,
|
||||
&vc.CreatedAt,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
c.ID = strconv.Itoa(vc.ID)
|
||||
c.Number = vc.Number
|
||||
c.CreatedAt = vc.CreatedAt
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -45,14 +51,16 @@ func (c *Card) FromResponse(res *gremlin.Response) error {
|
||||
return err
|
||||
}
|
||||
var vc struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Number string `json:"number,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Number string `json:"number,omitempty"`
|
||||
CreatedAt int64 `json:"created_at,omitempty"`
|
||||
}
|
||||
if err := vmap.Decode(&vc); err != nil {
|
||||
return err
|
||||
}
|
||||
c.ID = vc.ID
|
||||
c.Number = vc.Number
|
||||
c.CreatedAt = time.Unix(vc.CreatedAt, 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -85,6 +93,7 @@ func (c *Card) String() string {
|
||||
buf.WriteString("Card(")
|
||||
buf.WriteString(fmt.Sprintf("id=%v", c.ID))
|
||||
buf.WriteString(fmt.Sprintf(", number=%v", c.Number))
|
||||
buf.WriteString(fmt.Sprintf(", created_at=%v", c.CreatedAt))
|
||||
buf.WriteString(")")
|
||||
return buf.String()
|
||||
}
|
||||
@@ -117,16 +126,18 @@ func (c *Cards) FromResponse(res *gremlin.Response) error {
|
||||
return err
|
||||
}
|
||||
var vc []struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Number string `json:"number,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Number string `json:"number,omitempty"`
|
||||
CreatedAt int64 `json:"created_at,omitempty"`
|
||||
}
|
||||
if err := vmap.Decode(&vc); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range vc {
|
||||
*c = append(*c, &Card{
|
||||
ID: v.ID,
|
||||
Number: v.Number,
|
||||
ID: v.ID,
|
||||
Number: v.Number,
|
||||
CreatedAt: time.Unix(v.CreatedAt, 0),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
package card
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"fbc/ent/entc/integration/ent/schema"
|
||||
)
|
||||
|
||||
@@ -13,6 +15,8 @@ const (
|
||||
FieldID = "id"
|
||||
// FieldNumber holds the string denoting the number vertex property in the database.
|
||||
FieldNumber = "number"
|
||||
// FieldCreatedAt holds the string denoting the created_at vertex property in the database.
|
||||
FieldCreatedAt = "created_at"
|
||||
|
||||
// Table holds the table name of the card in the database.
|
||||
Table = "cards"
|
||||
@@ -32,10 +36,13 @@ const (
|
||||
var Columns = []string{
|
||||
FieldID,
|
||||
FieldNumber,
|
||||
FieldCreatedAt,
|
||||
}
|
||||
|
||||
var (
|
||||
fields = schema.Card{}.Fields()
|
||||
// NumberValidator is a validator for the "number" field. It is called by the builders before save.
|
||||
NumberValidator = fields[0].Validators()[0].(func(string) error)
|
||||
// DefaultCreatedAt holds the default value for the created_at field.
|
||||
DefaultCreatedAt = fields[1].Value().(func() time.Time)
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ package card
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"fbc/ent/entc/integration/ent/predicate"
|
||||
|
||||
@@ -168,6 +169,18 @@ func Number(v string) predicate.Card {
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ.
|
||||
func CreatedAt(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.EQ(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// NumberEQ applies the EQ predicate on the "number" field.
|
||||
func NumberEQ(v string) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
@@ -320,6 +333,122 @@ func NumberHasSuffix(v string) predicate.Card {
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
|
||||
func CreatedAtEQ(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.EQ(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.EQ(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtNEQ applies the NEQ predicate on the "created_at" field.
|
||||
func CreatedAtNEQ(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.NEQ(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.NEQ(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtGT applies the GT predicate on the "created_at" field.
|
||||
func CreatedAtGT(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.GT(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.GT(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtGTE applies the GTE predicate on the "created_at" field.
|
||||
func CreatedAtGTE(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.GTE(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.GTE(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtLT applies the LT predicate on the "created_at" field.
|
||||
func CreatedAtLT(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.LT(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.LT(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtLTE applies the LTE predicate on the "created_at" field.
|
||||
func CreatedAtLTE(v time.Time) predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
s.Where(sql.LTE(s.C(FieldCreatedAt), v))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.LTE(v))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtIn applies the In predicate on the "created_at" field.
|
||||
func CreatedAtIn(vs ...time.Time) predicate.Card {
|
||||
v := make([]interface{}, len(vs))
|
||||
for i := range v {
|
||||
v[i] = vs[i]
|
||||
}
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
// if not arguments were provided, append the FALSE constants,
|
||||
// since we can't apply "IN ()". This will make this predicate falsy.
|
||||
if len(vs) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
s.Where(sql.In(s.C(FieldCreatedAt), v...))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.Within(v...))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// CreatedAtNotIn applies the NotIn predicate on the "created_at" field.
|
||||
func CreatedAtNotIn(vs ...time.Time) predicate.Card {
|
||||
v := make([]interface{}, len(vs))
|
||||
for i := range v {
|
||||
v[i] = vs[i]
|
||||
}
|
||||
return predicate.CardPerDialect(
|
||||
func(s *sql.Selector) {
|
||||
// if not arguments were provided, append the FALSE constants,
|
||||
// since we can't apply "IN ()". This will make this predicate falsy.
|
||||
if len(vs) == 0 {
|
||||
s.Where(sql.False())
|
||||
return
|
||||
}
|
||||
s.Where(sql.NotIn(s.C(FieldCreatedAt), v...))
|
||||
},
|
||||
func(t *dsl.Traversal) {
|
||||
t.Has(Label, FieldCreatedAt, p.Without(v...))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// HasOwner applies the HasEdge predicate on the "owner" edge.
|
||||
func HasOwner() predicate.Card {
|
||||
return predicate.CardPerDialect(
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"fbc/ent/entc/integration/ent/card"
|
||||
"fbc/ent/entc/integration/ent/user"
|
||||
@@ -23,8 +24,9 @@ import (
|
||||
// CardCreate is the builder for creating a Card entity.
|
||||
type CardCreate struct {
|
||||
config
|
||||
number *string
|
||||
owner map[string]struct{}
|
||||
number *string
|
||||
created_at *time.Time
|
||||
owner map[string]struct{}
|
||||
}
|
||||
|
||||
// SetNumber sets the number field.
|
||||
@@ -33,6 +35,20 @@ func (cc *CardCreate) SetNumber(s string) *CardCreate {
|
||||
return cc
|
||||
}
|
||||
|
||||
// SetCreatedAt sets the created_at field.
|
||||
func (cc *CardCreate) SetCreatedAt(t time.Time) *CardCreate {
|
||||
cc.created_at = &t
|
||||
return cc
|
||||
}
|
||||
|
||||
// SetNillableCreatedAt sets the created_at field if the given value is not nil.
|
||||
func (cc *CardCreate) SetNillableCreatedAt(t *time.Time) *CardCreate {
|
||||
if t != nil {
|
||||
cc.SetCreatedAt(*t)
|
||||
}
|
||||
return cc
|
||||
}
|
||||
|
||||
// SetOwnerID sets the owner edge to User by id.
|
||||
func (cc *CardCreate) SetOwnerID(id string) *CardCreate {
|
||||
if cc.owner == nil {
|
||||
@@ -63,6 +79,10 @@ func (cc *CardCreate) Save(ctx context.Context) (*Card, error) {
|
||||
if err := card.NumberValidator(*cc.number); err != nil {
|
||||
return nil, fmt.Errorf("ent: validator failed for field \"number\": %v", err)
|
||||
}
|
||||
if cc.created_at == nil {
|
||||
v := card.DefaultCreatedAt()
|
||||
cc.created_at = &v
|
||||
}
|
||||
if len(cc.owner) > 1 {
|
||||
return nil, errors.New("ent: multiple assignments on a unique edge \"owner\"")
|
||||
}
|
||||
@@ -99,6 +119,10 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) {
|
||||
builder.Set(card.FieldNumber, *cc.number)
|
||||
c.Number = *cc.number
|
||||
}
|
||||
if cc.created_at != nil {
|
||||
builder.Set(card.FieldCreatedAt, *cc.created_at)
|
||||
c.CreatedAt = *cc.created_at
|
||||
}
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
return nil, rollback(tx, err)
|
||||
@@ -160,6 +184,9 @@ func (cc *CardCreate) gremlin() *dsl.Traversal {
|
||||
if cc.number != nil {
|
||||
v.Property(dsl.Single, card.FieldNumber, *cc.number)
|
||||
}
|
||||
if cc.created_at != nil {
|
||||
v.Property(dsl.Single, card.FieldCreatedAt, *cc.created_at)
|
||||
}
|
||||
for id := range cc.owner {
|
||||
v.AddE(user.CardLabel).From(g.V(id)).InV()
|
||||
constraints = append(constraints, &constraint{
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"fbc/ent/entc/integration/ent/card"
|
||||
"fbc/ent/entc/integration/ent/predicate"
|
||||
@@ -25,6 +26,7 @@ import (
|
||||
type CardUpdate struct {
|
||||
config
|
||||
number *string
|
||||
created_at *time.Time
|
||||
owner map[string]struct{}
|
||||
clearedOwner bool
|
||||
predicates []predicate.Card
|
||||
@@ -42,6 +44,20 @@ func (cu *CardUpdate) SetNumber(s string) *CardUpdate {
|
||||
return cu
|
||||
}
|
||||
|
||||
// SetCreatedAt sets the created_at field.
|
||||
func (cu *CardUpdate) SetCreatedAt(t time.Time) *CardUpdate {
|
||||
cu.created_at = &t
|
||||
return cu
|
||||
}
|
||||
|
||||
// SetNillableCreatedAt sets the created_at field if the given value is not nil.
|
||||
func (cu *CardUpdate) SetNillableCreatedAt(t *time.Time) *CardUpdate {
|
||||
if t != nil {
|
||||
cu.SetCreatedAt(*t)
|
||||
}
|
||||
return cu
|
||||
}
|
||||
|
||||
// SetOwnerID sets the owner edge to User by id.
|
||||
func (cu *CardUpdate) SetOwnerID(id string) *CardUpdate {
|
||||
if cu.owner == nil {
|
||||
@@ -148,6 +164,10 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
update = true
|
||||
builder.Set(card.FieldNumber, *cu.number)
|
||||
}
|
||||
if cu.created_at != nil {
|
||||
update = true
|
||||
builder.Set(card.FieldCreatedAt, *cu.created_at)
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
@@ -220,6 +240,9 @@ func (cu *CardUpdate) gremlin() *dsl.Traversal {
|
||||
if cu.number != nil {
|
||||
v.Property(dsl.Single, card.FieldNumber, *cu.number)
|
||||
}
|
||||
if cu.created_at != nil {
|
||||
v.Property(dsl.Single, card.FieldCreatedAt, *cu.created_at)
|
||||
}
|
||||
if cu.clearedOwner {
|
||||
tr := rv.Clone().InE(user.CardLabel).Drop().Iterate()
|
||||
trs = append(trs, tr)
|
||||
@@ -251,6 +274,7 @@ type CardUpdateOne struct {
|
||||
config
|
||||
id string
|
||||
number *string
|
||||
created_at *time.Time
|
||||
owner map[string]struct{}
|
||||
clearedOwner bool
|
||||
}
|
||||
@@ -261,6 +285,20 @@ func (cuo *CardUpdateOne) SetNumber(s string) *CardUpdateOne {
|
||||
return cuo
|
||||
}
|
||||
|
||||
// SetCreatedAt sets the created_at field.
|
||||
func (cuo *CardUpdateOne) SetCreatedAt(t time.Time) *CardUpdateOne {
|
||||
cuo.created_at = &t
|
||||
return cuo
|
||||
}
|
||||
|
||||
// SetNillableCreatedAt sets the created_at field if the given value is not nil.
|
||||
func (cuo *CardUpdateOne) SetNillableCreatedAt(t *time.Time) *CardUpdateOne {
|
||||
if t != nil {
|
||||
cuo.SetCreatedAt(*t)
|
||||
}
|
||||
return cuo
|
||||
}
|
||||
|
||||
// SetOwnerID sets the owner edge to User by id.
|
||||
func (cuo *CardUpdateOne) SetOwnerID(id string) *CardUpdateOne {
|
||||
if cuo.owner == nil {
|
||||
@@ -371,6 +409,11 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (c *Card, err error) {
|
||||
builder.Set(card.FieldNumber, *cuo.number)
|
||||
c.Number = *cuo.number
|
||||
}
|
||||
if cuo.created_at != nil {
|
||||
update = true
|
||||
builder.Set(card.FieldCreatedAt, *cuo.created_at)
|
||||
c.CreatedAt = *cuo.created_at
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
if err := tx.Exec(ctx, query, args, &res); err != nil {
|
||||
@@ -444,6 +487,9 @@ func (cuo *CardUpdateOne) gremlin(id string) *dsl.Traversal {
|
||||
if cuo.number != nil {
|
||||
v.Property(dsl.Single, card.FieldNumber, *cuo.number)
|
||||
}
|
||||
if cuo.created_at != nil {
|
||||
v.Property(dsl.Single, card.FieldCreatedAt, *cuo.created_at)
|
||||
}
|
||||
if cuo.clearedOwner {
|
||||
tr := rv.Clone().InE(user.CardLabel).Drop().Iterate()
|
||||
trs = append(trs, tr)
|
||||
|
||||
@@ -33,6 +33,7 @@ func ExampleCard() {
|
||||
c := client.Card.
|
||||
Create().
|
||||
SetNumber("string").
|
||||
SetCreatedAt(time.Now()).
|
||||
SaveX(ctx)
|
||||
log.Println("card created:", c)
|
||||
|
||||
@@ -299,6 +300,7 @@ func ExampleUser() {
|
||||
c0 := client.Card.
|
||||
Create().
|
||||
SetNumber("string").
|
||||
SetCreatedAt(time.Now()).
|
||||
SaveX(ctx)
|
||||
log.Println("card created:", c0)
|
||||
pe1 := client.Pet.
|
||||
|
||||
@@ -16,6 +16,7 @@ var (
|
||||
CardsColumns = []*schema.Column{
|
||||
{Name: "id", Type: field.TypeInt, Increment: true},
|
||||
{Name: "number", Type: field.TypeString},
|
||||
{Name: "created_at", Type: field.TypeTime},
|
||||
{Name: "owner_id", Type: field.TypeInt, Unique: true, Nullable: true},
|
||||
}
|
||||
// CardsTable holds the schema information for the "cards" table.
|
||||
@@ -26,7 +27,7 @@ var (
|
||||
ForeignKeys: []*schema.ForeignKey{
|
||||
{
|
||||
Symbol: "cards_users_card",
|
||||
Columns: []*schema.Column{CardsColumns[2]},
|
||||
Columns: []*schema.Column{CardsColumns[3]},
|
||||
|
||||
RefColumns: []*schema.Column{UsersColumns[0]},
|
||||
OnDelete: schema.SetNull,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"fbc/ent"
|
||||
"fbc/ent/schema/edge"
|
||||
"fbc/ent/schema/field"
|
||||
@@ -16,6 +18,8 @@ func (Card) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("number").
|
||||
MinLen(1),
|
||||
field.Time("created_at").
|
||||
Default(time.Now),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -116,6 +116,7 @@ var tests = []func(*testing.T, *ent.Client){
|
||||
M2MSelfRef,
|
||||
M2MSameType,
|
||||
M2MTwoTypes,
|
||||
DefaultValue,
|
||||
}
|
||||
|
||||
func Sanity(t *testing.T, client *ent.Client) {
|
||||
@@ -1836,6 +1837,12 @@ func Tx(t *testing.T, client *ent.Client) {
|
||||
require.NoError(tx.Rollback())
|
||||
}
|
||||
|
||||
func DefaultValue(t *testing.T, client *ent.Client) {
|
||||
ctx := context.Background()
|
||||
c1 := client.Card.Create().SetNumber("102030").SaveX(ctx)
|
||||
require.False(t, c1.CreatedAt.IsZero())
|
||||
}
|
||||
|
||||
func drop(t *testing.T, client *ent.Client) {
|
||||
t.Log("drop data from database")
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user