schema/field: add DefaultFunc for numeric types and add support for it in entc (#1153)

* entc/load: allow defaultfuncs on numeric types

* schema/field: add DefaultFunc on numeric types

* docs: document DefaultFunc better

* chore: update generated files

* pr: address issues

* docs: updates on faq, address requested changes
This commit is contained in:
Morgan
2021-01-10 13:05:45 +01:00
committed by GitHub
parent 727e41e357
commit e4cc63c411
8 changed files with 239 additions and 13 deletions

View File

@@ -108,6 +108,13 @@ 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 interface{}) *{{ $builder }} {
b.desc.Default = fn
return b
}
// Nillable indicates that this field is a nillable.
// Unlike "Optional" only fields, "Nillable" fields are pointers in the generated field.
func (b *{{ $builder }}) Nillable() *{{ $builder }} {
@@ -191,6 +198,9 @@ func (b *{{ $builder }}) Annotations(annotations ...schema.Annotation) *{{ $buil
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc({{ $t }}Type)
}
return b.desc
}