2014-04-05 13:21:52 +08:00
|
|
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// Package cast provides easy and safe casting in Go.
|
2014-04-05 13:21:52 +08:00
|
|
|
package cast
|
|
|
|
|
2019-06-24 09:57:59 +08:00
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
2014-04-05 13:21:52 +08:00
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToBool casts an interface to a bool type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToBool(i interface{}) bool {
|
|
|
|
v, _ := ToBoolE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToTime casts an interface to a time.Time type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToTime(i interface{}) time.Time {
|
|
|
|
v, _ := ToTimeE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2019-06-24 09:57:59 +08:00
|
|
|
// ToURL casts an interface to a *url.URL type.
|
|
|
|
func ToURL(i interface{}) *url.URL {
|
|
|
|
v, _ := ToURLE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToDuration casts an interface to a time.Duration type.
|
2015-02-19 10:56:24 +08:00
|
|
|
func ToDuration(i interface{}) time.Duration {
|
|
|
|
v, _ := ToDurationE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToFloat64 casts an interface to a float64 type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToFloat64(i interface{}) float64 {
|
|
|
|
v, _ := ToFloat64E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-04 03:44:11 +08:00
|
|
|
// ToFloat32 casts an interface to a float32 type.
|
|
|
|
func ToFloat32(i interface{}) float32 {
|
|
|
|
v, _ := ToFloat32E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToInt64 casts an interface to an int64 type.
|
2016-01-29 02:41:34 +08:00
|
|
|
func ToInt64(i interface{}) int64 {
|
|
|
|
v, _ := ToInt64E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToInt32 casts an interface to an int32 type.
|
2017-02-10 15:00:46 +08:00
|
|
|
func ToInt32(i interface{}) int32 {
|
|
|
|
v, _ := ToInt32E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToInt16 casts an interface to an int16 type.
|
2017-02-10 15:00:46 +08:00
|
|
|
func ToInt16(i interface{}) int16 {
|
|
|
|
v, _ := ToInt16E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToInt8 casts an interface to an int8 type.
|
2017-02-10 15:00:46 +08:00
|
|
|
func ToInt8(i interface{}) int8 {
|
|
|
|
v, _ := ToInt8E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToInt casts an interface to an int type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToInt(i interface{}) int {
|
|
|
|
v, _ := ToIntE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-04 03:44:11 +08:00
|
|
|
// ToUint casts an interface to a uint type.
|
|
|
|
func ToUint(i interface{}) uint {
|
|
|
|
v, _ := ToUintE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToUint64 casts an interface to a uint64 type.
|
|
|
|
func ToUint64(i interface{}) uint64 {
|
|
|
|
v, _ := ToUint64E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToUint32 casts an interface to a uint32 type.
|
|
|
|
func ToUint32(i interface{}) uint32 {
|
|
|
|
v, _ := ToUint32E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToUint16 casts an interface to a uint16 type.
|
|
|
|
func ToUint16(i interface{}) uint16 {
|
|
|
|
v, _ := ToUint16E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToUint8 casts an interface to a uint8 type.
|
|
|
|
func ToUint8(i interface{}) uint8 {
|
|
|
|
v, _ := ToUint8E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToString casts an interface to a string type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToString(i interface{}) string {
|
|
|
|
v, _ := ToStringE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToStringMapString casts an interface to a map[string]string type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToStringMapString(i interface{}) map[string]string {
|
|
|
|
v, _ := ToStringMapStringE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToStringMapStringSlice casts an interface to a map[string][]string type.
|
2015-07-31 04:13:54 +08:00
|
|
|
func ToStringMapStringSlice(i interface{}) map[string][]string {
|
|
|
|
v, _ := ToStringMapStringSliceE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToStringMapBool casts an interface to a map[string]bool type.
|
2014-11-06 18:32:02 +08:00
|
|
|
func ToStringMapBool(i interface{}) map[string]bool {
|
|
|
|
v, _ := ToStringMapBoolE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-10-25 02:00:14 +08:00
|
|
|
// ToStringMapInt casts an interface to a map[string]int type.
|
|
|
|
func ToStringMapInt(i interface{}) map[string]int {
|
|
|
|
v, _ := ToStringMapIntE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStringMapInt64 casts an interface to a map[string]int64 type.
|
|
|
|
func ToStringMapInt64(i interface{}) map[string]int64 {
|
|
|
|
v, _ := ToStringMapInt64E(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToStringMap casts an interface to a map[string]interface{} type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToStringMap(i interface{}) map[string]interface{} {
|
|
|
|
v, _ := ToStringMapE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToSlice casts an interface to a []interface{} type.
|
2014-04-27 06:56:25 +08:00
|
|
|
func ToSlice(i interface{}) []interface{} {
|
|
|
|
v, _ := ToSliceE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToBoolSlice casts an interface to a []bool type.
|
2016-09-20 04:26:41 +08:00
|
|
|
func ToBoolSlice(i interface{}) []bool {
|
|
|
|
v, _ := ToBoolSliceE(i)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToStringSlice casts an interface to a []string type.
|
2014-04-05 13:21:52 +08:00
|
|
|
func ToStringSlice(i interface{}) []string {
|
|
|
|
v, _ := ToStringSliceE(i)
|
|
|
|
return v
|
|
|
|
}
|
2015-02-24 18:58:16 +08:00
|
|
|
|
2017-03-03 20:53:30 +08:00
|
|
|
// ToIntSlice casts an interface to a []int type.
|
2015-02-24 18:58:16 +08:00
|
|
|
func ToIntSlice(i interface{}) []int {
|
|
|
|
v, _ := ToIntSliceE(i)
|
|
|
|
return v
|
|
|
|
}
|
2017-04-13 16:50:28 +08:00
|
|
|
|
|
|
|
// ToDurationSlice casts an interface to a []time.Duration type.
|
|
|
|
func ToDurationSlice(i interface{}) []time.Duration {
|
|
|
|
v, _ := ToDurationSliceE(i)
|
|
|
|
return v
|
|
|
|
}
|