diff --git a/entc/gen/template/runtime.tmpl b/entc/gen/template/runtime.tmpl index b8ef49867..743a019b8 100644 --- a/entc/gen/template/runtime.tmpl +++ b/entc/gen/template/runtime.tmpl @@ -155,7 +155,11 @@ func init() { // {{ $default }} holds the default value on creation for the {{ $f.Name }} field. {{- $defaultType := print $f.Type.Type }}{{ if $f.DefaultFunc }}{{ $defaultType = print "func() " $f.Type }}{{ end }} {{- if and $f.HasGoType (not (hasPrefix $defaultType "func")) }} - {{ $default }} = {{ $f.Type }}({{ $desc }}.Default.({{ $defaultType }})) + {{- if or $f.IsJSON $f.IsOther }} + {{ $default }} = {{ $desc }}.Default.({{ $f.Type }}) + {{- else }} + {{ $default }} = {{ $f.Type }}({{ $desc }}.Default.({{ $defaultType }})) + {{- end }} {{- else }} {{ $default }} = {{ $desc }}.Default.({{ $defaultType }}) {{- end }} diff --git a/entc/gen/type.go b/entc/gen/type.go index 97ca9e1d5..1694dff3d 100644 --- a/entc/gen/type.go +++ b/entc/gen/type.go @@ -1005,6 +1005,9 @@ func (f Field) IsTime() bool { return f.Type != nil && f.Type.Type == field.Type // IsJSON returns true if the field is a JSON field. func (f Field) IsJSON() bool { return f.Type != nil && f.Type.Type == field.TypeJSON } +// IsOther returns true if the field is an Other field. +func (f Field) IsOther() bool { return f.Type != nil && f.Type.Type == field.TypeOther } + // IsString returns true if the field is a string field. func (f Field) IsString() bool { return f.Type != nil && f.Type.Type == field.TypeString } diff --git a/entc/integration/ent/entql.go b/entc/integration/ent/entql.go index a0fae5fa1..921741d9a 100644 --- a/entc/integration/ent/entql.go +++ b/entc/integration/ent/entql.go @@ -104,6 +104,7 @@ var schemaGraph = func() *sqlgraph.Schema { fieldtype.FieldDatetime: {Type: field.TypeTime, Column: fieldtype.FieldDatetime}, fieldtype.FieldDecimal: {Type: field.TypeFloat64, Column: fieldtype.FieldDecimal}, fieldtype.FieldLinkOther: {Type: field.TypeOther, Column: fieldtype.FieldLinkOther}, + fieldtype.FieldLinkOtherFunc: {Type: field.TypeOther, Column: fieldtype.FieldLinkOtherFunc}, fieldtype.FieldMAC: {Type: field.TypeString, Column: fieldtype.FieldMAC}, fieldtype.FieldStringArray: {Type: field.TypeOther, Column: fieldtype.FieldStringArray}, fieldtype.FieldPassword: {Type: field.TypeString, Column: fieldtype.FieldPassword}, @@ -969,6 +970,11 @@ func (f *FieldTypeFilter) WhereLinkOther(p entql.OtherP) { f.Where(p.Field(fieldtype.FieldLinkOther)) } +// WhereLinkOtherFunc applies the entql other predicate on the link_other_func field. +func (f *FieldTypeFilter) WhereLinkOtherFunc(p entql.OtherP) { + f.Where(p.Field(fieldtype.FieldLinkOtherFunc)) +} + // WhereMAC applies the entql string predicate on the mac field. func (f *FieldTypeFilter) WhereMAC(p entql.StringP) { f.Where(p.Field(fieldtype.FieldMAC)) diff --git a/entc/integration/ent/fieldtype.go b/entc/integration/ent/fieldtype.go index 52c9c965d..5acc6f02d 100644 --- a/entc/integration/ent/fieldtype.go +++ b/entc/integration/ent/fieldtype.go @@ -80,6 +80,8 @@ type FieldType struct { Decimal float64 `json:"decimal,omitempty"` // LinkOther holds the value of the "link_other" field. LinkOther *schema.Link `json:"link_other,omitempty"` + // LinkOtherFunc holds the value of the "link_other_func" field. + LinkOtherFunc *schema.Link `json:"link_other_func,omitempty"` // MAC holds the value of the "mac" field. MAC schema.MAC `json:"mac,omitempty"` // StringArray holds the value of the "string_array" field. @@ -174,7 +176,7 @@ func (*FieldType) scanValues(columns []string) ([]interface{}, error) { values[i] = new(role.Priority) case fieldtype.FieldBigInt: values[i] = new(schema.BigInt) - case fieldtype.FieldLinkOther, fieldtype.FieldLink: + case fieldtype.FieldLinkOther, fieldtype.FieldLinkOtherFunc, fieldtype.FieldLink: values[i] = new(schema.Link) case fieldtype.FieldMAC: values[i] = new(schema.MAC) @@ -390,6 +392,12 @@ func (ft *FieldType) assignValues(columns []string, values []interface{}) error } else if value != nil { ft.LinkOther = value } + case fieldtype.FieldLinkOtherFunc: + if value, ok := values[i].(*schema.Link); !ok { + return fmt.Errorf("unexpected type %T for field link_other_func", values[i]) + } else if value != nil { + ft.LinkOtherFunc = value + } case fieldtype.FieldMAC: if value, ok := values[i].(*schema.MAC); !ok { return fmt.Errorf("unexpected type %T for field mac", values[i]) @@ -711,6 +719,8 @@ func (ft *FieldType) String() string { builder.WriteString(fmt.Sprintf("%v", ft.Decimal)) builder.WriteString(", link_other=") builder.WriteString(fmt.Sprintf("%v", ft.LinkOther)) + builder.WriteString(", link_other_func=") + builder.WriteString(fmt.Sprintf("%v", ft.LinkOtherFunc)) builder.WriteString(", mac=") builder.WriteString(fmt.Sprintf("%v", ft.MAC)) builder.WriteString(", string_array=") diff --git a/entc/integration/ent/fieldtype/fieldtype.go b/entc/integration/ent/fieldtype/fieldtype.go index 3d926b222..a54ae0773 100644 --- a/entc/integration/ent/fieldtype/fieldtype.go +++ b/entc/integration/ent/fieldtype/fieldtype.go @@ -76,6 +76,8 @@ const ( FieldDecimal = "decimal" // FieldLinkOther holds the string denoting the link_other field in the database. FieldLinkOther = "link_other" + // FieldLinkOtherFunc holds the string denoting the link_other_func field in the database. + FieldLinkOtherFunc = "link_other_func" // FieldMAC holds the string denoting the mac field in the database. FieldMAC = "mac" // FieldStringArray holds the string denoting the string_array field in the database. @@ -182,6 +184,7 @@ var Columns = []string{ FieldDatetime, FieldDecimal, FieldLinkOther, + FieldLinkOtherFunc, FieldMAC, FieldStringArray, FieldPassword, @@ -246,6 +249,10 @@ var ( UpdateDefaultInt64 func() int64 // ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. ValidateOptionalInt32Validator func(int32) error + // DefaultLinkOther holds the default value on creation for the "link_other" field. + DefaultLinkOther *schema.Link + // DefaultLinkOtherFunc holds the default value on creation for the "link_other_func" field. + DefaultLinkOtherFunc func() *schema.Link // MACValidator is a validator for the "mac" field. It is called by the builders before save. MACValidator func(string) error // UpdateDefaultDuration holds the default value on update for the "duration" field. diff --git a/entc/integration/ent/fieldtype/where.go b/entc/integration/ent/fieldtype/where.go index c5653c018..9fb6ab1f5 100644 --- a/entc/integration/ent/fieldtype/where.go +++ b/entc/integration/ent/fieldtype/where.go @@ -283,6 +283,13 @@ func LinkOther(v *schema.Link) predicate.FieldType { }) } +// LinkOtherFunc applies equality check predicate on the "link_other_func" field. It's identical to LinkOtherFuncEQ. +func LinkOtherFunc(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldLinkOtherFunc), v)) + }) +} + // MAC applies equality check predicate on the "mac" field. It's identical to MACEQ. func MAC(v schema.MAC) predicate.FieldType { return predicate.FieldType(func(s *sql.Selector) { @@ -2857,6 +2864,96 @@ func LinkOtherNotNil() predicate.FieldType { }) } +// LinkOtherFuncEQ applies the EQ predicate on the "link_other_func" field. +func LinkOtherFuncEQ(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.EQ(s.C(FieldLinkOtherFunc), v)) + }) +} + +// LinkOtherFuncNEQ applies the NEQ predicate on the "link_other_func" field. +func LinkOtherFuncNEQ(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NEQ(s.C(FieldLinkOtherFunc), v)) + }) +} + +// LinkOtherFuncIn applies the In predicate on the "link_other_func" field. +func LinkOtherFuncIn(vs ...*schema.Link) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(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(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.In(s.C(FieldLinkOtherFunc), v...)) + }) +} + +// LinkOtherFuncNotIn applies the NotIn predicate on the "link_other_func" field. +func LinkOtherFuncNotIn(vs ...*schema.Link) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(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(v) == 0 { + s.Where(sql.False()) + return + } + s.Where(sql.NotIn(s.C(FieldLinkOtherFunc), v...)) + }) +} + +// LinkOtherFuncGT applies the GT predicate on the "link_other_func" field. +func LinkOtherFuncGT(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GT(s.C(FieldLinkOtherFunc), v)) + }) +} + +// LinkOtherFuncGTE applies the GTE predicate on the "link_other_func" field. +func LinkOtherFuncGTE(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.GTE(s.C(FieldLinkOtherFunc), v)) + }) +} + +// LinkOtherFuncLT applies the LT predicate on the "link_other_func" field. +func LinkOtherFuncLT(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LT(s.C(FieldLinkOtherFunc), v)) + }) +} + +// LinkOtherFuncLTE applies the LTE predicate on the "link_other_func" field. +func LinkOtherFuncLTE(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.LTE(s.C(FieldLinkOtherFunc), v)) + }) +} + +// LinkOtherFuncIsNil applies the IsNil predicate on the "link_other_func" field. +func LinkOtherFuncIsNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.IsNull(s.C(FieldLinkOtherFunc))) + }) +} + +// LinkOtherFuncNotNil applies the NotNil predicate on the "link_other_func" field. +func LinkOtherFuncNotNil() predicate.FieldType { + return predicate.FieldType(func(s *sql.Selector) { + s.Where(sql.NotNull(s.C(FieldLinkOtherFunc))) + }) +} + // MACEQ applies the EQ predicate on the "mac" field. func MACEQ(v schema.MAC) predicate.FieldType { return predicate.FieldType(func(s *sql.Selector) { diff --git a/entc/integration/ent/fieldtype_create.go b/entc/integration/ent/fieldtype_create.go index 02f271c34..c76605805 100644 --- a/entc/integration/ent/fieldtype_create.go +++ b/entc/integration/ent/fieldtype_create.go @@ -361,6 +361,12 @@ func (ftc *FieldTypeCreate) SetLinkOther(s *schema.Link) *FieldTypeCreate { return ftc } +// SetLinkOtherFunc sets the "link_other_func" field. +func (ftc *FieldTypeCreate) SetLinkOtherFunc(s *schema.Link) *FieldTypeCreate { + ftc.mutation.SetLinkOtherFunc(s) + return ftc +} + // SetMAC sets the "mac" field. func (ftc *FieldTypeCreate) SetMAC(s schema.MAC) *FieldTypeCreate { ftc.mutation.SetMAC(s) @@ -824,6 +830,14 @@ func (ftc *FieldTypeCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (ftc *FieldTypeCreate) defaults() { + if _, ok := ftc.mutation.LinkOther(); !ok { + v := fieldtype.DefaultLinkOther + ftc.mutation.SetLinkOther(v) + } + if _, ok := ftc.mutation.LinkOtherFunc(); !ok { + v := fieldtype.DefaultLinkOtherFunc() + ftc.mutation.SetLinkOtherFunc(v) + } if _, ok := ftc.mutation.Dir(); !ok { v := fieldtype.DefaultDir() ftc.mutation.SetDir(v) @@ -1179,6 +1193,14 @@ func (ftc *FieldTypeCreate) createSpec() (*FieldType, *sqlgraph.CreateSpec) { }) _node.LinkOther = value } + if value, ok := ftc.mutation.LinkOtherFunc(); ok { + _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ + Type: field.TypeOther, + Value: value, + Column: fieldtype.FieldLinkOtherFunc, + }) + _node.LinkOtherFunc = value + } if value, ok := ftc.mutation.MAC(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, @@ -2121,6 +2143,24 @@ func (u *FieldTypeUpsert) ClearLinkOther() *FieldTypeUpsert { return u } +// SetLinkOtherFunc sets the "link_other_func" field. +func (u *FieldTypeUpsert) SetLinkOtherFunc(v *schema.Link) *FieldTypeUpsert { + u.Set(fieldtype.FieldLinkOtherFunc, v) + return u +} + +// UpdateLinkOtherFunc sets the "link_other_func" field to the value that was provided on create. +func (u *FieldTypeUpsert) UpdateLinkOtherFunc() *FieldTypeUpsert { + u.SetExcluded(fieldtype.FieldLinkOtherFunc) + return u +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (u *FieldTypeUpsert) ClearLinkOtherFunc() *FieldTypeUpsert { + u.SetNull(fieldtype.FieldLinkOtherFunc) + return u +} + // SetMAC sets the "mac" field. func (u *FieldTypeUpsert) SetMAC(v schema.MAC) *FieldTypeUpsert { u.Set(fieldtype.FieldMAC, v) @@ -3523,6 +3563,27 @@ func (u *FieldTypeUpsertOne) ClearLinkOther() *FieldTypeUpsertOne { }) } +// SetLinkOtherFunc sets the "link_other_func" field. +func (u *FieldTypeUpsertOne) SetLinkOtherFunc(v *schema.Link) *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.SetLinkOtherFunc(v) + }) +} + +// UpdateLinkOtherFunc sets the "link_other_func" field to the value that was provided on create. +func (u *FieldTypeUpsertOne) UpdateLinkOtherFunc() *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.UpdateLinkOtherFunc() + }) +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (u *FieldTypeUpsertOne) ClearLinkOtherFunc() *FieldTypeUpsertOne { + return u.Update(func(s *FieldTypeUpsert) { + s.ClearLinkOtherFunc() + }) +} + // SetMAC sets the "mac" field. func (u *FieldTypeUpsertOne) SetMAC(v schema.MAC) *FieldTypeUpsertOne { return u.Update(func(s *FieldTypeUpsert) { @@ -5197,6 +5258,27 @@ func (u *FieldTypeUpsertBulk) ClearLinkOther() *FieldTypeUpsertBulk { }) } +// SetLinkOtherFunc sets the "link_other_func" field. +func (u *FieldTypeUpsertBulk) SetLinkOtherFunc(v *schema.Link) *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.SetLinkOtherFunc(v) + }) +} + +// UpdateLinkOtherFunc sets the "link_other_func" field to the value that was provided on create. +func (u *FieldTypeUpsertBulk) UpdateLinkOtherFunc() *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.UpdateLinkOtherFunc() + }) +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (u *FieldTypeUpsertBulk) ClearLinkOtherFunc() *FieldTypeUpsertBulk { + return u.Update(func(s *FieldTypeUpsert) { + s.ClearLinkOtherFunc() + }) +} + // SetMAC sets the "mac" field. func (u *FieldTypeUpsertBulk) SetMAC(v schema.MAC) *FieldTypeUpsertBulk { return u.Update(func(s *FieldTypeUpsert) { diff --git a/entc/integration/ent/fieldtype_update.go b/entc/integration/ent/fieldtype_update.go index 87ea07e2b..62ceb292c 100644 --- a/entc/integration/ent/fieldtype_update.go +++ b/entc/integration/ent/fieldtype_update.go @@ -667,6 +667,18 @@ func (ftu *FieldTypeUpdate) ClearLinkOther() *FieldTypeUpdate { return ftu } +// SetLinkOtherFunc sets the "link_other_func" field. +func (ftu *FieldTypeUpdate) SetLinkOtherFunc(s *schema.Link) *FieldTypeUpdate { + ftu.mutation.SetLinkOtherFunc(s) + return ftu +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (ftu *FieldTypeUpdate) ClearLinkOtherFunc() *FieldTypeUpdate { + ftu.mutation.ClearLinkOtherFunc() + return ftu +} + // SetMAC sets the "mac" field. func (ftu *FieldTypeUpdate) SetMAC(s schema.MAC) *FieldTypeUpdate { ftu.mutation.SetMAC(s) @@ -1921,6 +1933,19 @@ func (ftu *FieldTypeUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: fieldtype.FieldLinkOther, }) } + if value, ok := ftu.mutation.LinkOtherFunc(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeOther, + Value: value, + Column: fieldtype.FieldLinkOtherFunc, + }) + } + if ftu.mutation.LinkOtherFuncCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeOther, + Column: fieldtype.FieldLinkOtherFunc, + }) + } if value, ok := ftu.mutation.MAC(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeString, @@ -3057,6 +3082,18 @@ func (ftuo *FieldTypeUpdateOne) ClearLinkOther() *FieldTypeUpdateOne { return ftuo } +// SetLinkOtherFunc sets the "link_other_func" field. +func (ftuo *FieldTypeUpdateOne) SetLinkOtherFunc(s *schema.Link) *FieldTypeUpdateOne { + ftuo.mutation.SetLinkOtherFunc(s) + return ftuo +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (ftuo *FieldTypeUpdateOne) ClearLinkOtherFunc() *FieldTypeUpdateOne { + ftuo.mutation.ClearLinkOtherFunc() + return ftuo +} + // SetMAC sets the "mac" field. func (ftuo *FieldTypeUpdateOne) SetMAC(s schema.MAC) *FieldTypeUpdateOne { ftuo.mutation.SetMAC(s) @@ -4335,6 +4372,19 @@ func (ftuo *FieldTypeUpdateOne) sqlSave(ctx context.Context) (_node *FieldType, Column: fieldtype.FieldLinkOther, }) } + if value, ok := ftuo.mutation.LinkOtherFunc(); ok { + _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ + Type: field.TypeOther, + Value: value, + Column: fieldtype.FieldLinkOtherFunc, + }) + } + if ftuo.mutation.LinkOtherFuncCleared() { + _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ + Type: field.TypeOther, + Column: fieldtype.FieldLinkOtherFunc, + }) + } if value, ok := ftuo.mutation.MAC(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeString, diff --git a/entc/integration/ent/migrate/schema.go b/entc/integration/ent/migrate/schema.go index 863b0fc18..bb0701899 100644 --- a/entc/integration/ent/migrate/schema.go +++ b/entc/integration/ent/migrate/schema.go @@ -97,6 +97,7 @@ var ( {Name: "datetime", Type: field.TypeTime, Nullable: true, SchemaType: map[string]string{"mysql": "datetime", "postgres": "date"}}, {Name: "decimal", Type: field.TypeFloat64, Nullable: true, SchemaType: map[string]string{"mysql": "decimal(6,2)", "postgres": "numeric"}}, {Name: "link_other", Type: field.TypeOther, Nullable: true, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "varchar", "sqlite3": "varchar(255)"}}, + {Name: "link_other_func", Type: field.TypeOther, Nullable: true, SchemaType: map[string]string{"mysql": "varchar(255)", "postgres": "varchar", "sqlite3": "varchar(255)"}}, {Name: "mac", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "macaddr"}}, {Name: "string_array", Type: field.TypeOther, Nullable: true, SchemaType: map[string]string{"mysql": "blob", "postgres": "text[]", "sqlite3": "json"}}, {Name: "password", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"mysql": "char(32)"}}, @@ -143,7 +144,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "field_types_files_field", - Columns: []*schema.Column{FieldTypesColumns[64]}, + Columns: []*schema.Column{FieldTypesColumns[65]}, RefColumns: []*schema.Column{FilesColumns[0]}, OnDelete: schema.SetNull, }, diff --git a/entc/integration/ent/mutation.go b/entc/integration/ent/mutation.go index 767aad6da..fbf5adac5 100644 --- a/entc/integration/ent/mutation.go +++ b/entc/integration/ent/mutation.go @@ -1361,6 +1361,7 @@ type FieldTypeMutation struct { decimal *float64 adddecimal *float64 link_other **schema.Link + link_other_func **schema.Link mac *schema.MAC string_array *schema.Strings password *string @@ -3246,6 +3247,55 @@ func (m *FieldTypeMutation) ResetLinkOther() { delete(m.clearedFields, fieldtype.FieldLinkOther) } +// SetLinkOtherFunc sets the "link_other_func" field. +func (m *FieldTypeMutation) SetLinkOtherFunc(s *schema.Link) { + m.link_other_func = &s +} + +// LinkOtherFunc returns the value of the "link_other_func" field in the mutation. +func (m *FieldTypeMutation) LinkOtherFunc() (r *schema.Link, exists bool) { + v := m.link_other_func + if v == nil { + return + } + return *v, true +} + +// OldLinkOtherFunc returns the old "link_other_func" field's value of the FieldType entity. +// If the FieldType object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *FieldTypeMutation) OldLinkOtherFunc(ctx context.Context) (v *schema.Link, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldLinkOtherFunc is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldLinkOtherFunc requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldLinkOtherFunc: %w", err) + } + return oldValue.LinkOtherFunc, nil +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (m *FieldTypeMutation) ClearLinkOtherFunc() { + m.link_other_func = nil + m.clearedFields[fieldtype.FieldLinkOtherFunc] = struct{}{} +} + +// LinkOtherFuncCleared returns if the "link_other_func" field was cleared in this mutation. +func (m *FieldTypeMutation) LinkOtherFuncCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldLinkOtherFunc] + return ok +} + +// ResetLinkOtherFunc resets all changes to the "link_other_func" field. +func (m *FieldTypeMutation) ResetLinkOtherFunc() { + m.link_other_func = nil + delete(m.clearedFields, fieldtype.FieldLinkOtherFunc) +} + // SetMAC sets the "mac" field. func (m *FieldTypeMutation) SetMAC(s schema.MAC) { m.mac = &s @@ -5111,7 +5161,7 @@ func (m *FieldTypeMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 63) + fields := make([]string, 0, 64) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -5193,6 +5243,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.link_other != nil { fields = append(fields, fieldtype.FieldLinkOther) } + if m.link_other_func != nil { + fields = append(fields, fieldtype.FieldLinkOtherFunc) + } if m.mac != nil { fields = append(fields, fieldtype.FieldMAC) } @@ -5363,6 +5416,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.Decimal() case fieldtype.FieldLinkOther: return m.LinkOther() + case fieldtype.FieldLinkOtherFunc: + return m.LinkOtherFunc() case fieldtype.FieldMAC: return m.MAC() case fieldtype.FieldStringArray: @@ -5498,6 +5553,8 @@ func (m *FieldTypeMutation) OldField(ctx context.Context, name string) (ent.Valu return m.OldDecimal(ctx) case fieldtype.FieldLinkOther: return m.OldLinkOther(ctx) + case fieldtype.FieldLinkOtherFunc: + return m.OldLinkOtherFunc(ctx) case fieldtype.FieldMAC: return m.OldMAC(ctx) case fieldtype.FieldStringArray: @@ -5768,6 +5825,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetLinkOther(v) return nil + case fieldtype.FieldLinkOtherFunc: + v, ok := value.(*schema.Link) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetLinkOtherFunc(v) + return nil case fieldtype.FieldMAC: v, ok := value.(schema.MAC) if !ok { @@ -6491,6 +6555,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldLinkOther) { fields = append(fields, fieldtype.FieldLinkOther) } + if m.FieldCleared(fieldtype.FieldLinkOtherFunc) { + fields = append(fields, fieldtype.FieldLinkOtherFunc) + } if m.FieldCleared(fieldtype.FieldMAC) { fields = append(fields, fieldtype.FieldMAC) } @@ -6664,6 +6731,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldLinkOther: m.ClearLinkOther() return nil + case fieldtype.FieldLinkOtherFunc: + m.ClearLinkOtherFunc() + return nil case fieldtype.FieldMAC: m.ClearMAC() return nil @@ -6846,6 +6916,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldLinkOther: m.ResetLinkOther() return nil + case fieldtype.FieldLinkOtherFunc: + m.ResetLinkOtherFunc() + return nil case fieldtype.FieldMAC: m.ResetMAC() return nil diff --git a/entc/integration/ent/runtime.go b/entc/integration/ent/runtime.go index e1901b814..f7a8625ed 100644 --- a/entc/integration/ent/runtime.go +++ b/entc/integration/ent/runtime.go @@ -65,36 +65,44 @@ func init() { fieldtypeDescValidateOptionalInt32 := fieldtypeFields[15].Descriptor() // fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error) + // fieldtypeDescLinkOther is the schema descriptor for link_other field. + fieldtypeDescLinkOther := fieldtypeFields[26].Descriptor() + // fieldtype.DefaultLinkOther holds the default value on creation for the link_other field. + fieldtype.DefaultLinkOther = fieldtypeDescLinkOther.Default.(*schema.Link) + // fieldtypeDescLinkOtherFunc is the schema descriptor for link_other_func field. + fieldtypeDescLinkOtherFunc := fieldtypeFields[27].Descriptor() + // fieldtype.DefaultLinkOtherFunc holds the default value on creation for the link_other_func field. + fieldtype.DefaultLinkOtherFunc = fieldtypeDescLinkOtherFunc.Default.(func() *schema.Link) // fieldtypeDescMAC is the schema descriptor for mac field. - fieldtypeDescMAC := fieldtypeFields[27].Descriptor() + fieldtypeDescMAC := fieldtypeFields[28].Descriptor() // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. fieldtype.MACValidator = fieldtypeDescMAC.Validators[0].(func(string) error) // fieldtypeDescDuration is the schema descriptor for duration field. - fieldtypeDescDuration := fieldtypeFields[31].Descriptor() + fieldtypeDescDuration := fieldtypeFields[32].Descriptor() // fieldtype.UpdateDefaultDuration holds the default value on update for the duration field. fieldtype.UpdateDefaultDuration = fieldtypeDescDuration.UpdateDefault.(func() time.Duration) // fieldtypeDescDir is the schema descriptor for dir field. - fieldtypeDescDir := fieldtypeFields[32].Descriptor() + fieldtypeDescDir := fieldtypeFields[33].Descriptor() // fieldtype.DefaultDir holds the default value on creation for the dir field. fieldtype.DefaultDir = fieldtypeDescDir.Default.(func() http.Dir) // fieldtypeDescNdir is the schema descriptor for ndir field. - fieldtypeDescNdir := fieldtypeFields[33].Descriptor() + fieldtypeDescNdir := fieldtypeFields[34].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[34].Descriptor() + fieldtypeDescStr := fieldtypeFields[35].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[35].Descriptor() + fieldtypeDescNullStr := fieldtypeFields[36].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[36].Descriptor() + fieldtypeDescLink := fieldtypeFields[37].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) // fieldtypeDescRawData is the schema descriptor for raw_data field. - fieldtypeDescRawData := fieldtypeFields[42].Descriptor() + fieldtypeDescRawData := fieldtypeFields[43].Descriptor() // fieldtype.RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. fieldtype.RawDataValidator = func() func([]byte) error { validators := fieldtypeDescRawData.Validators @@ -112,21 +120,21 @@ func init() { } }() // fieldtypeDescIP is the schema descriptor for ip field. - fieldtypeDescIP := fieldtypeFields[44].Descriptor() + fieldtypeDescIP := fieldtypeFields[45].Descriptor() // fieldtype.DefaultIP holds the default value on creation for the ip field. fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtype.IPValidator is a validator for the "ip" field. It is called by the builders before save. fieldtype.IPValidator = fieldtypeDescIP.Validators[0].(func([]byte) error) // fieldtypeDescPair is the schema descriptor for pair field. - fieldtypeDescPair := fieldtypeFields[57].Descriptor() + fieldtypeDescPair := fieldtypeFields[58].Descriptor() // fieldtype.DefaultPair holds the default value on creation for the pair field. fieldtype.DefaultPair = fieldtypeDescPair.Default.(func() schema.Pair) // fieldtypeDescVstring is the schema descriptor for vstring field. - fieldtypeDescVstring := fieldtypeFields[59].Descriptor() + fieldtypeDescVstring := fieldtypeFields[60].Descriptor() // fieldtype.DefaultVstring holds the default value on creation for the vstring field. fieldtype.DefaultVstring = fieldtypeDescVstring.Default.(func() schema.VString) // fieldtypeDescTriple is the schema descriptor for triple field. - fieldtypeDescTriple := fieldtypeFields[60].Descriptor() + fieldtypeDescTriple := fieldtypeFields[61].Descriptor() // fieldtype.DefaultTriple holds the default value on creation for the triple field. fieldtype.DefaultTriple = fieldtypeDescTriple.Default.(func() schema.Triple) fileFields := schema.File{}.Fields() diff --git a/entc/integration/ent/schema/fieldtype.go b/entc/integration/ent/schema/fieldtype.go index c196e819c..0a0a45f6b 100644 --- a/entc/integration/ent/schema/fieldtype.go +++ b/entc/integration/ent/schema/fieldtype.go @@ -112,7 +112,16 @@ func (FieldType) Fields() []ent.Field { //nolint:funlen dialect.MySQL: "varchar(255)", dialect.SQLite: "varchar(255)", }). - Optional(), + Optional(). + Default(DefaultLink()), + field.Other("link_other_func", &Link{}). + SchemaType(map[string]string{ + dialect.Postgres: "varchar", + dialect.MySQL: "varchar(255)", + dialect.SQLite: "varchar(255)", + }). + Optional(). + Default(DefaultLink), field.String("mac"). Optional(). GoType(MAC{}). @@ -414,6 +423,11 @@ type Link struct { *url.URL } +func DefaultLink() *Link { + u, _ := url.Parse("127.0.0.1") + return &Link{URL: u} +} + // Scan implements the Scanner interface. func (l *Link) Scan(value interface{}) (err error) { switch v := value.(type) { diff --git a/entc/integration/gremlin/ent/fieldtype.go b/entc/integration/gremlin/ent/fieldtype.go index fbaa1db39..5fe7ce5a0 100644 --- a/entc/integration/gremlin/ent/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype.go @@ -80,6 +80,8 @@ type FieldType struct { Decimal float64 `json:"decimal,omitempty"` // LinkOther holds the value of the "link_other" field. LinkOther *schema.Link `json:"link_other,omitempty"` + // LinkOtherFunc holds the value of the "link_other_func" field. + LinkOtherFunc *schema.Link `json:"link_other_func,omitempty"` // MAC holds the value of the "mac" field. MAC schema.MAC `json:"mac,omitempty"` // StringArray holds the value of the "string_array" field. @@ -189,6 +191,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { Datetime int64 `json:"datetime,omitempty"` Decimal float64 `json:"decimal,omitempty"` LinkOther *schema.Link `json:"link_other,omitempty"` + LinkOtherFunc *schema.Link `json:"link_other_func,omitempty"` MAC schema.MAC `json:"mac,omitempty"` StringArray schema.Strings `json:"string_array,omitempty"` Password string `json:"password,omitempty"` @@ -257,6 +260,7 @@ func (ft *FieldType) FromResponse(res *gremlin.Response) error { ft.Datetime = time.Unix(0, scanft.Datetime) ft.Decimal = scanft.Decimal ft.LinkOther = scanft.LinkOther + ft.LinkOtherFunc = scanft.LinkOtherFunc ft.MAC = scanft.MAC ft.StringArray = scanft.StringArray ft.Password = scanft.Password @@ -383,6 +387,8 @@ func (ft *FieldType) String() string { builder.WriteString(fmt.Sprintf("%v", ft.Decimal)) builder.WriteString(", link_other=") builder.WriteString(fmt.Sprintf("%v", ft.LinkOther)) + builder.WriteString(", link_other_func=") + builder.WriteString(fmt.Sprintf("%v", ft.LinkOtherFunc)) builder.WriteString(", mac=") builder.WriteString(fmt.Sprintf("%v", ft.MAC)) builder.WriteString(", string_array=") @@ -510,6 +516,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { Datetime int64 `json:"datetime,omitempty"` Decimal float64 `json:"decimal,omitempty"` LinkOther *schema.Link `json:"link_other,omitempty"` + LinkOtherFunc *schema.Link `json:"link_other_func,omitempty"` MAC schema.MAC `json:"mac,omitempty"` StringArray schema.Strings `json:"string_array,omitempty"` Password string `json:"password,omitempty"` @@ -580,6 +587,7 @@ func (ft *FieldTypes) FromResponse(res *gremlin.Response) error { Datetime: time.Unix(0, v.Datetime), Decimal: v.Decimal, LinkOther: v.LinkOther, + LinkOtherFunc: v.LinkOtherFunc, MAC: v.MAC, StringArray: v.StringArray, Password: v.Password, diff --git a/entc/integration/gremlin/ent/fieldtype/fieldtype.go b/entc/integration/gremlin/ent/fieldtype/fieldtype.go index 718371dac..8c49a0cd5 100644 --- a/entc/integration/gremlin/ent/fieldtype/fieldtype.go +++ b/entc/integration/gremlin/ent/fieldtype/fieldtype.go @@ -76,6 +76,8 @@ const ( FieldDecimal = "decimal" // FieldLinkOther holds the string denoting the link_other field in the database. FieldLinkOther = "link_other" + // FieldLinkOtherFunc holds the string denoting the link_other_func field in the database. + FieldLinkOtherFunc = "link_other_func" // FieldMAC holds the string denoting the mac field in the database. FieldMAC = "mac" // FieldStringArray holds the string denoting the string_array field in the database. @@ -155,6 +157,10 @@ var ( UpdateDefaultInt64 func() int64 // ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. ValidateOptionalInt32Validator func(int32) error + // DefaultLinkOther holds the default value on creation for the "link_other" field. + DefaultLinkOther *schema.Link + // DefaultLinkOtherFunc holds the default value on creation for the "link_other_func" field. + DefaultLinkOtherFunc func() *schema.Link // MACValidator is a validator for the "mac" field. It is called by the builders before save. MACValidator func(string) error // UpdateDefaultDuration holds the default value on update for the "duration" field. diff --git a/entc/integration/gremlin/ent/fieldtype/where.go b/entc/integration/gremlin/ent/fieldtype/where.go index 577b0d4b7..039b309bf 100644 --- a/entc/integration/gremlin/ent/fieldtype/where.go +++ b/entc/integration/gremlin/ent/fieldtype/where.go @@ -274,6 +274,13 @@ func LinkOther(v *schema.Link) predicate.FieldType { }) } +// LinkOtherFunc applies equality check predicate on the "link_other_func" field. It's identical to LinkOtherFuncEQ. +func LinkOtherFunc(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.EQ(v)) + }) +} + // MAC applies equality check predicate on the "mac" field. It's identical to MACEQ. func MAC(v schema.MAC) predicate.FieldType { return predicate.FieldType(func(t *dsl.Traversal) { @@ -2524,6 +2531,84 @@ func LinkOtherNotNil() predicate.FieldType { }) } +// LinkOtherFuncEQ applies the EQ predicate on the "link_other_func" field. +func LinkOtherFuncEQ(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.EQ(v)) + }) +} + +// LinkOtherFuncNEQ applies the NEQ predicate on the "link_other_func" field. +func LinkOtherFuncNEQ(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.NEQ(v)) + }) +} + +// LinkOtherFuncIn applies the In predicate on the "link_other_func" field. +func LinkOtherFuncIn(vs ...*schema.Link) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.Within(v...)) + }) +} + +// LinkOtherFuncNotIn applies the NotIn predicate on the "link_other_func" field. +func LinkOtherFuncNotIn(vs ...*schema.Link) predicate.FieldType { + v := make([]interface{}, len(vs)) + for i := range v { + v[i] = vs[i] + } + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.Without(v...)) + }) +} + +// LinkOtherFuncGT applies the GT predicate on the "link_other_func" field. +func LinkOtherFuncGT(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.GT(v)) + }) +} + +// LinkOtherFuncGTE applies the GTE predicate on the "link_other_func" field. +func LinkOtherFuncGTE(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.GTE(v)) + }) +} + +// LinkOtherFuncLT applies the LT predicate on the "link_other_func" field. +func LinkOtherFuncLT(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.LT(v)) + }) +} + +// LinkOtherFuncLTE applies the LTE predicate on the "link_other_func" field. +func LinkOtherFuncLTE(v *schema.Link) predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.Has(Label, FieldLinkOtherFunc, p.LTE(v)) + }) +} + +// LinkOtherFuncIsNil applies the IsNil predicate on the "link_other_func" field. +func LinkOtherFuncIsNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).HasNot(FieldLinkOtherFunc) + }) +} + +// LinkOtherFuncNotNil applies the NotNil predicate on the "link_other_func" field. +func LinkOtherFuncNotNil() predicate.FieldType { + return predicate.FieldType(func(t *dsl.Traversal) { + t.HasLabel(Label).Has(FieldLinkOtherFunc) + }) +} + // MACEQ applies the EQ predicate on the "mac" field. func MACEQ(v schema.MAC) predicate.FieldType { return predicate.FieldType(func(t *dsl.Traversal) { diff --git a/entc/integration/gremlin/ent/fieldtype_create.go b/entc/integration/gremlin/ent/fieldtype_create.go index d5066ffbb..4421387c5 100644 --- a/entc/integration/gremlin/ent/fieldtype_create.go +++ b/entc/integration/gremlin/ent/fieldtype_create.go @@ -361,6 +361,12 @@ func (ftc *FieldTypeCreate) SetLinkOther(s *schema.Link) *FieldTypeCreate { return ftc } +// SetLinkOtherFunc sets the "link_other_func" field. +func (ftc *FieldTypeCreate) SetLinkOtherFunc(s *schema.Link) *FieldTypeCreate { + ftc.mutation.SetLinkOtherFunc(s) + return ftc +} + // SetMAC sets the "mac" field. func (ftc *FieldTypeCreate) SetMAC(s schema.MAC) *FieldTypeCreate { ftc.mutation.SetMAC(s) @@ -824,6 +830,14 @@ func (ftc *FieldTypeCreate) ExecX(ctx context.Context) { // defaults sets the default values of the builder before save. func (ftc *FieldTypeCreate) defaults() { + if _, ok := ftc.mutation.LinkOther(); !ok { + v := fieldtype.DefaultLinkOther + ftc.mutation.SetLinkOther(v) + } + if _, ok := ftc.mutation.LinkOtherFunc(); !ok { + v := fieldtype.DefaultLinkOtherFunc() + ftc.mutation.SetLinkOtherFunc(v) + } if _, ok := ftc.mutation.Dir(); !ok { v := fieldtype.DefaultDir() ftc.mutation.SetDir(v) @@ -1037,6 +1051,9 @@ func (ftc *FieldTypeCreate) gremlin() *dsl.Traversal { if value, ok := ftc.mutation.LinkOther(); ok { v.Property(dsl.Single, fieldtype.FieldLinkOther, value) } + if value, ok := ftc.mutation.LinkOtherFunc(); ok { + v.Property(dsl.Single, fieldtype.FieldLinkOtherFunc, value) + } if value, ok := ftc.mutation.MAC(); ok { v.Property(dsl.Single, fieldtype.FieldMAC, value) } diff --git a/entc/integration/gremlin/ent/fieldtype_update.go b/entc/integration/gremlin/ent/fieldtype_update.go index bdbeb2e0e..5dca2bdfa 100644 --- a/entc/integration/gremlin/ent/fieldtype_update.go +++ b/entc/integration/gremlin/ent/fieldtype_update.go @@ -669,6 +669,18 @@ func (ftu *FieldTypeUpdate) ClearLinkOther() *FieldTypeUpdate { return ftu } +// SetLinkOtherFunc sets the "link_other_func" field. +func (ftu *FieldTypeUpdate) SetLinkOtherFunc(s *schema.Link) *FieldTypeUpdate { + ftu.mutation.SetLinkOtherFunc(s) + return ftu +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (ftu *FieldTypeUpdate) ClearLinkOtherFunc() *FieldTypeUpdate { + ftu.mutation.ClearLinkOtherFunc() + return ftu +} + // SetMAC sets the "mac" field. func (ftu *FieldTypeUpdate) SetMAC(s schema.MAC) *FieldTypeUpdate { ftu.mutation.SetMAC(s) @@ -1589,6 +1601,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if value, ok := ftu.mutation.LinkOther(); ok { v.Property(dsl.Single, fieldtype.FieldLinkOther, value) } + if value, ok := ftu.mutation.LinkOtherFunc(); ok { + v.Property(dsl.Single, fieldtype.FieldLinkOtherFunc, value) + } if value, ok := ftu.mutation.MAC(); ok { v.Property(dsl.Single, fieldtype.FieldMAC, value) } @@ -1785,6 +1800,9 @@ func (ftu *FieldTypeUpdate) gremlin() *dsl.Traversal { if ftu.mutation.LinkOtherCleared() { properties = append(properties, fieldtype.FieldLinkOther) } + if ftu.mutation.LinkOtherFuncCleared() { + properties = append(properties, fieldtype.FieldLinkOtherFunc) + } if ftu.mutation.MACCleared() { properties = append(properties, fieldtype.FieldMAC) } @@ -2524,6 +2542,18 @@ func (ftuo *FieldTypeUpdateOne) ClearLinkOther() *FieldTypeUpdateOne { return ftuo } +// SetLinkOtherFunc sets the "link_other_func" field. +func (ftuo *FieldTypeUpdateOne) SetLinkOtherFunc(s *schema.Link) *FieldTypeUpdateOne { + ftuo.mutation.SetLinkOtherFunc(s) + return ftuo +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (ftuo *FieldTypeUpdateOne) ClearLinkOtherFunc() *FieldTypeUpdateOne { + ftuo.mutation.ClearLinkOtherFunc() + return ftuo +} + // SetMAC sets the "mac" field. func (ftuo *FieldTypeUpdateOne) SetMAC(s schema.MAC) *FieldTypeUpdateOne { ftuo.mutation.SetMAC(s) @@ -3456,6 +3486,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if value, ok := ftuo.mutation.LinkOther(); ok { v.Property(dsl.Single, fieldtype.FieldLinkOther, value) } + if value, ok := ftuo.mutation.LinkOtherFunc(); ok { + v.Property(dsl.Single, fieldtype.FieldLinkOtherFunc, value) + } if value, ok := ftuo.mutation.MAC(); ok { v.Property(dsl.Single, fieldtype.FieldMAC, value) } @@ -3652,6 +3685,9 @@ func (ftuo *FieldTypeUpdateOne) gremlin(id string) *dsl.Traversal { if ftuo.mutation.LinkOtherCleared() { properties = append(properties, fieldtype.FieldLinkOther) } + if ftuo.mutation.LinkOtherFuncCleared() { + properties = append(properties, fieldtype.FieldLinkOtherFunc) + } if ftuo.mutation.MACCleared() { properties = append(properties, fieldtype.FieldMAC) } diff --git a/entc/integration/gremlin/ent/mutation.go b/entc/integration/gremlin/ent/mutation.go index 2f65eec44..1c37bb2f0 100644 --- a/entc/integration/gremlin/ent/mutation.go +++ b/entc/integration/gremlin/ent/mutation.go @@ -1361,6 +1361,7 @@ type FieldTypeMutation struct { decimal *float64 adddecimal *float64 link_other **schema.Link + link_other_func **schema.Link mac *schema.MAC string_array *schema.Strings password *string @@ -3246,6 +3247,55 @@ func (m *FieldTypeMutation) ResetLinkOther() { delete(m.clearedFields, fieldtype.FieldLinkOther) } +// SetLinkOtherFunc sets the "link_other_func" field. +func (m *FieldTypeMutation) SetLinkOtherFunc(s *schema.Link) { + m.link_other_func = &s +} + +// LinkOtherFunc returns the value of the "link_other_func" field in the mutation. +func (m *FieldTypeMutation) LinkOtherFunc() (r *schema.Link, exists bool) { + v := m.link_other_func + if v == nil { + return + } + return *v, true +} + +// OldLinkOtherFunc returns the old "link_other_func" field's value of the FieldType entity. +// If the FieldType object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *FieldTypeMutation) OldLinkOtherFunc(ctx context.Context) (v *schema.Link, err error) { + if !m.op.Is(OpUpdateOne) { + return v, fmt.Errorf("OldLinkOtherFunc is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, fmt.Errorf("OldLinkOtherFunc requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldLinkOtherFunc: %w", err) + } + return oldValue.LinkOtherFunc, nil +} + +// ClearLinkOtherFunc clears the value of the "link_other_func" field. +func (m *FieldTypeMutation) ClearLinkOtherFunc() { + m.link_other_func = nil + m.clearedFields[fieldtype.FieldLinkOtherFunc] = struct{}{} +} + +// LinkOtherFuncCleared returns if the "link_other_func" field was cleared in this mutation. +func (m *FieldTypeMutation) LinkOtherFuncCleared() bool { + _, ok := m.clearedFields[fieldtype.FieldLinkOtherFunc] + return ok +} + +// ResetLinkOtherFunc resets all changes to the "link_other_func" field. +func (m *FieldTypeMutation) ResetLinkOtherFunc() { + m.link_other_func = nil + delete(m.clearedFields, fieldtype.FieldLinkOtherFunc) +} + // SetMAC sets the "mac" field. func (m *FieldTypeMutation) SetMAC(s schema.MAC) { m.mac = &s @@ -5111,7 +5161,7 @@ func (m *FieldTypeMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *FieldTypeMutation) Fields() []string { - fields := make([]string, 0, 63) + fields := make([]string, 0, 64) if m.int != nil { fields = append(fields, fieldtype.FieldInt) } @@ -5193,6 +5243,9 @@ func (m *FieldTypeMutation) Fields() []string { if m.link_other != nil { fields = append(fields, fieldtype.FieldLinkOther) } + if m.link_other_func != nil { + fields = append(fields, fieldtype.FieldLinkOtherFunc) + } if m.mac != nil { fields = append(fields, fieldtype.FieldMAC) } @@ -5363,6 +5416,8 @@ func (m *FieldTypeMutation) Field(name string) (ent.Value, bool) { return m.Decimal() case fieldtype.FieldLinkOther: return m.LinkOther() + case fieldtype.FieldLinkOtherFunc: + return m.LinkOtherFunc() case fieldtype.FieldMAC: return m.MAC() case fieldtype.FieldStringArray: @@ -5498,6 +5553,8 @@ func (m *FieldTypeMutation) OldField(ctx context.Context, name string) (ent.Valu return m.OldDecimal(ctx) case fieldtype.FieldLinkOther: return m.OldLinkOther(ctx) + case fieldtype.FieldLinkOtherFunc: + return m.OldLinkOtherFunc(ctx) case fieldtype.FieldMAC: return m.OldMAC(ctx) case fieldtype.FieldStringArray: @@ -5768,6 +5825,13 @@ func (m *FieldTypeMutation) SetField(name string, value ent.Value) error { } m.SetLinkOther(v) return nil + case fieldtype.FieldLinkOtherFunc: + v, ok := value.(*schema.Link) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetLinkOtherFunc(v) + return nil case fieldtype.FieldMAC: v, ok := value.(schema.MAC) if !ok { @@ -6491,6 +6555,9 @@ func (m *FieldTypeMutation) ClearedFields() []string { if m.FieldCleared(fieldtype.FieldLinkOther) { fields = append(fields, fieldtype.FieldLinkOther) } + if m.FieldCleared(fieldtype.FieldLinkOtherFunc) { + fields = append(fields, fieldtype.FieldLinkOtherFunc) + } if m.FieldCleared(fieldtype.FieldMAC) { fields = append(fields, fieldtype.FieldMAC) } @@ -6664,6 +6731,9 @@ func (m *FieldTypeMutation) ClearField(name string) error { case fieldtype.FieldLinkOther: m.ClearLinkOther() return nil + case fieldtype.FieldLinkOtherFunc: + m.ClearLinkOtherFunc() + return nil case fieldtype.FieldMAC: m.ClearMAC() return nil @@ -6846,6 +6916,9 @@ func (m *FieldTypeMutation) ResetField(name string) error { case fieldtype.FieldLinkOther: m.ResetLinkOther() return nil + case fieldtype.FieldLinkOtherFunc: + m.ResetLinkOtherFunc() + return nil case fieldtype.FieldMAC: m.ResetMAC() return nil diff --git a/entc/integration/gremlin/ent/runtime.go b/entc/integration/gremlin/ent/runtime.go index 1d4fd3020..91957b87c 100644 --- a/entc/integration/gremlin/ent/runtime.go +++ b/entc/integration/gremlin/ent/runtime.go @@ -65,36 +65,44 @@ func init() { fieldtypeDescValidateOptionalInt32 := fieldtypeFields[15].Descriptor() // fieldtype.ValidateOptionalInt32Validator is a validator for the "validate_optional_int32" field. It is called by the builders before save. fieldtype.ValidateOptionalInt32Validator = fieldtypeDescValidateOptionalInt32.Validators[0].(func(int32) error) + // fieldtypeDescLinkOther is the schema descriptor for link_other field. + fieldtypeDescLinkOther := fieldtypeFields[26].Descriptor() + // fieldtype.DefaultLinkOther holds the default value on creation for the link_other field. + fieldtype.DefaultLinkOther = fieldtypeDescLinkOther.Default.(*schema.Link) + // fieldtypeDescLinkOtherFunc is the schema descriptor for link_other_func field. + fieldtypeDescLinkOtherFunc := fieldtypeFields[27].Descriptor() + // fieldtype.DefaultLinkOtherFunc holds the default value on creation for the link_other_func field. + fieldtype.DefaultLinkOtherFunc = fieldtypeDescLinkOtherFunc.Default.(func() *schema.Link) // fieldtypeDescMAC is the schema descriptor for mac field. - fieldtypeDescMAC := fieldtypeFields[27].Descriptor() + fieldtypeDescMAC := fieldtypeFields[28].Descriptor() // fieldtype.MACValidator is a validator for the "mac" field. It is called by the builders before save. fieldtype.MACValidator = fieldtypeDescMAC.Validators[0].(func(string) error) // fieldtypeDescDuration is the schema descriptor for duration field. - fieldtypeDescDuration := fieldtypeFields[31].Descriptor() + fieldtypeDescDuration := fieldtypeFields[32].Descriptor() // fieldtype.UpdateDefaultDuration holds the default value on update for the duration field. fieldtype.UpdateDefaultDuration = fieldtypeDescDuration.UpdateDefault.(func() time.Duration) // fieldtypeDescDir is the schema descriptor for dir field. - fieldtypeDescDir := fieldtypeFields[32].Descriptor() + fieldtypeDescDir := fieldtypeFields[33].Descriptor() // fieldtype.DefaultDir holds the default value on creation for the dir field. fieldtype.DefaultDir = fieldtypeDescDir.Default.(func() http.Dir) // fieldtypeDescNdir is the schema descriptor for ndir field. - fieldtypeDescNdir := fieldtypeFields[33].Descriptor() + fieldtypeDescNdir := fieldtypeFields[34].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[34].Descriptor() + fieldtypeDescStr := fieldtypeFields[35].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[35].Descriptor() + fieldtypeDescNullStr := fieldtypeFields[36].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[36].Descriptor() + fieldtypeDescLink := fieldtypeFields[37].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) // fieldtypeDescRawData is the schema descriptor for raw_data field. - fieldtypeDescRawData := fieldtypeFields[42].Descriptor() + fieldtypeDescRawData := fieldtypeFields[43].Descriptor() // fieldtype.RawDataValidator is a validator for the "raw_data" field. It is called by the builders before save. fieldtype.RawDataValidator = func() func([]byte) error { validators := fieldtypeDescRawData.Validators @@ -112,21 +120,21 @@ func init() { } }() // fieldtypeDescIP is the schema descriptor for ip field. - fieldtypeDescIP := fieldtypeFields[44].Descriptor() + fieldtypeDescIP := fieldtypeFields[45].Descriptor() // fieldtype.DefaultIP holds the default value on creation for the ip field. fieldtype.DefaultIP = fieldtypeDescIP.Default.(func() net.IP) // fieldtype.IPValidator is a validator for the "ip" field. It is called by the builders before save. fieldtype.IPValidator = fieldtypeDescIP.Validators[0].(func([]byte) error) // fieldtypeDescPair is the schema descriptor for pair field. - fieldtypeDescPair := fieldtypeFields[57].Descriptor() + fieldtypeDescPair := fieldtypeFields[58].Descriptor() // fieldtype.DefaultPair holds the default value on creation for the pair field. fieldtype.DefaultPair = fieldtypeDescPair.Default.(func() schema.Pair) // fieldtypeDescVstring is the schema descriptor for vstring field. - fieldtypeDescVstring := fieldtypeFields[59].Descriptor() + fieldtypeDescVstring := fieldtypeFields[60].Descriptor() // fieldtype.DefaultVstring holds the default value on creation for the vstring field. fieldtype.DefaultVstring = fieldtypeDescVstring.Default.(func() schema.VString) // fieldtypeDescTriple is the schema descriptor for triple field. - fieldtypeDescTriple := fieldtypeFields[60].Descriptor() + fieldtypeDescTriple := fieldtypeFields[61].Descriptor() // fieldtype.DefaultTriple holds the default value on creation for the triple field. fieldtype.DefaultTriple = fieldtypeDescTriple.Default.(func() schema.Triple) fileFields := schema.File{}.Fields() diff --git a/entc/integration/json/ent/migrate/schema.go b/entc/integration/json/ent/migrate/schema.go index ae0196d63..473ee9b00 100644 --- a/entc/integration/json/ent/migrate/schema.go +++ b/entc/integration/json/ent/migrate/schema.go @@ -18,7 +18,7 @@ var ( {Name: "t", Type: field.TypeJSON, Nullable: true}, {Name: "url", Type: field.TypeJSON, Nullable: true}, {Name: "raw", Type: field.TypeJSON, Nullable: true}, - {Name: "dirs", Type: field.TypeJSON, Nullable: true}, + {Name: "dirs", Type: field.TypeJSON}, {Name: "ints", Type: field.TypeJSON, Nullable: true}, {Name: "floats", Type: field.TypeJSON, Nullable: true}, {Name: "strings", Type: field.TypeJSON, Nullable: true}, diff --git a/entc/integration/json/ent/mutation.go b/entc/integration/json/ent/mutation.go index 83d0df787..0064b39c2 100644 --- a/entc/integration/json/ent/mutation.go +++ b/entc/integration/json/ent/mutation.go @@ -309,22 +309,9 @@ func (m *UserMutation) OldDirs(ctx context.Context) (v []http.Dir, err error) { return oldValue.Dirs, nil } -// ClearDirs clears the value of the "dirs" field. -func (m *UserMutation) ClearDirs() { - m.dirs = nil - m.clearedFields[user.FieldDirs] = struct{}{} -} - -// DirsCleared returns if the "dirs" field was cleared in this mutation. -func (m *UserMutation) DirsCleared() bool { - _, ok := m.clearedFields[user.FieldDirs] - return ok -} - // ResetDirs resets all changes to the "dirs" field. func (m *UserMutation) ResetDirs() { m.dirs = nil - delete(m.clearedFields, user.FieldDirs) } // SetInts sets the "ints" field. @@ -657,9 +644,6 @@ func (m *UserMutation) ClearedFields() []string { if m.FieldCleared(user.FieldRaw) { fields = append(fields, user.FieldRaw) } - if m.FieldCleared(user.FieldDirs) { - fields = append(fields, user.FieldDirs) - } if m.FieldCleared(user.FieldInts) { fields = append(fields, user.FieldInts) } @@ -692,9 +676,6 @@ func (m *UserMutation) ClearField(name string) error { case user.FieldRaw: m.ClearRaw() return nil - case user.FieldDirs: - m.ClearDirs() - return nil case user.FieldInts: m.ClearInts() return nil diff --git a/entc/integration/json/ent/runtime.go b/entc/integration/json/ent/runtime.go index d0160ddf9..de6242b4c 100644 --- a/entc/integration/json/ent/runtime.go +++ b/entc/integration/json/ent/runtime.go @@ -6,8 +6,25 @@ package ent +import ( + "net/http" + + "entgo.io/ent/entc/integration/json/ent/schema" + "entgo.io/ent/entc/integration/json/ent/user" +) + // The init function reads all schema descriptors with runtime code // (default values, validators, hooks and policies) and stitches it // to their package variables. func init() { + userFields := schema.User{}.Fields() + _ = userFields + // userDescDirs is the schema descriptor for dirs field. + userDescDirs := userFields[3].Descriptor() + // user.DefaultDirs holds the default value on creation for the dirs field. + user.DefaultDirs = userDescDirs.Default.(func() []http.Dir) + // userDescInts is the schema descriptor for ints field. + userDescInts := userFields[4].Descriptor() + // user.DefaultInts holds the default value on creation for the ints field. + user.DefaultInts = userDescInts.Default.([]int) } diff --git a/entc/integration/json/ent/schema/user.go b/entc/integration/json/ent/schema/user.go index eee768db8..b30981b65 100644 --- a/entc/integration/json/ent/schema/user.go +++ b/entc/integration/json/ent/schema/user.go @@ -28,9 +28,12 @@ func (User) Fields() []ent.Field { field.JSON("raw", json.RawMessage{}). Optional(), field.JSON("dirs", []http.Dir{}). - Optional(), + Default(func() []http.Dir { + return []http.Dir{"/tmp"} + }), field.Ints("ints"). - Optional(), + Optional(). + Default([]int{1, 2, 3}), field.Floats("floats"). Optional(), field.Strings("strings"). diff --git a/entc/integration/json/ent/user/user.go b/entc/integration/json/ent/user/user.go index 7396450c9..c3a97bcbd 100644 --- a/entc/integration/json/ent/user/user.go +++ b/entc/integration/json/ent/user/user.go @@ -6,6 +6,10 @@ package user +import ( + "net/http" +) + const ( // Label holds the string label denoting the user type in the database. Label = "user" @@ -50,3 +54,10 @@ func ValidColumn(column string) bool { } return false } + +var ( + // DefaultDirs holds the default value on creation for the "dirs" field. + DefaultDirs func() []http.Dir + // DefaultInts holds the default value on creation for the "ints" field. + DefaultInts []int +) diff --git a/entc/integration/json/ent/user/where.go b/entc/integration/json/ent/user/where.go index ba723d4e3..8c723452a 100644 --- a/entc/integration/json/ent/user/where.go +++ b/entc/integration/json/ent/user/where.go @@ -136,20 +136,6 @@ func RawNotNil() predicate.User { }) } -// DirsIsNil applies the IsNil predicate on the "dirs" field. -func DirsIsNil() predicate.User { - return predicate.User(func(s *sql.Selector) { - s.Where(sql.IsNull(s.C(FieldDirs))) - }) -} - -// DirsNotNil applies the NotNil predicate on the "dirs" field. -func DirsNotNil() predicate.User { - return predicate.User(func(s *sql.Selector) { - s.Where(sql.NotNull(s.C(FieldDirs))) - }) -} - // IntsIsNil applies the IsNil predicate on the "ints" field. func IntsIsNil() predicate.User { return predicate.User(func(s *sql.Selector) { diff --git a/entc/integration/json/ent/user_create.go b/entc/integration/json/ent/user_create.go index ef86a3754..27dca8a11 100644 --- a/entc/integration/json/ent/user_create.go +++ b/entc/integration/json/ent/user_create.go @@ -9,6 +9,7 @@ package ent import ( "context" "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -79,6 +80,7 @@ func (uc *UserCreate) Save(ctx context.Context) (*User, error) { err error node *User ) + uc.defaults() if len(uc.hooks) == 0 { if err = uc.check(); err != nil { return nil, err @@ -136,8 +138,23 @@ func (uc *UserCreate) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (uc *UserCreate) defaults() { + if _, ok := uc.mutation.Dirs(); !ok { + v := user.DefaultDirs() + uc.mutation.SetDirs(v) + } + if _, ok := uc.mutation.Ints(); !ok { + v := user.DefaultInts + uc.mutation.SetInts(v) + } +} + // check runs all checks and user-defined validators on the builder. func (uc *UserCreate) check() error { + if _, ok := uc.mutation.Dirs(); !ok { + return &ValidationError{Name: "dirs", err: errors.New(`ent: missing required field "User.dirs"`)} + } return nil } @@ -238,6 +255,7 @@ func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { for i := range ucb.builders { func(i int, root context.Context) { builder := ucb.builders[i] + builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*UserMutation) if !ok { diff --git a/entc/integration/json/ent/user_update.go b/entc/integration/json/ent/user_update.go index 6128eda1d..569eed716 100644 --- a/entc/integration/json/ent/user_update.go +++ b/entc/integration/json/ent/user_update.go @@ -77,12 +77,6 @@ func (uu *UserUpdate) SetDirs(h []http.Dir) *UserUpdate { return uu } -// ClearDirs clears the value of the "dirs" field. -func (uu *UserUpdate) ClearDirs() *UserUpdate { - uu.mutation.ClearDirs() - return uu -} - // SetInts sets the "ints" field. func (uu *UserUpdate) SetInts(i []int) *UserUpdate { uu.mutation.SetInts(i) @@ -242,12 +236,6 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Column: user.FieldDirs, }) } - if uu.mutation.DirsCleared() { - _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ - Type: field.TypeJSON, - Column: user.FieldDirs, - }) - } if value, ok := uu.mutation.Ints(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeJSON, @@ -348,12 +336,6 @@ func (uuo *UserUpdateOne) SetDirs(h []http.Dir) *UserUpdateOne { return uuo } -// ClearDirs clears the value of the "dirs" field. -func (uuo *UserUpdateOne) ClearDirs() *UserUpdateOne { - uuo.mutation.ClearDirs() - return uuo -} - // SetInts sets the "ints" field. func (uuo *UserUpdateOne) SetInts(i []int) *UserUpdateOne { uuo.mutation.SetInts(i) @@ -537,12 +519,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Column: user.FieldDirs, }) } - if uuo.mutation.DirsCleared() { - _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ - Type: field.TypeJSON, - Column: user.FieldDirs, - }) - } if value, ok := uuo.mutation.Ints(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeJSON, diff --git a/entc/integration/json/json_test.go b/entc/integration/json/json_test.go index 194f0ec19..bdad79931 100644 --- a/entc/integration/json/json_test.go +++ b/entc/integration/json/json_test.go @@ -143,6 +143,11 @@ func Ints(t *testing.T, client *ent.Client) { usr = usr.Update().ClearInts().SaveX(ctx) require.Empty(t, usr.Ints) require.Empty(t, client.User.GetX(ctx, usr.ID).Ints) + + usr = client.User.Create().SaveX(ctx) + require.Equal(t, []int{1, 2, 3}, usr.Ints) + usr = client.User.GetX(ctx, usr.ID) + require.Equal(t, []int{1, 2, 3}, usr.Ints) } func Floats(t *testing.T, client *ent.Client) { @@ -189,6 +194,11 @@ func Dirs(t *testing.T, client *ent.Client) { usr := client.User.Create().SetDirs(dirs).SaveX(ctx) require.Equal(t, dirs, usr.Dirs) require.Equal(t, dirs, client.User.GetX(ctx, usr.ID).Dirs) + + usr = client.User.Create().SaveX(ctx) + require.Equal(t, []http.Dir{"/tmp"}, usr.Dirs) + usr = client.User.GetX(ctx, usr.ID) + require.Equal(t, []http.Dir{"/tmp"}, usr.Dirs) } func URL(t *testing.T, client *ent.Client) { diff --git a/entc/integration/type_test.go b/entc/integration/type_test.go index b56fc1ba2..8274dd240 100644 --- a/entc/integration/type_test.go +++ b/entc/integration/type_test.go @@ -116,6 +116,7 @@ func Types(t *testing.T, client *ent.Client) { exists, err = client.FieldType.Query().Where(fieldtype.DurationLT(time.Hour)).Exist(ctx) require.NoError(err) require.False(exists) + require.Equal("127.0.0.1", ft.LinkOtherFunc.String()) err = client.FieldType.Create(). SetInt(1). diff --git a/entc/load/schema_test.go b/entc/load/schema_test.go index fa332ed57..6a40798d8 100644 --- a/entc/load/schema_test.go +++ b/entc/load/schema_test.go @@ -8,6 +8,7 @@ import ( "context" "encoding/json" "math" + "net/http" "reflect" "testing" "time" @@ -299,6 +300,8 @@ func (WithDefaults) Fields() []ent.Field { }), field.Float("balance"). Default(0), + field.JSON("dirs", []http.Dir{}). + Default([]http.Dir{"/tmp"}), } } @@ -329,6 +332,7 @@ func TestMarshalDefaults(t *testing.T) { require.True(t, schema.Fields[5].Default) require.Equal(t, schema.Fields[5].DefaultKind, reflect.Func) require.True(t, schema.Fields[6].Default) + require.True(t, schema.Fields[7].Default) } type TimeMixin struct { diff --git a/schema/field/field.go b/schema/field/field.go index f61ab140e..e18530946 100644 --- a/schema/field/field.go +++ b/schema/field/field.go @@ -732,6 +732,28 @@ func (b *jsonBuilder) Annotations(annotations ...schema.Annotation) *jsonBuilder return b } +// Default sets the default value of the field. For example: +// +// field.JSON("dirs", []http.Dir{}). +// // A static default value. +// Default([]http.Dir{"/tmp"}) +// +// field.JSON("dirs", []http.Dir{}). +// // A function for generating the default value. +// Default(DefaultDirs) +// +func (b *jsonBuilder) Default(v interface{}) *jsonBuilder { + b.desc.Default = v + switch fieldT, defaultT := b.desc.Info.RType.rtype, reflect.TypeOf(v); { + case fieldT == defaultT: + case defaultT.Kind() == reflect.Func: + b.desc.checkDefaultFunc(b.desc.Info.RType.rtype) + default: + b.desc.Err = fmt.Errorf("expect type (func() %[1]s) or (%[1]s) for other default value", b.desc.Info) + } + return b +} + // Descriptor implements the ent.Field interface by returning its descriptor. func (b *jsonBuilder) Descriptor() *Descriptor { return b.desc diff --git a/schema/field/field_test.go b/schema/field/field_test.go index 39ba00d89..59634379d 100644 --- a/schema/field/field_test.go +++ b/schema/field/field_test.go @@ -467,13 +467,31 @@ func TestJSON(t *testing.T) { fd = field.Strings("strings"). Optional(). + Default([]string{"a", "b"}). Descriptor() + assert.NoError(t, fd.Err) assert.True(t, fd.Optional) assert.Empty(t, fd.Info.PkgPath) assert.Equal(t, "strings", fd.Name) + assert.Equal(t, []string{"a", "b"}, fd.Default) assert.Equal(t, field.TypeJSON, fd.Info.Type) assert.Equal(t, "[]string", fd.Info.String()) + fd = field.JSON("dirs", []http.Dir{}). + Default([]http.Dir{"a", "b"}). + Descriptor() + assert.NoError(t, fd.Err) + fd = field.JSON("dirs", []http.Dir{}). + Default(func() []http.Dir { + return []http.Dir{"/tmp"} + }). + Descriptor() + assert.NoError(t, fd.Err) + fd = field.JSON("dirs", []http.Dir{}). + Default([]string{"a", "b"}). + Descriptor() + assert.Error(t, fd.Err) + fd = field.JSON("values", &url.Values{}).Descriptor() assert.Equal(t, "net/url", fd.Info.PkgPath) fd = field.JSON("values", []url.Values{}).Descriptor()