forked from mirror/cast
Fix gofmt and golint errors
This commit is contained in:
parent
55be5581bd
commit
2c719d95d6
19
cast.go
19
cast.go
|
@ -3,95 +3,114 @@
|
||||||
// Use of this source code is governed by an MIT-style
|
// Use of this source code is governed by an MIT-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package cast provides easy and safe casting in Go.
|
||||||
package cast
|
package cast
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
// ToBool casts an interface to a bool type.
|
||||||
func ToBool(i interface{}) bool {
|
func ToBool(i interface{}) bool {
|
||||||
v, _ := ToBoolE(i)
|
v, _ := ToBoolE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToTime casts an interface to a time.Time type.
|
||||||
func ToTime(i interface{}) time.Time {
|
func ToTime(i interface{}) time.Time {
|
||||||
v, _ := ToTimeE(i)
|
v, _ := ToTimeE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToDuration casts an interface to a time.Duration type.
|
||||||
func ToDuration(i interface{}) time.Duration {
|
func ToDuration(i interface{}) time.Duration {
|
||||||
v, _ := ToDurationE(i)
|
v, _ := ToDurationE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToFloat64 casts an interface to a float64 type.
|
||||||
func ToFloat64(i interface{}) float64 {
|
func ToFloat64(i interface{}) float64 {
|
||||||
v, _ := ToFloat64E(i)
|
v, _ := ToFloat64E(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInt64 casts an interface to an int64 type.
|
||||||
func ToInt64(i interface{}) int64 {
|
func ToInt64(i interface{}) int64 {
|
||||||
v, _ := ToInt64E(i)
|
v, _ := ToInt64E(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInt32 casts an interface to an int32 type.
|
||||||
func ToInt32(i interface{}) int32 {
|
func ToInt32(i interface{}) int32 {
|
||||||
v, _ := ToInt32E(i)
|
v, _ := ToInt32E(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInt16 casts an interface to an int16 type.
|
||||||
func ToInt16(i interface{}) int16 {
|
func ToInt16(i interface{}) int16 {
|
||||||
v, _ := ToInt16E(i)
|
v, _ := ToInt16E(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInt8 casts an interface to an int8 type.
|
||||||
func ToInt8(i interface{}) int8 {
|
func ToInt8(i interface{}) int8 {
|
||||||
v, _ := ToInt8E(i)
|
v, _ := ToInt8E(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToInt casts an interface to an int type.
|
||||||
func ToInt(i interface{}) int {
|
func ToInt(i interface{}) int {
|
||||||
v, _ := ToIntE(i)
|
v, _ := ToIntE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToString casts an interface to a string type.
|
||||||
func ToString(i interface{}) string {
|
func ToString(i interface{}) string {
|
||||||
v, _ := ToStringE(i)
|
v, _ := ToStringE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToStringMapString casts an interface to a map[string]string type.
|
||||||
func ToStringMapString(i interface{}) map[string]string {
|
func ToStringMapString(i interface{}) map[string]string {
|
||||||
v, _ := ToStringMapStringE(i)
|
v, _ := ToStringMapStringE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToStringMapStringSlice casts an interface to a map[string][]string type.
|
||||||
func ToStringMapStringSlice(i interface{}) map[string][]string {
|
func ToStringMapStringSlice(i interface{}) map[string][]string {
|
||||||
v, _ := ToStringMapStringSliceE(i)
|
v, _ := ToStringMapStringSliceE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToStringMapBool casts an interface to a map[string]bool type.
|
||||||
func ToStringMapBool(i interface{}) map[string]bool {
|
func ToStringMapBool(i interface{}) map[string]bool {
|
||||||
v, _ := ToStringMapBoolE(i)
|
v, _ := ToStringMapBoolE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToStringMap casts an interface to a map[string]interface{} type.
|
||||||
func ToStringMap(i interface{}) map[string]interface{} {
|
func ToStringMap(i interface{}) map[string]interface{} {
|
||||||
v, _ := ToStringMapE(i)
|
v, _ := ToStringMapE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToSlice casts an interface to a []interface{} type.
|
||||||
func ToSlice(i interface{}) []interface{} {
|
func ToSlice(i interface{}) []interface{} {
|
||||||
v, _ := ToSliceE(i)
|
v, _ := ToSliceE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToBoolSlice casts an interface to a []bool type.
|
||||||
func ToBoolSlice(i interface{}) []bool {
|
func ToBoolSlice(i interface{}) []bool {
|
||||||
v, _ := ToBoolSliceE(i)
|
v, _ := ToBoolSliceE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToStringSlice casts an interface to a []string type.
|
||||||
func ToStringSlice(i interface{}) []string {
|
func ToStringSlice(i interface{}) []string {
|
||||||
v, _ := ToStringSliceE(i)
|
v, _ := ToStringSliceE(i)
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToIntSlice casts an interface to a []int type.
|
||||||
func ToIntSlice(i interface{}) []int {
|
func ToIntSlice(i interface{}) []int {
|
||||||
v, _ := ToIntSliceE(i)
|
v, _ := ToIntSliceE(i)
|
||||||
return v
|
return v
|
||||||
|
|
16
cast_test.go
16
cast_test.go
|
@ -132,17 +132,17 @@ func TestMaps(t *testing.T) {
|
||||||
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
|
||||||
|
|
||||||
// ToStringMapStringSlice inputs/outputs
|
// ToStringMapStringSlice inputs/outputs
|
||||||
var stringMapStringSlice = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
var stringMapStringSlice = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||||
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
|
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||||
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": []string{"value", "1"}, "key 2": []string{"value", "2"}, "key 3": []string{"value", "3"}}
|
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": {"value", "1"}, "key 2": {"value", "2"}, "key 3": {"value", "3"}}
|
||||||
var interfaceMapStringSlice = map[interface{}][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
var interfaceMapStringSlice = map[interface{}][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||||
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
|
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||||
|
|
||||||
var stringMapStringSliceMultiple = map[string][]string{"key 1": []string{"value 1", "value 2", "value 3"}, "key 2": []string{"value 1", "value 2", "value 3"}, "key 3": []string{"value 1", "value 2", "value 3"}}
|
var stringMapStringSliceMultiple = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
|
||||||
var stringMapStringSliceSingle = map[string][]string{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}, "key 3": []string{"value 3"}}
|
var stringMapStringSliceSingle = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}, "key 3": {"value 3"}}
|
||||||
|
|
||||||
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
||||||
var stringMapInterfaceResult1 = map[string][]string{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
|
var stringMapInterfaceResult1 = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}}
|
||||||
|
|
||||||
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
|
assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
|
||||||
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
|
assert.Equal(t, ToStringMapBool(stringMapBool), map[string]bool{"v1": true, "v2": false})
|
||||||
|
|
Loading…
Reference in New Issue