2016-07-21 21:48:32 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package conv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-20 13:14:10 +00:00
|
|
|
"strconv"
|
2016-07-21 21:48:32 +00:00
|
|
|
"time"
|
|
|
|
|
2017-11-16 20:30:27 +00:00
|
|
|
"github.com/abcum/surreal/sql"
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
)
|
|
|
|
|
2017-11-16 20:30:27 +00:00
|
|
|
func toNumber(str string) (float64, error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
val, err := strconv.ParseFloat(str, 64)
|
|
|
|
if err != nil {
|
|
|
|
val = 0.0
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a number, but found '%v'", str)
|
|
|
|
|
2016-10-20 13:14:10 +00:00
|
|
|
}
|
2017-11-16 20:30:27 +00:00
|
|
|
return float64(int64(val)), err
|
2016-10-20 13:14:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toDouble(str string) (float64, error) {
|
|
|
|
val, err := strconv.ParseFloat(str, 64)
|
|
|
|
if err != nil {
|
|
|
|
val = 0.0
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a number, but found '%v'", str)
|
2016-10-20 13:14:10 +00:00
|
|
|
}
|
|
|
|
return float64(val), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBoolean(str string) (bool, error) {
|
|
|
|
val, err := strconv.ParseBool(str)
|
|
|
|
if err != nil {
|
|
|
|
val = false
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a boolean, but found '%v'", str)
|
2016-10-20 13:14:10 +00:00
|
|
|
}
|
|
|
|
return bool(val), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2018-01-10 13:17:50 +00:00
|
|
|
func MightBe(obj interface{}) (val interface{}, ok bool) {
|
|
|
|
switch now := obj.(type) {
|
|
|
|
case string:
|
|
|
|
if val, ok := MightBeDatetime(now); ok {
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
if val, ok := MightBeRecord(now); ok {
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func MightBeDatetime(obj string) (val interface{}, ok bool) {
|
|
|
|
if val, err := time.Parse(time.RFC3339, obj); err == nil {
|
|
|
|
return val, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func MightBeRecord(obj string) (val interface{}, ok bool) {
|
|
|
|
if val := sql.ParseThing(obj); val != nil {
|
|
|
|
return val, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2017-11-16 20:30:27 +00:00
|
|
|
func ConvertTo(t, k string, obj interface{}) (val interface{}, err error) {
|
|
|
|
switch t {
|
|
|
|
default:
|
|
|
|
return obj, nil
|
|
|
|
case "uuid":
|
|
|
|
return ConvertToUuid(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
func ConvertToUuid(obj interface{}) (val string, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
val = fmt.Sprintf("%v", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
if !govalidator.IsUUID(val) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a UUID, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToEmail(obj interface{}) (val string, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
val = fmt.Sprintf("%v", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
if !govalidator.IsEmail(val) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected an email address, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return govalidator.NormalizeEmail(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToPhone(obj interface{}) (val string, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
val = fmt.Sprintf("%v", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
if !govalidator.Matches(val, `^[\s\d\+\-\(\)]+$`) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a phone number, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToArray(obj interface{}) (val []interface{}, err error) {
|
|
|
|
if now, ok := obj.([]interface{}); ok {
|
|
|
|
val = now
|
|
|
|
} else {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected an array, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToObject(obj interface{}) (val map[string]interface{}, err error) {
|
|
|
|
if now, ok := obj.(map[string]interface{}); ok {
|
|
|
|
val = now
|
|
|
|
} else {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected an object, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToDomain(obj interface{}) (val string, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
val = fmt.Sprintf("%v", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
if !govalidator.IsDNSName(val) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a domain name, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToBase64(obj interface{}) (val string, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
val = fmt.Sprintf("%v", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
if !govalidator.IsBase64(val) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected base64 data, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToString(obj interface{}) (val string, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
switch now := obj.(type) {
|
|
|
|
case string:
|
2016-07-21 21:48:32 +00:00
|
|
|
return now, err
|
2017-11-16 20:30:27 +00:00
|
|
|
case []interface{}, map[string]interface{}:
|
|
|
|
return val, fmt.Errorf("Expected a string, but found '%v'", obj)
|
2016-10-20 13:14:10 +00:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%v", obj), err
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:30:27 +00:00
|
|
|
func ConvertToNumber(obj interface{}) (val float64, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
switch now := obj.(type) {
|
|
|
|
case int64:
|
2017-11-16 20:30:27 +00:00
|
|
|
return float64(now), err
|
2016-10-20 13:14:10 +00:00
|
|
|
case float64:
|
2017-11-16 20:30:27 +00:00
|
|
|
return float64(now), err
|
2016-10-20 13:14:10 +00:00
|
|
|
case string:
|
|
|
|
return toNumber(now)
|
|
|
|
default:
|
|
|
|
return toNumber(fmt.Sprintf("%v", obj))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToDouble(obj interface{}) (val float64, err error) {
|
|
|
|
switch now := obj.(type) {
|
|
|
|
case int64:
|
|
|
|
return float64(now), err
|
|
|
|
case float64:
|
|
|
|
return float64(now), err
|
|
|
|
case string:
|
|
|
|
return toDouble(now)
|
|
|
|
default:
|
|
|
|
return toDouble(fmt.Sprintf("%v", obj))
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToBoolean(obj interface{}) (val bool, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
switch now := obj.(type) {
|
|
|
|
case int64:
|
|
|
|
return now > 0, err
|
|
|
|
case float64:
|
|
|
|
return now > 0, err
|
|
|
|
case string:
|
|
|
|
return toBoolean(now)
|
|
|
|
default:
|
|
|
|
return toBoolean(fmt.Sprintf("%v", obj))
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToDatetime(obj interface{}) (val time.Time, err error) {
|
2017-12-12 01:05:40 +00:00
|
|
|
switch now := obj.(type) {
|
|
|
|
case time.Time:
|
2016-07-21 21:48:32 +00:00
|
|
|
val = now
|
2017-12-12 01:05:40 +00:00
|
|
|
case string:
|
|
|
|
val, err = time.Parse(time.RFC3339Nano, now)
|
|
|
|
default:
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a datetime, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToLatitude(obj interface{}) (val float64, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
str := fmt.Sprintf("%v", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
if !govalidator.IsLatitude(str) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a latitude value, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return govalidator.ToFloat(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToLongitude(obj interface{}) (val float64, err error) {
|
2016-10-20 13:14:10 +00:00
|
|
|
str := fmt.Sprintf("%v", obj)
|
|
|
|
if !govalidator.IsLongitude(str) {
|
2017-11-16 20:30:27 +00:00
|
|
|
err = fmt.Errorf("Expected a longitude value, but found '%v'", obj)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
|
|
|
return govalidator.ToFloat(str)
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:30:27 +00:00
|
|
|
func ConvertToRecord(obj interface{}, tb string) (val *sql.Thing, err error) {
|
2018-01-10 13:17:50 +00:00
|
|
|
switch now := obj.(type) {
|
|
|
|
case *sql.Thing:
|
2017-11-16 20:30:27 +00:00
|
|
|
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)
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
2018-01-10 13:17:50 +00:00
|
|
|
default:
|
2017-12-12 01:05:40 +00:00
|
|
|
switch tb {
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj)
|
|
|
|
case "":
|
2018-03-18 18:58:30 +00:00
|
|
|
err = fmt.Errorf("Expected a record of any type, but found '%v'", obj)
|
2017-12-12 01:05:40 +00:00
|
|
|
}
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|
2017-11-16 20:30:27 +00:00
|
|
|
return
|
2016-07-21 21:48:32 +00:00
|
|
|
}
|