diff --git a/.golangci.yml b/.golangci.yml index e2899b2a2..c033ab45a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,7 +7,7 @@ linters-settings: dupl: threshold: 100 funlen: - lines: 120 + lines: 130 statements: 80 goheader: template: |- diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index e8db88a6c..02f897c5b 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -8,7 +8,9 @@ package fieldtype import ( "fmt" + "net" + "github.com/facebook/ent/dialect/sql" "github.com/facebook/ent/entc/integration/ent/role" ) @@ -193,8 +195,14 @@ var ( ValidateOptionalInt32Validator func(int32) error // NdirValidator is a validator for the "ndir" field. It is called by the builders before save. NdirValidator func(string) error + // DefaultStr holds the default value on creation for the str field. + DefaultStr func() sql.NullString + // DefaultNullStr holds the default value on creation for the null_str field. + DefaultNullStr func() sql.NullString // LinkValidator is a validator for the "link" field. It is called by the builders before save. LinkValidator func(string) error + // DefaultIP holds the default value on creation for the ip field. + DefaultIP func() net.IP // MACValidator is a validator for the "mac" field. It is called by the builders before save. MACValidator func(string) error ) diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index 461010911..763c98f82 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -612,6 +612,18 @@ func (ftc *FieldTypeCreate) SaveX(ctx context.Context) *FieldType { // defaults sets the default values of the builder before save. func (ftc *FieldTypeCreate) defaults() { + if _, ok := ftc.mutation.Str(); !ok { + v := fieldtype.DefaultStr() + ftc.mutation.SetStr(v) + } + if _, ok := ftc.mutation.NullStr(); !ok { + v := fieldtype.DefaultNullStr() + ftc.mutation.SetNullStr(v) + } + if _, ok := ftc.mutation.IP(); !ok { + v := fieldtype.DefaultIP() + ftc.mutation.SetIP(v) + } if _, ok := ftc.mutation.Role(); !ok { v := fieldtype.DefaultRole ftc.mutation.SetRole(v) diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index 0e609be36..1ae9f7bf4 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -7,8 +7,10 @@ package ent import ( + "net" "time" + "github.com/facebook/ent/dialect/sql" "github.com/facebook/ent/entc/integration/ent/card" "github.com/facebook/ent/entc/integration/ent/fieldtype" "github.com/facebook/ent/entc/integration/ent/file" @@ -56,10 +58,22 @@ func init() { fieldtypeDescNdir := fieldtypeFields[27].Descriptor() // fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save. fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error) + // fieldtypeDescStr is the schema descriptor for str field. + fieldtypeDescStr := fieldtypeFields[28].Descriptor() + // fieldtype.DefaultStr holds the default value on creation for the str field. + fieldtype.DefaultStr = fieldtypeDescStr.Default.(func() sql.NullString) + // fieldtypeDescNullStr is the schema descriptor for null_str field. + fieldtypeDescNullStr := fieldtypeFields[29].Descriptor() + // fieldtype.DefaultNullStr holds the default value on creation for the null_str field. + fieldtype.DefaultNullStr = fieldtypeDescNullStr.Default.(func() sql.NullString) // fieldtypeDescLink is the schema descriptor for link field. fieldtypeDescLink := fieldtypeFields[30].Descriptor() // fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save. fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error) + // fieldtypeDescIP is the schema descriptor for ip field. + fieldtypeDescIP := fieldtypeFields[36].Descriptor() + // fieldtype.DefaultIP holds the default value on creation for the ip field. + fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtypeDescMAC is the schema descriptor for mac field. fieldtypeDescMAC := fieldtypeFields[45].Descriptor() // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. diff --git a/entc/integration/ent/schema/fieldtype.go b/entc/integration/ent/schema/fieldtype.go index 82b188455..34e4dddec 100644 --- a/entc/integration/ent/schema/fieldtype.go +++ b/entc/integration/ent/schema/fieldtype.go @@ -79,11 +79,17 @@ func (FieldType) Fields() []ent.Field { GoType(http.Dir("ndir")), field.String("str"). Optional(). - GoType(&sql.NullString{}), + GoType(&sql.NullString{}). + DefaultFunc(func() sql.NullString { + return sql.NullString{String: "default", Valid: true} + }), field.String("null_str"). Optional(). Nillable(). - GoType(&sql.NullString{}), + GoType(&sql.NullString{}). + DefaultFunc(func() sql.NullString { + return sql.NullString{String: "default", Valid: true} + }), field.String("link"). Optional(). NotEmpty(). @@ -107,7 +113,10 @@ func (FieldType) Fields() []ent.Field { GoType(&sql.NullTime{}), field.Bytes("ip"). Optional(). - GoType(net.IP("127.0.0.1")), + GoType(net.IP("127.0.0.1")). + DefaultFunc(func() net.IP { + return net.IP("127.0.0.1") + }), field.Int("null_int64"). Optional(). GoType(&sql.NullInt64{}), diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index 0fd94f4f2..82b43acfd 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -7,7 +7,9 @@ package fieldtype import ( + "database/sql" "fmt" + "net" "github.com/facebook/ent/entc/integration/ent/role" ) @@ -118,8 +120,14 @@ var ( ValidateOptionalInt32Validator func(int32) error // NdirValidator is a validator for the "ndir" field. It is called by the builders before save. NdirValidator func(string) error + // DefaultStr holds the default value on creation for the str field. + DefaultStr func() sql.NullString + // DefaultNullStr holds the default value on creation for the null_str field. + DefaultNullStr func() sql.NullString // LinkValidator is a validator for the "link" field. It is called by the builders before save. LinkValidator func(string) error + // DefaultIP holds the default value on creation for the ip field. + DefaultIP func() net.IP // MACValidator is a validator for the "mac" field. It is called by the builders before save. MACValidator func(string) error ) diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index 4ba4722cd..91f96a03b 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -613,6 +613,18 @@ func (ftc *FieldTypeCreate) SaveX(ctx context.Context) *FieldType { // defaults sets the default values of the builder before save. func (ftc *FieldTypeCreate) defaults() { + if _, ok := ftc.mutation.Str(); !ok { + v := fieldtype.DefaultStr() + ftc.mutation.SetStr(v) + } + if _, ok := ftc.mutation.NullStr(); !ok { + v := fieldtype.DefaultNullStr() + ftc.mutation.SetNullStr(v) + } + if _, ok := ftc.mutation.IP(); !ok { + v := fieldtype.DefaultIP() + ftc.mutation.SetIP(v) + } if _, ok := ftc.mutation.Role(); !ok { v := fieldtype.DefaultRole ftc.mutation.SetRole(v) diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 3c7103f8f..450ace506 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -7,6 +7,8 @@ package ent import ( + "database/sql" + "net" "time" "github.com/facebook/ent/entc/integration/ent/schema" @@ -56,10 +58,22 @@ func init() { fieldtypeDescNdir := fieldtypeFields[27].Descriptor() // fieldtype.NdirValidator is a validator for the "ndir" field. It is called by the builders before save. fieldtype.NdirValidator = fieldtypeDescNdir.Validators[0].(func(string) error) + // fieldtypeDescStr is the schema descriptor for str field. + fieldtypeDescStr := fieldtypeFields[28].Descriptor() + // fieldtype.DefaultStr holds the default value on creation for the str field. + fieldtype.DefaultStr = fieldtypeDescStr.Default.(func() sql.NullString) + // fieldtypeDescNullStr is the schema descriptor for null_str field. + fieldtypeDescNullStr := fieldtypeFields[29].Descriptor() + // fieldtype.DefaultNullStr holds the default value on creation for the null_str field. + fieldtype.DefaultNullStr = fieldtypeDescNullStr.Default.(func() sql.NullString) // fieldtypeDescLink is the schema descriptor for link field. fieldtypeDescLink := fieldtypeFields[30].Descriptor() // fieldtype.LinkValidator is a validator for the "link" field. It is called by the builders before save. fieldtype.LinkValidator = fieldtypeDescLink.Validators[0].(func(string) error) + // fieldtypeDescIP is the schema descriptor for ip field. + fieldtypeDescIP := fieldtypeFields[36].Descriptor() + // fieldtype.DefaultIP holds the default value on creation for the ip field. + fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtypeDescMAC is the schema descriptor for mac field. fieldtypeDescMAC := fieldtypeFields[45].Descriptor() // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. diff --git a/entc/integration/type_test.go b/entc/integration/type_test.go index 035fbc68f..d81c141c2 100644 --- a/entc/integration/type_test.go +++ b/entc/integration/type_test.go @@ -61,8 +61,7 @@ func Types(t *testing.T, client *ent.Client) { SetNillableInt64(math.MinInt64). SetDir("dir"). SetNdir("ndir"). - SetStr(sql.NullString{String: "str", Valid: true}). - SetNullStr(sql.NullString{String: "str", Valid: true}). + SetNullStr(sql.NullString{String: "not-default", Valid: true}). SetLink(schema.Link{URL: link}). SetNullLink(schema.Link{URL: link}). SetRole(role.Admin). @@ -79,10 +78,11 @@ func Types(t *testing.T, client *ent.Client) { require.Equal(http.Dir("dir"), ft.Dir) require.NotNil(*ft.Ndir) require.Equal(http.Dir("ndir"), *ft.Ndir) - require.Equal("str", ft.Str.String) - require.Equal("str", ft.NullStr.String) + require.Equal("default", ft.Str.String) + require.Equal("not-default", ft.NullStr.String) require.Equal("localhost", ft.Link.String()) require.Equal("localhost", ft.NullLink.String()) + require.Equal(net.IP("127.0.0.1").String(), ft.IP.String()) mac, err := net.ParseMAC("3b:b3:6b:3c:10:79") require.NoError(err) dt, err := time.Parse(time.RFC3339, "1906-01-02T00:00:00+00:00") diff --git a/schema/field/field_test.go b/schema/field/field_test.go index 8ac149328..cb2f6fcba 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -185,9 +185,7 @@ func TestBytes(t *testing.T) { fd = field.Bytes("uuid"). GoType(&uuid.UUID{}). - DefaultFunc(func() uuid.UUID { - return uuid.New() - }). + DefaultFunc(uuid.New). Descriptor() assert.NoError(t, fd.Err) assert.Equal(t, "uuid.UUID", fd.Info.Ident)