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