schema/field: add GoType option for float fields (#534)

This commit is contained in:
Ariel Mashraki
2020-06-09 08:20:54 +03:00
committed by GitHub
parent e4d57a47f7
commit 9e0d5755ef
3 changed files with 66 additions and 0 deletions

View File

@@ -99,6 +99,30 @@ func TestFloat(t *testing.T) {
fd = f.Descriptor()
assert.Len(t, fd.Validators, 2)
assert.Equal(t, field.TypeFloat32, field.Float32("age").Descriptor().Info.Type)
type Count float64
fd = field.Float("active").GoType(Count(0)).Descriptor()
assert.NoError(t, fd.Err())
assert.Equal(t, "field_test.Count", fd.Info.Ident)
assert.Equal(t, "github.com/facebookincubator/ent/schema/field_test", fd.Info.PkgPath)
assert.Equal(t, "field_test.Count", fd.Info.String())
assert.False(t, fd.Info.Nillable)
assert.False(t, fd.Info.ValueScanner())
fd = field.Float("count").GoType(&sql.NullFloat64{}).Descriptor()
assert.NoError(t, fd.Err())
assert.Equal(t, "sql.NullFloat64", fd.Info.Ident)
assert.Equal(t, "database/sql", fd.Info.PkgPath)
assert.Equal(t, "sql.NullFloat64", fd.Info.String())
assert.True(t, fd.Info.Nillable)
assert.True(t, fd.Info.ValueScanner())
fd = field.Float("count").GoType(1).Descriptor()
assert.Error(t, fd.Err())
fd = field.Float("count").GoType(struct{}{}).Descriptor()
assert.Error(t, fd.Err())
fd = field.Float("count").GoType(new(Count)).Descriptor()
assert.Error(t, fd.Err())
}
func TestBool(t *testing.T) {

View File

@@ -309,10 +309,27 @@ func (b *{{ $builder }}) SchemaType(types map[string]string) *{{ $builder }} {
return b
}
{{ $tt := title $t.String }}
// GoType overrides the default Go type with a custom one.
//
// field.{{ $tt }}("{{ $t }}").
// GoType(pkg.{{ $tt }}(0))
//
func (b *{{ $builder }}) GoType(typ interface{}) *{{ $builder }} {
b.desc.goType(typ, {{ $t }}Type)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *{{ $builder }}) Descriptor() *Descriptor {
return b.desc
}
{{ end }}
var (
{{- range $t := $.Floats }}
{{ $t }}Type = reflect.TypeOf({{ $t }}(0))
{{- end }}
)
{{ end }}

View File

@@ -1558,6 +1558,16 @@ func (b *float64Builder) SchemaType(types map[string]string) *float64Builder {
return b
}
// GoType overrides the default Go type with a custom one.
//
// field.Float64("float64").
// GoType(pkg.Float64(0))
//
func (b *float64Builder) GoType(typ interface{}) *float64Builder {
b.desc.goType(typ, float64Type)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float64Builder) Descriptor() *Descriptor {
return b.desc
@@ -1681,7 +1691,22 @@ func (b *float32Builder) SchemaType(types map[string]string) *float32Builder {
return b
}
// GoType overrides the default Go type with a custom one.
//
// field.Float32("float32").
// GoType(pkg.Float32(0))
//
func (b *float32Builder) GoType(typ interface{}) *float32Builder {
b.desc.goType(typ, float32Type)
return b
}
// Descriptor implements the ent.Field interface by returning its descriptor.
func (b *float32Builder) Descriptor() *Descriptor {
return b.desc
}
var (
float64Type = reflect.TypeOf(float64(0))
float32Type = reflect.TypeOf(float32(0))
)