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
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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-04-27 14:23:58 +00:00
|
|
|
func MustBe(t, obj interface{}) (val interface{}) {
|
|
|
|
switch t {
|
|
|
|
default:
|
|
|
|
return obj
|
|
|
|
case "array":
|
|
|
|
return MustBeArray(obj)
|
|
|
|
case "object":
|
|
|
|
return MustBeObject(obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustBeArray(obj interface{}) (val interface{}) {
|
|
|
|
if now, ok := obj.([]interface{}); ok {
|
|
|
|
val = now
|
|
|
|
} else {
|
|
|
|
val = make([]interface{}, 0)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func MustBeObject(obj interface{}) (val interface{}) {
|
|
|
|
if now, ok := obj.(map[string]interface{}); ok {
|
|
|
|
val = now
|
|
|
|
} else {
|
|
|
|
val = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
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 "array":
|
|
|
|
return ConvertToArray(obj)
|
|
|
|
case "object":
|
|
|
|
return ConvertToObject(obj)
|
|
|
|
case "string":
|
|
|
|
return ConvertToString(obj)
|
|
|
|
case "number":
|
|
|
|
return ConvertToNumber(obj)
|
|
|
|
case "boolean":
|
|
|
|
return ConvertToBoolean(obj)
|
|
|
|
case "datetime":
|
|
|
|
return ConvertToDatetime(obj)
|
|
|
|
case "record":
|
|
|
|
return ConvertToRecord(obj, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:48:32 +00:00
|
|
|
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 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-04-17 14:01:16 +00:00
|
|
|
case string:
|
|
|
|
if val = sql.ParseThing(now); val == nil {
|
|
|
|
err = fmt.Errorf("Expected a record of type '%s', but found '%v'", tb, obj)
|
|
|
|
}
|
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
|
|
|
}
|