in datahub/types.go [184:338]
func validateFieldValue(ft FieldType, val interface{}) (DataType, error) {
switch ft {
case BIGINT:
realval, err := getIntegerValue(val)
if err != nil {
return nil, err
}
if int64(realval) < -9223372036854775807 || int64(realval) > 9223372036854775807 {
return nil, fmt.Errorf("BIGINT type field must be in [-9223372036854775807,9223372036854775807]")
}
return Bigint(realval), nil
case STRING:
var realval String
switch v := val.(type) {
case String:
realval = v
case string:
realval = String(v)
default:
return nil, fmt.Errorf("value type[%T] not match field type[STRING]", val)
}
return realval, nil
case BOOLEAN:
switch v := val.(type) {
case Boolean:
return v, nil
case bool:
return Boolean(v), nil
default:
return nil, fmt.Errorf("value type[%T] not match field type[BOOLEAN]", val)
}
case DOUBLE:
switch v := val.(type) {
case Double:
return v, nil
case float64:
return Double(v), nil
case json.Number:
nval, err := v.Float64()
if err != nil {
return nil, err
}
return Double(nval), nil
default:
return nil, fmt.Errorf("value type[%T] not match field type[DOUBLE]", val)
}
case TIMESTAMP:
var realval Timestamp
switch v := val.(type) {
case Timestamp:
realval = v
case uint:
realval = Timestamp(v)
case uint8:
realval = Timestamp(v)
case uint16:
realval = Timestamp(v)
case uint32:
realval = Timestamp(v)
case uint64:
realval = Timestamp(v)
case int:
if v < 0 {
return nil, fmt.Errorf("TIMESTAMP type field must be in positive")
}
realval = Timestamp(v)
case int8:
if v < 0 {
return nil, fmt.Errorf("TIMESTAMP type field must be in positive")
}
realval = Timestamp(v)
case int16:
if v < 0 {
return nil, fmt.Errorf("TIMESTAMP type field must be in positive")
}
realval = Timestamp(v)
case int32:
if v < 0 {
return nil, fmt.Errorf("TIMESTAMP type field must be in positive")
}
realval = Timestamp(v)
case int64:
if v < 0 {
return nil, fmt.Errorf("TIMESTAMP type field must be in positive")
}
realval = Timestamp(v)
case json.Number:
nval, err := v.Int64()
if err != nil {
return nil, err
}
if nval < 0 {
return nil, fmt.Errorf("TIMESTAMP type field must be in positive")
}
realval = Timestamp(nval)
default:
return nil, fmt.Errorf("value type[%T] not match field type[TIMESTAMP]", val)
}
return realval, nil
case DECIMAL:
var realval Decimal
switch v := val.(type) {
case decimal.Decimal:
realval = Decimal(v)
default:
return nil, fmt.Errorf("value type[%T] not match field type[DECIMAL]", val)
}
return realval, nil
case INTEGER:
realval, err := getIntegerValue(val)
if err != nil {
return nil, err
}
if realval > math.MaxInt32 || realval < math.MinInt32 {
return nil, fmt.Errorf("%T exceed the range of INTEGER", val)
}
return Integer(realval), nil
case FLOAT:
switch v := val.(type) {
case Float:
return v, nil
case float32:
return Float(v), nil
case json.Number:
nval, err := v.Float64()
if err != nil {
return nil, err
}
return Float(nval), nil
default:
return nil, fmt.Errorf("value type[%T] not match field type[FLOAT]", val)
}
case TINYINT:
realval, err := getIntegerValue(val)
if err != nil {
return nil, err
}
if realval > math.MaxInt8 || realval < math.MinInt8 {
return nil, fmt.Errorf("%T exceed the range of TINYINT", val)
}
return Tinyint(realval), nil
case SMALLINT:
realval, err := getIntegerValue(val)
if err != nil {
return nil, err
}
if realval > math.MaxInt16 || realval < math.MinInt16 {
return nil, fmt.Errorf("%T exceed the range of TINYINT", val)
}
return Smallint(realval), nil
default:
return nil, fmt.Errorf("field type[%T] is not illegal", ft)
}
}