ent/field: add update_default option time field

Reviewed By: alexsn

Differential Revision: D17070907

fbshipit-source-id: 63c9ce75c58e524044c38f9461cb04e8e45c8017
This commit is contained in:
Ariel Mashraki
2019-08-27 06:53:44 -07:00
committed by Facebook Github Bot
parent bd07c86b60
commit 772b8a33f8
30 changed files with 376 additions and 100 deletions

View File

@@ -303,6 +303,18 @@ func (b *timeBuilder) Default(f func() time.Time) *timeBuilder {
return b
}
// UpdateDefault sets the function that is applied to set default value
// of the field on update. For example:
//
// field.Time("updated_at").
// Default(time.Now).
// UpdateDefault(time.Now),
//
func (b *timeBuilder) UpdateDefault(f func() time.Time) *timeBuilder {
b.desc.UpdateDefault = f
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *timeBuilder) Descriptor() *Descriptor {
return b.desc

View File

@@ -100,6 +100,14 @@ func TestTime(t *testing.T) {
assert.Equal(t, "time.Time", fd.Type.String())
assert.NotNil(t, fd.Default)
assert.Equal(t, now, fd.Default.(func() time.Time)())
fd = field.Time("updated_at").
UpdateDefault(func() time.Time {
return now
}).
Descriptor()
assert.Equal(t, "updated_at", fd.Name)
assert.Equal(t, now, fd.UpdateDefault.(func() time.Time)())
}
func TestField_Tag(t *testing.T) {