schema/field: add DefaultFunc and UpdateDefault to float types (#4256)

* feat: add `DefaultFunc` to float type

* feat: add `UpdateDefault` to float type

* test: update `TestMarshalDefaults` in `schema_test.go`
This commit is contained in:
Tantatorn Suksangwarn
2024-11-30 05:26:01 +07:00
committed by GitHub
parent 365b498176
commit eb09059141
4 changed files with 93 additions and 0 deletions

View File

@@ -306,6 +306,10 @@ func (WithDefaults) Fields() []ent.Field {
Default(0),
field.JSON("dirs", []http.Dir{}).
Default([]http.Dir{"/tmp"}),
field.Float("float_default_func").
DefaultFunc(func() float64 {
return math.Pi
}),
}
}
@@ -337,6 +341,7 @@ func TestMarshalDefaults(t *testing.T) {
require.Equal(t, schema.Fields[5].DefaultKind, reflect.Func)
require.True(t, schema.Fields[6].Default)
require.True(t, schema.Fields[7].Default)
require.Equal(t, schema.Fields[8].DefaultKind, reflect.Func)
}
type TimeMixin struct {

View File

@@ -157,6 +157,30 @@ func TestFloat(t *testing.T) {
assert.Error(t, fd.Err)
}
func TestFloat_DefaultFunc(t *testing.T) {
type CustomFloat float64
f1 := func() CustomFloat { return 1.2 }
fd := field.Float("weight").DefaultFunc(f1).GoType(CustomFloat(0.)).Descriptor()
assert.NoError(t, fd.Err)
fd = field.Float("weight").DefaultFunc(f1).Descriptor()
assert.Error(t, fd.Err, "`var _ float = f1()` should fail")
f2 := func() float64 { return 1000 }
fd = field.Float("weight").GoType(CustomFloat(0)).DefaultFunc(f2).Descriptor()
assert.Error(t, fd.Err, "`var _ CustomFloat = f2()` should fail")
fd = field.Float("weight").DefaultFunc(f2).UpdateDefault(f2).Descriptor()
assert.NoError(t, fd.Err)
assert.NotNil(t, fd.Default)
assert.NotNil(t, fd.UpdateDefault)
f3 := func() float64 { return 1.2 }
fd = field.Float("weight").DefaultFunc(f3).Descriptor()
assert.NoError(t, fd.Err)
}
func TestBool(t *testing.T) {
fd := field.Bool("active").Default(true).Comment("comment").Immutable().Descriptor()
assert.Equal(t, "active", fd.Name)

View File

@@ -320,6 +320,25 @@ func (b *{{ $builder }}) Default(i {{ $t }}) *{{ $builder }} {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *{{ $builder }}) DefaultFunc(fn any) *{{ $builder }} {
b.desc.Default = fn
return b
}
// UpdateDefault sets the function that is applied to set default value
// of the field on update. For example:
//
// field.{{ title $t.String }}("{{$t.String}}").
// Default(0).
// UpdateDefault(GenNumber),
//
func (b *{{ $builder }}) UpdateDefault(fn any) *{{ $builder }} {
b.desc.UpdateDefault = fn
return b
}
// Nillable indicates that this field is a nillable.
// Unlike "Optional" only fields, "Nillable" fields are pointers in the generated struct.
func (b *{{ $builder }}) Nillable() *{{ $builder }} {
@@ -434,6 +453,9 @@ func (b *{{ $builder }}) Deprecated(reason ...string) *{{ $builder }} {
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
if b.desc.Default != nil || b.desc.UpdateDefault != nil {
b.desc.checkDefaultFunc({{ $t }}Type)
}
b.desc.checkGoType({{ $t }}Type)
return b.desc
}

View File

@@ -2115,6 +2115,24 @@ func (b *float64Builder) Default(i float64) *float64Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *float64Builder) DefaultFunc(fn any) *float64Builder {
b.desc.Default = fn
return b
}
// UpdateDefault sets the function that is applied to set default value
// of the field on update. For example:
//
// field.Float64("float64").
// Default(0).
// UpdateDefault(GenNumber),
func (b *float64Builder) UpdateDefault(fn any) *float64Builder {
b.desc.UpdateDefault = fn
return b
}
// Nillable indicates that this field is a nillable.
// Unlike "Optional" only fields, "Nillable" fields are pointers in the generated struct.
func (b *float64Builder) Nillable() *float64Builder {
@@ -2224,6 +2242,9 @@ func (b *float64Builder) Deprecated(reason ...string) *float64Builder {
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float64Builder) Descriptor() *Descriptor {
if b.desc.Default != nil || b.desc.UpdateDefault != nil {
b.desc.checkDefaultFunc(float64Type)
}
b.desc.checkGoType(float64Type)
return b.desc
}
@@ -2288,6 +2309,24 @@ func (b *float32Builder) Default(i float32) *float32Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *float32Builder) DefaultFunc(fn any) *float32Builder {
b.desc.Default = fn
return b
}
// UpdateDefault sets the function that is applied to set default value
// of the field on update. For example:
//
// field.Float32("float32").
// Default(0).
// UpdateDefault(GenNumber),
func (b *float32Builder) UpdateDefault(fn any) *float32Builder {
b.desc.UpdateDefault = fn
return b
}
// Nillable indicates that this field is a nillable.
// Unlike "Optional" only fields, "Nillable" fields are pointers in the generated struct.
func (b *float32Builder) Nillable() *float32Builder {
@@ -2397,6 +2436,9 @@ func (b *float32Builder) Deprecated(reason ...string) *float32Builder {
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float32Builder) Descriptor() *Descriptor {
if b.desc.Default != nil || b.desc.UpdateDefault != nil {
b.desc.checkDefaultFunc(float32Type)
}
b.desc.checkGoType(float32Type)
return b.desc
}