2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-12-11 21:22:41 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
2021-09-15 21:17:45 +08:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
2020-10-30 22:26:26 +08:00
|
|
|
|
2019-12-11 21:22:41 +08:00
|
|
|
// SliceInt is alias of Ints.
|
2021-02-05 14:44:20 +08:00
|
|
|
func SliceInt(any interface{}) []int {
|
|
|
|
return Ints(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SliceInt32 is alias of Int32s.
|
2021-02-05 14:44:20 +08:00
|
|
|
func SliceInt32(any interface{}) []int32 {
|
|
|
|
return Int32s(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:17:45 +08:00
|
|
|
// SliceInt64 is alias of Int64s.
|
2021-02-05 14:44:20 +08:00
|
|
|
func SliceInt64(any interface{}) []int64 {
|
|
|
|
return Int64s(any)
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Ints converts `any` to []int.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Ints(any interface{}) []int {
|
|
|
|
if any == nil {
|
2019-12-11 21:22:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
var array []int
|
2021-02-05 14:44:20 +08:00
|
|
|
switch value := any.(type) {
|
2020-04-07 21:25:52 +08:00
|
|
|
case []string:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int(v)
|
|
|
|
}
|
|
|
|
case []int:
|
|
|
|
array = value
|
|
|
|
case []int8:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []int16:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []int32:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []int64:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []uint:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []uint8:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []uint16:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []uint32:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []uint64:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int(v)
|
|
|
|
}
|
|
|
|
case []bool:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
if v {
|
|
|
|
array[k] = 1
|
|
|
|
} else {
|
|
|
|
array[k] = 0
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
case []float32:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int(v)
|
|
|
|
}
|
|
|
|
case []float64:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int(v)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int(v)
|
|
|
|
}
|
|
|
|
case [][]byte:
|
|
|
|
array = make([]int, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int(v)
|
|
|
|
}
|
|
|
|
default:
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iInts); ok {
|
2020-04-07 21:25:52 +08:00
|
|
|
return v.Ints()
|
|
|
|
}
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iInterfaces); ok {
|
2020-04-07 21:25:52 +08:00
|
|
|
return Ints(v.Interfaces())
|
|
|
|
}
|
2021-09-15 21:17:45 +08:00
|
|
|
// JSON format string value converting.
|
|
|
|
var result []int
|
|
|
|
if checkJsonAndUnmarshalUseNumber(any, &result) {
|
|
|
|
return result
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
// Not a common type, it then uses reflection for conversion.
|
|
|
|
var reflectValue reflect.Value
|
|
|
|
if v, ok := value.(reflect.Value); ok {
|
|
|
|
reflectValue = v
|
|
|
|
} else {
|
|
|
|
reflectValue = reflect.ValueOf(value)
|
|
|
|
}
|
|
|
|
reflectKind := reflectValue.Kind()
|
|
|
|
for reflectKind == reflect.Ptr {
|
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
}
|
|
|
|
switch reflectKind {
|
2020-10-30 22:26:26 +08:00
|
|
|
case reflect.Slice, reflect.Array:
|
2021-02-05 14:44:20 +08:00
|
|
|
var (
|
|
|
|
length = reflectValue.Len()
|
|
|
|
slice = make([]int, length)
|
|
|
|
)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
slice[i] = Int(reflectValue.Index(i).Interface())
|
2020-10-30 22:26:26 +08:00
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
return slice
|
|
|
|
|
2020-10-30 22:26:26 +08:00
|
|
|
default:
|
2021-09-15 21:17:45 +08:00
|
|
|
if reflectValue.IsZero() {
|
|
|
|
return []int{}
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
return []int{Int(any)}
|
2020-10-30 22:26:26 +08:00
|
|
|
}
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
return array
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Int32s converts `any` to []int32.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Int32s(any interface{}) []int32 {
|
|
|
|
if any == nil {
|
2019-12-11 21:22:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
var array []int32
|
2021-02-05 14:44:20 +08:00
|
|
|
switch value := any.(type) {
|
2020-04-07 21:25:52 +08:00
|
|
|
case []string:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int32(v)
|
|
|
|
}
|
|
|
|
case []int:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []int8:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []int16:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []int32:
|
|
|
|
array = value
|
|
|
|
case []int64:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []uint:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []uint8:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []uint16:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []uint32:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []uint64:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int32(v)
|
|
|
|
}
|
|
|
|
case []bool:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
if v {
|
|
|
|
array[k] = 1
|
|
|
|
} else {
|
|
|
|
array[k] = 0
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
case []float32:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int32(v)
|
|
|
|
}
|
|
|
|
case []float64:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int32(v)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int32(v)
|
|
|
|
}
|
|
|
|
case [][]byte:
|
|
|
|
array = make([]int32, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int32(v)
|
|
|
|
}
|
|
|
|
default:
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iInts); ok {
|
2020-04-07 21:25:52 +08:00
|
|
|
return Int32s(v.Ints())
|
|
|
|
}
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iInterfaces); ok {
|
2020-04-07 21:25:52 +08:00
|
|
|
return Int32s(v.Interfaces())
|
|
|
|
}
|
2021-09-15 21:17:45 +08:00
|
|
|
// JSON format string value converting.
|
|
|
|
var result []int32
|
|
|
|
if checkJsonAndUnmarshalUseNumber(any, &result) {
|
|
|
|
return result
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
// Not a common type, it then uses reflection for conversion.
|
|
|
|
var reflectValue reflect.Value
|
|
|
|
if v, ok := value.(reflect.Value); ok {
|
|
|
|
reflectValue = v
|
|
|
|
} else {
|
|
|
|
reflectValue = reflect.ValueOf(value)
|
|
|
|
}
|
|
|
|
reflectKind := reflectValue.Kind()
|
|
|
|
for reflectKind == reflect.Ptr {
|
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
}
|
|
|
|
switch reflectKind {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
var (
|
|
|
|
length = reflectValue.Len()
|
|
|
|
slice = make([]int32, length)
|
|
|
|
)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
slice[i] = Int32(reflectValue.Index(i).Interface())
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
|
|
|
|
default:
|
2021-09-15 21:17:45 +08:00
|
|
|
if reflectValue.IsZero() {
|
|
|
|
return []int32{}
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
return []int32{Int32(any)}
|
|
|
|
}
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
return array
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Int64s converts `any` to []int64.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Int64s(any interface{}) []int64 {
|
|
|
|
if any == nil {
|
2019-12-11 21:22:41 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
var array []int64
|
2021-02-05 14:44:20 +08:00
|
|
|
switch value := any.(type) {
|
2020-04-07 21:25:52 +08:00
|
|
|
case []string:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int64(v)
|
|
|
|
}
|
|
|
|
case []int:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []int8:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []int16:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []int32:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []int64:
|
|
|
|
array = value
|
|
|
|
case []uint:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []uint8:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []uint16:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []uint32:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []uint64:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = int64(v)
|
|
|
|
}
|
|
|
|
case []bool:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
if v {
|
|
|
|
array[k] = 1
|
|
|
|
} else {
|
|
|
|
array[k] = 0
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
case []float32:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int64(v)
|
|
|
|
}
|
|
|
|
case []float64:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int64(v)
|
|
|
|
}
|
|
|
|
case []interface{}:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int64(v)
|
|
|
|
}
|
|
|
|
case [][]byte:
|
|
|
|
array = make([]int64, len(value))
|
|
|
|
for k, v := range value {
|
|
|
|
array[k] = Int64(v)
|
|
|
|
}
|
|
|
|
default:
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iInts); ok {
|
2020-04-07 21:25:52 +08:00
|
|
|
return Int64s(v.Ints())
|
|
|
|
}
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iInterfaces); ok {
|
2020-04-07 21:25:52 +08:00
|
|
|
return Int64s(v.Interfaces())
|
|
|
|
}
|
2021-09-15 21:17:45 +08:00
|
|
|
// JSON format string value converting.
|
|
|
|
var result []int64
|
|
|
|
if checkJsonAndUnmarshalUseNumber(any, &result) {
|
|
|
|
return result
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
// Not a common type, it then uses reflection for conversion.
|
|
|
|
var reflectValue reflect.Value
|
|
|
|
if v, ok := value.(reflect.Value); ok {
|
|
|
|
reflectValue = v
|
|
|
|
} else {
|
|
|
|
reflectValue = reflect.ValueOf(value)
|
|
|
|
}
|
|
|
|
reflectKind := reflectValue.Kind()
|
|
|
|
for reflectKind == reflect.Ptr {
|
|
|
|
reflectValue = reflectValue.Elem()
|
|
|
|
reflectKind = reflectValue.Kind()
|
|
|
|
}
|
|
|
|
switch reflectKind {
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
var (
|
|
|
|
length = reflectValue.Len()
|
|
|
|
slice = make([]int64, length)
|
|
|
|
)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
slice[i] = Int64(reflectValue.Index(i).Interface())
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
|
|
|
|
default:
|
2021-09-15 21:17:45 +08:00
|
|
|
if reflectValue.IsZero() {
|
|
|
|
return []int64{}
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
return []int64{Int64(any)}
|
|
|
|
}
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|
2020-04-07 21:25:52 +08:00
|
|
|
return array
|
2019-12-11 21:22:41 +08:00
|
|
|
}
|