schema/field: relax signature of DefaultFunc for Bytes)(

This commit is contained in:
Travis Cline
2021-01-03 13:21:00 -08:00
parent 952a34c9c2
commit 53dfe95936

View File

@@ -507,7 +507,7 @@ func (b *bytesBuilder) Default(v []byte) *bytesBuilder {
// field.Bytes("cuid").
// DefaultFunc(cuid.New)
//
func (b *bytesBuilder) DefaultFunc(fn func() []byte) *bytesBuilder {
func (b *bytesBuilder) DefaultFunc(fn interface{}) *bytesBuilder {
b.desc.Default = fn
return b
}
@@ -597,6 +597,13 @@ func (b *bytesBuilder) SchemaType(types map[string]string) *bytesBuilder {
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *bytesBuilder) Descriptor() *Descriptor {
// If the Default is present and a function, check that it's signtaure is appropriate.
if b.desc.Default != nil {
typ := reflect.TypeOf(b.desc.Default)
if typ.Kind() == reflect.Func && (typ.NumIn() != 0 || typ.NumOut() != 1 || typ.Out(0).String() != b.desc.Info.String()) {
b.desc.Err = fmt.Errorf("expect type (func() %s) for default value", b.desc.Info)
}
}
return b.desc
}