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 (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Type is a field type.
|
||||
@@ -482,6 +483,17 @@ func (b *timeBuilder) StructTag(s string) *timeBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
// Default sets the function that is applied to set default value
|
||||
// of the field on creation. For example:
|
||||
//
|
||||
// field.Time("created_at").
|
||||
// Default(time.Now)
|
||||
//
|
||||
func (b *timeBuilder) Default(f func() time.Time) *timeBuilder {
|
||||
b.value = f
|
||||
return b
|
||||
}
|
||||
|
||||
// boolBuilder is the builder for boolean fields.
|
||||
type boolBuilder struct {
|
||||
Field
|
||||
|
||||
@@ -3,6 +3,7 @@ package field_test
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"fbc/ent"
|
||||
"fbc/ent/schema/field"
|
||||
@@ -75,10 +76,16 @@ func TestCharset(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
f := field.Time("created_at")
|
||||
now := time.Now()
|
||||
f := field.Time("created_at").
|
||||
Default(func() time.Time {
|
||||
return now
|
||||
})
|
||||
assert.Equal(t, "created_at", f.Name())
|
||||
assert.Equal(t, field.TypeTime, f.Type())
|
||||
assert.Equal(t, "time.Time", f.Type().String())
|
||||
assert.NotNil(t, f.Value())
|
||||
assert.Equal(t, now, f.Value().(func() time.Time)())
|
||||
}
|
||||
|
||||
func TestField_Tag(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user