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

@@ -174,6 +174,13 @@ func (b *intBuilder) Default(i int) *intBuilder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *intBuilder) DefaultFunc(fn interface{}) *intBuilder {
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 *intBuilder) Nillable() *intBuilder {
@@ -256,6 +263,9 @@ func (b *intBuilder) Annotations(annotations ...schema.Annotation) *intBuilder {
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *intBuilder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(intType)
}
return b.desc
}
@@ -314,6 +324,13 @@ func (b *uintBuilder) Default(i uint) *uintBuilder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *uintBuilder) DefaultFunc(fn interface{}) *uintBuilder {
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 *uintBuilder) Nillable() *uintBuilder {
@@ -396,6 +413,9 @@ func (b *uintBuilder) Annotations(annotations ...schema.Annotation) *uintBuilder
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uintBuilder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(uintType)
}
return b.desc
}
@@ -464,6 +484,13 @@ func (b *int8Builder) Default(i int8) *int8Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *int8Builder) DefaultFunc(fn interface{}) *int8Builder {
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 *int8Builder) Nillable() *int8Builder {
@@ -546,6 +573,9 @@ func (b *int8Builder) Annotations(annotations ...schema.Annotation) *int8Builder
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int8Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(int8Type)
}
return b.desc
}
@@ -614,6 +644,13 @@ func (b *int16Builder) Default(i int16) *int16Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *int16Builder) DefaultFunc(fn interface{}) *int16Builder {
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 *int16Builder) Nillable() *int16Builder {
@@ -696,6 +733,9 @@ func (b *int16Builder) Annotations(annotations ...schema.Annotation) *int16Build
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int16Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(int16Type)
}
return b.desc
}
@@ -764,6 +804,13 @@ func (b *int32Builder) Default(i int32) *int32Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *int32Builder) DefaultFunc(fn interface{}) *int32Builder {
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 *int32Builder) Nillable() *int32Builder {
@@ -846,6 +893,9 @@ func (b *int32Builder) Annotations(annotations ...schema.Annotation) *int32Build
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int32Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(int32Type)
}
return b.desc
}
@@ -914,6 +964,13 @@ func (b *int64Builder) Default(i int64) *int64Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *int64Builder) DefaultFunc(fn interface{}) *int64Builder {
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 *int64Builder) Nillable() *int64Builder {
@@ -996,6 +1053,9 @@ func (b *int64Builder) Annotations(annotations ...schema.Annotation) *int64Build
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *int64Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(int64Type)
}
return b.desc
}
@@ -1054,6 +1114,13 @@ func (b *uint8Builder) Default(i uint8) *uint8Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *uint8Builder) DefaultFunc(fn interface{}) *uint8Builder {
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 *uint8Builder) Nillable() *uint8Builder {
@@ -1136,6 +1203,9 @@ func (b *uint8Builder) Annotations(annotations ...schema.Annotation) *uint8Build
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint8Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(uint8Type)
}
return b.desc
}
@@ -1194,6 +1264,13 @@ func (b *uint16Builder) Default(i uint16) *uint16Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *uint16Builder) DefaultFunc(fn interface{}) *uint16Builder {
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 *uint16Builder) Nillable() *uint16Builder {
@@ -1276,6 +1353,9 @@ func (b *uint16Builder) Annotations(annotations ...schema.Annotation) *uint16Bui
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint16Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(uint16Type)
}
return b.desc
}
@@ -1334,6 +1414,13 @@ func (b *uint32Builder) Default(i uint32) *uint32Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *uint32Builder) DefaultFunc(fn interface{}) *uint32Builder {
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 *uint32Builder) Nillable() *uint32Builder {
@@ -1416,6 +1503,9 @@ func (b *uint32Builder) Annotations(annotations ...schema.Annotation) *uint32Bui
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint32Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(uint32Type)
}
return b.desc
}
@@ -1474,6 +1564,13 @@ func (b *uint64Builder) Default(i uint64) *uint64Builder {
return b
}
// DefaultFunc sets the function that is applied to set the default value
// of the field on creation.
func (b *uint64Builder) DefaultFunc(fn interface{}) *uint64Builder {
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 *uint64Builder) Nillable() *uint64Builder {
@@ -1556,6 +1653,9 @@ func (b *uint64Builder) Annotations(annotations ...schema.Annotation) *uint64Bui
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *uint64Builder) Descriptor() *Descriptor {
if b.desc.Default != nil {
b.desc.checkDefaultFunc(uint64Type)
}
return b.desc
}