Improve ‘conv’ package for converting values to specific data types

This commit is contained in:
Tobie Morgan Hitchcock 2017-11-16 20:30:27 +00:00
parent da503f973e
commit 096984ad05

View file

@ -19,21 +19,26 @@ import (
"strconv"
"time"
"github.com/abcum/surreal/sql"
"github.com/asaskevich/govalidator"
)
func toNumber(str string) (int64, error) {
func toNumber(str string) (float64, error) {
val, err := strconv.ParseFloat(str, 64)
if err != nil {
val = 0.0
err = fmt.Errorf("Expected a number, but found '%v'", str)
}
return int64(val), err
return float64(int64(val)), err
}
func toDouble(str string) (float64, error) {
val, err := strconv.ParseFloat(str, 64)
if err != nil {
val = 0.0
err = fmt.Errorf("Expected a number, but found '%v'", str)
}
return float64(val), err
}
@ -42,16 +47,58 @@ func toBoolean(str string) (bool, error) {
val, err := strconv.ParseBool(str)
if err != nil {
val = false
err = fmt.Errorf("Expected a boolean, but found '%v'", str)
}
return bool(val), err
}
// --------------------------------------------------
func ConvertTo(t, k string, obj interface{}) (val interface{}, err error) {
switch t {
default:
return obj, nil
case "url":
return ConvertToUrl(obj)
case "uuid":
return ConvertToUuid(obj)
case "color":
return ConvertToColor(obj)
case "email":
return ConvertToEmail(obj)
case "phone":
return ConvertToPhone(obj)
case "array":
return ConvertToArray(obj)
case "object":
return ConvertToObject(obj)
case "domain":
return ConvertToDomain(obj)
case "base64":
return ConvertToBase64(obj)
case "string":
return ConvertToString(obj)
case "number":
return ConvertToNumber(obj)
case "double":
return ConvertToDouble(obj)
case "boolean":
return ConvertToBoolean(obj)
case "datetime":
return ConvertToDatetime(obj)
case "latitude":
return ConvertToLatitude(obj)
case "longitude":
return ConvertToLongitude(obj)
case "record":
return ConvertToRecord(obj, k)
}
}
func ConvertToUrl(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.IsURL(val) {
err = fmt.Errorf("Not a valid url")
err = fmt.Errorf("Expected a URL, but found '%v'", obj)
}
return
}
@ -59,7 +106,7 @@ func ConvertToUrl(obj interface{}) (val string, err error) {
func ConvertToUuid(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.IsUUID(val) {
err = fmt.Errorf("Not a valid uuid")
err = fmt.Errorf("Expected a UUID, but found '%v'", obj)
}
return
}
@ -67,7 +114,7 @@ func ConvertToUuid(obj interface{}) (val string, err error) {
func ConvertToEmail(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.IsEmail(val) {
err = fmt.Errorf("Not a valid email")
err = fmt.Errorf("Expected an email address, but found '%v'", obj)
}
return govalidator.NormalizeEmail(val)
}
@ -75,7 +122,7 @@ func ConvertToEmail(obj interface{}) (val string, err error) {
func ConvertToPhone(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.Matches(val, `^[\s\d\+\-\(\)]+$`) {
err = fmt.Errorf("Not a valid phone")
err = fmt.Errorf("Expected a phone number, but found '%v'", obj)
}
return
}
@ -83,7 +130,7 @@ func ConvertToPhone(obj interface{}) (val string, err error) {
func ConvertToColor(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.IsHexcolor(val) && !govalidator.IsRGBcolor(val) {
err = fmt.Errorf("Not a valid color")
err = fmt.Errorf("Expected a HEX or RGB color, but found '%v'", obj)
}
return
}
@ -92,7 +139,7 @@ func ConvertToArray(obj interface{}) (val []interface{}, err error) {
if now, ok := obj.([]interface{}); ok {
val = now
} else {
err = fmt.Errorf("Not a valid array")
err = fmt.Errorf("Expected an array, but found '%v'", obj)
}
return
}
@ -101,7 +148,7 @@ func ConvertToObject(obj interface{}) (val map[string]interface{}, err error) {
if now, ok := obj.(map[string]interface{}); ok {
val = now
} else {
err = fmt.Errorf("Not a valid object")
err = fmt.Errorf("Expected an object, but found '%v'", obj)
}
return
}
@ -109,7 +156,7 @@ func ConvertToObject(obj interface{}) (val map[string]interface{}, err error) {
func ConvertToDomain(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.IsDNSName(val) {
err = fmt.Errorf("Not a valid domain name")
err = fmt.Errorf("Expected a domain name, but found '%v'", obj)
}
return
}
@ -117,7 +164,7 @@ func ConvertToDomain(obj interface{}) (val string, err error) {
func ConvertToBase64(obj interface{}) (val string, err error) {
val = fmt.Sprintf("%v", obj)
if !govalidator.IsBase64(val) {
err = fmt.Errorf("Not valid base64 data")
err = fmt.Errorf("Expected base64 data, but found '%v'", obj)
}
return
}
@ -126,21 +173,19 @@ func ConvertToString(obj interface{}) (val string, err error) {
switch now := obj.(type) {
case string:
return now, err
case []interface{}:
return val, fmt.Errorf("Not valid string")
case map[string]interface{}:
return val, fmt.Errorf("Not valid string")
case []interface{}, map[string]interface{}:
return val, fmt.Errorf("Expected a string, but found '%v'", obj)
default:
return fmt.Sprintf("%v", obj), err
}
}
func ConvertToNumber(obj interface{}) (val int64, err error) {
func ConvertToNumber(obj interface{}) (val float64, err error) {
switch now := obj.(type) {
case int64:
return int64(now), err
return float64(now), err
case float64:
return int64(now), err
return float64(now), err
case string:
return toNumber(now)
default:
@ -178,7 +223,7 @@ func ConvertToDatetime(obj interface{}) (val time.Time, err error) {
if now, ok := obj.(time.Time); ok {
val = now
} else {
err = fmt.Errorf("Not a valid datetime")
err = fmt.Errorf("Expected a datetime, but found '%v'", obj)
}
return
}
@ -186,7 +231,7 @@ func ConvertToDatetime(obj interface{}) (val time.Time, err error) {
func ConvertToLatitude(obj interface{}) (val float64, err error) {
str := fmt.Sprintf("%v", obj)
if !govalidator.IsLatitude(str) {
err = fmt.Errorf("Not a valid latitude")
err = fmt.Errorf("Expected a latitude value, but found '%v'", obj)
}
return govalidator.ToFloat(str)
}
@ -194,20 +239,23 @@ func ConvertToLatitude(obj interface{}) (val float64, err error) {
func ConvertToLongitude(obj interface{}) (val float64, err error) {
str := fmt.Sprintf("%v", obj)
if !govalidator.IsLongitude(str) {
err = fmt.Errorf("Not a valid longitude")
err = fmt.Errorf("Expected a longitude value, but found '%v'", obj)
}
return govalidator.ToFloat(str)
}
func ConvertToOneOf(obj interface{}, pos ...interface{}) (val interface{}, err error) {
for _, now := range pos {
if num, ok := obj.(int64); ok {
if float64(num) == now {
return obj, err
}
} else if obj == now {
return obj, err
func ConvertToRecord(obj interface{}, tb string) (val *sql.Thing, err error) {
if now, ok := obj.(*sql.Thing); ok {
switch tb {
case now.TB:
val = now
case "":
val = now
default:
err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj)
}
} else {
err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj)
}
return nil, fmt.Errorf("Not a valid option")
return
}