schema/field: improve error message for GoType (#969)

This commit is contained in:
Ariel Mashraki
2020-11-20 23:48:54 +02:00
committed by GitHub
parent d10b888e1c
commit fe47093c55
8 changed files with 252 additions and 2 deletions

View File

@@ -945,6 +945,9 @@ func (d *Descriptor) goType(typ interface{}, expectType reflect.Type) {
}
default:
d.err = fmt.Errorf("GoType must be a %q type or ValueScanner", expectType)
if pt := reflect.PtrTo(t); pt.Implements(valueScannerType) {
d.err = fmt.Errorf("%s. Use %s instead", d.err, pt)
}
}
d.Info = info
}

View File

@@ -80,8 +80,10 @@ func TestInt(t *testing.T) {
assert.True(t, fd.Info.Nillable)
assert.True(t, fd.Info.ValueScanner())
fd = field.Int("count").GoType(sql.NullInt64{}).Descriptor()
assert.EqualError(t, fd.Err(), `GoType must be a "int" type or ValueScanner. Use *sql.NullInt64 instead`)
fd = field.Int("count").GoType(false).Descriptor()
assert.Error(t, fd.Err())
assert.EqualError(t, fd.Err(), `GoType must be a "int" type or ValueScanner`)
fd = field.Int("count").GoType(struct{}{}).Descriptor()
assert.Error(t, fd.Err())
fd = field.Int("count").GoType(new(Count)).Descriptor()