raise error when failed to parse default value, close #3378

This commit is contained in:
Jinzhu 2020-09-03 18:20:57 +08:00
parent f2adb088c5
commit 78e9c9b748
1 changed files with 14 additions and 5 deletions

View File

@ -70,6 +70,8 @@ type Field struct {
} }
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
var err error
field := &Field{ field := &Field{
Name: fieldStruct.Name, Name: fieldStruct.Name,
BindNames: []string{fieldStruct.Name}, BindNames: []string{fieldStruct.Name},
@ -151,7 +153,6 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
} }
if num, ok := field.TagSettings["SIZE"]; ok { if num, ok := field.TagSettings["SIZE"]; ok {
var err error
if field.Size, err = strconv.Atoi(num); err != nil { if field.Size, err = strconv.Atoi(num); err != nil {
field.Size = -1 field.Size = -1
} }
@ -181,22 +182,30 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
case reflect.Bool: case reflect.Bool:
field.DataType = Bool field.DataType = Bool
if field.HasDefaultValue && field.DefaultValue != "" { if field.HasDefaultValue && field.DefaultValue != "" {
field.DefaultValueInterface, _ = strconv.ParseBool(field.DefaultValue) if field.DefaultValueInterface, err = strconv.ParseBool(field.DefaultValue); err != nil {
schema.err = fmt.Errorf("failed to parse %v as default value for bool, got error: %v", field.DefaultValue, err)
}
} }
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.DataType = Int field.DataType = Int
if field.HasDefaultValue && field.DefaultValue != "" { if field.HasDefaultValue && field.DefaultValue != "" {
field.DefaultValueInterface, _ = strconv.ParseInt(field.DefaultValue, 0, 64) if field.DefaultValueInterface, err = strconv.ParseInt(field.DefaultValue, 0, 64); err != nil {
schema.err = fmt.Errorf("failed to parse %v as default value for int, got error: %v", field.DefaultValue, err)
}
} }
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field.DataType = Uint field.DataType = Uint
if field.HasDefaultValue && field.DefaultValue != "" { if field.HasDefaultValue && field.DefaultValue != "" {
field.DefaultValueInterface, _ = strconv.ParseUint(field.DefaultValue, 0, 64) if field.DefaultValueInterface, err = strconv.ParseUint(field.DefaultValue, 0, 64); err != nil {
schema.err = fmt.Errorf("failed to parse %v as default value for uint, got error: %v", field.DefaultValue, err)
}
} }
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
field.DataType = Float field.DataType = Float
if field.HasDefaultValue && field.DefaultValue != "" { if field.HasDefaultValue && field.DefaultValue != "" {
field.DefaultValueInterface, _ = strconv.ParseFloat(field.DefaultValue, 64) if field.DefaultValueInterface, err = strconv.ParseFloat(field.DefaultValue, 64); err != nil {
schema.err = fmt.Errorf("failed to parse %v as default value for float, got error: %v", field.DefaultValue, err)
}
} }
case reflect.String: case reflect.String:
field.DataType = String field.DataType = String