This commit is contained in:
huangqian 2021-11-16 07:36:23 +08:00
commit efcba5ecae
7 changed files with 470 additions and 493 deletions

View File

@ -1,7 +0,0 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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

View File

@ -8,6 +8,8 @@ package gconv
import (
"reflect"
"github.com/gogf/gf/v2/internal/utils"
)
// SliceAny is alias of Interfaces.
@ -22,100 +24,108 @@ func Interfaces(any interface{}) []interface{} {
}
if r, ok := any.([]interface{}); ok {
return r
} else if r, ok := any.(iInterfaces); ok {
return r.Interfaces()
} else {
var array []interface{}
switch value := any.(type) {
case []string:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int8:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int16:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int32:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int64:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint8:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint16:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint32:
for _, v := range value {
array = append(array, v)
}
case []uint64:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []bool:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []float32:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []float64:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
default:
// Finally we use reflection.
var (
reflectValue = reflect.ValueOf(any)
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Slice, reflect.Array:
array = make([]interface{}, reflectValue.Len())
for i := 0; i < reflectValue.Len(); i++ {
array[i] = reflectValue.Index(i).Interface()
}
default:
return []interface{}{any}
}
}
var (
array []interface{} = nil
)
switch value := any.(type) {
case []string:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int8:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int16:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int32:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []int64:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint8:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint16:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []uint32:
for _, v := range value {
array = append(array, v)
}
case []uint64:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []bool:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []float32:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
case []float64:
array = make([]interface{}, len(value))
for k, v := range value {
array[k] = v
}
}
if array != nil {
return array
}
if v, ok := any.(iInterfaces); ok {
return v.Interfaces()
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]interface{}, length)
)
for i := 0; i < length; i++ {
slice[i] = originValueAndKind.OriginValue.Index(i).Interface()
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []interface{}{}
}
return []interface{}{any}
}
}

View File

@ -6,7 +6,11 @@
package gconv
import "reflect"
import (
"reflect"
"github.com/gogf/gf/v2/internal/utils"
)
// SliceFloat is alias of Floats.
func SliceFloat(any interface{}) []float64 {
@ -33,7 +37,9 @@ func Float32s(any interface{}) []float32 {
if any == nil {
return nil
}
var array []float32
var (
array []float32 = nil
)
switch value := any.(type) {
case string:
if value == "" {
@ -111,49 +117,39 @@ func Float32s(any interface{}) []float32 {
for k, v := range value {
array[k] = Float32(v)
}
default:
if v, ok := any.(iFloats); ok {
return Float32s(v.Floats())
}
if v, ok := any.(iInterfaces); ok {
return Float32s(v.Interfaces())
}
// JSON format string value converting.
var result []float32
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]float32, length)
)
for i := 0; i < length; i++ {
slice[i] = Float32(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []float32{}
}
return []float32{Float32(any)}
}
}
return array
if array != nil {
return array
}
if v, ok := any.(iFloats); ok {
return Float32s(v.Floats())
}
if v, ok := any.(iInterfaces); ok {
return Float32s(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]float32, length)
)
for i := 0; i < length; i++ {
slice[i] = Float32(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []float32{}
}
return []float32{Float32(any)}
}
}
// Float64s converts `any` to []float64.
@ -161,7 +157,9 @@ func Float64s(any interface{}) []float64 {
if any == nil {
return nil
}
var array []float64
var (
array []float64 = nil
)
switch value := any.(type) {
case string:
if value == "" {
@ -239,48 +237,37 @@ func Float64s(any interface{}) []float64 {
for k, v := range value {
array[k] = Float64(v)
}
default:
if v, ok := any.(iFloats); ok {
return v.Floats()
}
if v, ok := any.(iInterfaces); ok {
return Floats(v.Interfaces())
}
// JSON format string value converting.
var result []float64
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]float64, length)
)
for i := 0; i < length; i++ {
slice[i] = Float64(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []float64{}
}
return []float64{Float64(any)}
}
}
return array
if array != nil {
return array
}
if v, ok := any.(iFloats); ok {
return v.Floats()
}
if v, ok := any.(iInterfaces); ok {
return Floats(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]float64, length)
)
for i := 0; i < length; i++ {
slice[i] = Float64(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []float64{}
}
return []float64{Float64(any)}
}
}

View File

@ -8,6 +8,8 @@ package gconv
import (
"reflect"
"github.com/gogf/gf/v2/internal/utils"
)
// SliceInt is alias of Ints.
@ -30,7 +32,9 @@ func Ints(any interface{}) []int {
if any == nil {
return nil
}
var array []int
var (
array []int = nil
)
switch value := any.(type) {
case []string:
array = make([]int, len(value))
@ -113,49 +117,39 @@ func Ints(any interface{}) []int {
for k, v := range value {
array[k] = Int(v)
}
default:
if v, ok := any.(iInts); ok {
return v.Ints()
}
if v, ok := any.(iInterfaces); ok {
return Ints(v.Interfaces())
}
// JSON format string value converting.
var result []int
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]int, length)
)
for i := 0; i < length; i++ {
slice[i] = Int(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []int{}
}
return []int{Int(any)}
}
}
return array
if array != nil {
return array
}
if v, ok := any.(iInts); ok {
return v.Ints()
}
if v, ok := any.(iInterfaces); ok {
return Ints(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]int, length)
)
for i := 0; i < length; i++ {
slice[i] = Int(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []int{}
}
return []int{Int(any)}
}
}
// Int32s converts `any` to []int32.
@ -163,7 +157,9 @@ func Int32s(any interface{}) []int32 {
if any == nil {
return nil
}
var array []int32
var (
array []int32 = nil
)
switch value := any.(type) {
case []string:
array = make([]int32, len(value))
@ -246,49 +242,39 @@ func Int32s(any interface{}) []int32 {
for k, v := range value {
array[k] = Int32(v)
}
default:
if v, ok := any.(iInts); ok {
return Int32s(v.Ints())
}
if v, ok := any.(iInterfaces); ok {
return Int32s(v.Interfaces())
}
// JSON format string value converting.
var result []int32
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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:
if reflectValue.IsZero() {
return []int32{}
}
return []int32{Int32(any)}
}
}
return array
if array != nil {
return array
}
if v, ok := any.(iInts); ok {
return Int32s(v.Ints())
}
if v, ok := any.(iInterfaces); ok {
return Int32s(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]int32, length)
)
for i := 0; i < length; i++ {
slice[i] = Int32(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []int32{}
}
return []int32{Int32(any)}
}
}
// Int64s converts `any` to []int64.
@ -296,7 +282,9 @@ func Int64s(any interface{}) []int64 {
if any == nil {
return nil
}
var array []int64
var (
array []int64 = nil
)
switch value := any.(type) {
case []string:
array = make([]int64, len(value))
@ -379,47 +367,37 @@ func Int64s(any interface{}) []int64 {
for k, v := range value {
array[k] = Int64(v)
}
default:
if v, ok := any.(iInts); ok {
return Int64s(v.Ints())
}
if v, ok := any.(iInterfaces); ok {
return Int64s(v.Interfaces())
}
// JSON format string value converting.
var result []int64
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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:
if reflectValue.IsZero() {
return []int64{}
}
return []int64{Int64(any)}
}
}
return array
if array != nil {
return array
}
if v, ok := any.(iInts); ok {
return Int64s(v.Ints())
}
if v, ok := any.(iInterfaces); ok {
return Int64s(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]int64, length)
)
for i := 0; i < length; i++ {
slice[i] = Int64(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []int64{}
}
return []int64{Int64(any)}
}
}

View File

@ -8,6 +8,8 @@ package gconv
import (
"reflect"
"github.com/gogf/gf/v2/internal/utils"
)
// SliceStr is alias of Strings.
@ -20,7 +22,9 @@ func Strings(any interface{}) []string {
if any == nil {
return nil
}
var array []string
var (
array []string = nil
)
switch value := any.(type) {
case []int:
array = make([]string, len(value))
@ -99,47 +103,37 @@ func Strings(any interface{}) []string {
for k, v := range value {
array[k] = String(v)
}
default:
if v, ok := any.(iStrings); ok {
return v.Strings()
}
if v, ok := any.(iInterfaces); ok {
return Strings(v.Interfaces())
}
// JSON format string value converting.
var result []string
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]string, length)
)
for i := 0; i < length; i++ {
slice[i] = String(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []string{}
}
return []string{String(any)}
}
}
return array
if array != nil {
return array
}
if v, ok := any.(iStrings); ok {
return v.Strings()
}
if v, ok := any.(iInterfaces); ok {
return Strings(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]string, length)
)
for i := 0; i < length; i++ {
slice[i] = String(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []string{}
}
return []string{String(any)}
}
}

View File

@ -6,7 +6,12 @@
package gconv
import "reflect"
import (
"reflect"
"strings"
"github.com/gogf/gf/v2/internal/utils"
)
// SliceUint is alias of Uints.
func SliceUint(any interface{}) []uint {
@ -29,13 +34,19 @@ func Uints(any interface{}) []uint {
return nil
}
var array []uint
var (
array []uint = nil
)
switch value := any.(type) {
case string:
value = strings.TrimSpace(value)
if value == "" {
return []uint{}
}
return []uint{Uint(value)}
if utils.IsNumeric(value) {
return []uint{Uint(value)}
}
case []string:
array = make([]uint, len(value))
for k, v := range value {
@ -112,49 +123,42 @@ func Uints(any interface{}) []uint {
for k, v := range value {
array[k] = Uint(v)
}
default:
if v, ok := any.(iUints); ok {
return v.Uints()
}
if v, ok := any.(iInterfaces); ok {
return Uints(v.Interfaces())
}
// JSON format string value converting.
var result []uint
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]uint, length)
)
for i := 0; i < length; i++ {
slice[i] = Uint(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []uint{}
}
return []uint{Uint(any)}
}
}
return array
if array != nil {
return array
}
// Default handler.
if v, ok := any.(iUints); ok {
return v.Uints()
}
if v, ok := any.(iInterfaces); ok {
return Uints(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]uint, length)
)
for i := 0; i < length; i++ {
slice[i] = Uint(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []uint{}
}
return []uint{Uint(any)}
}
}
// Uint32s converts `any` to []uint32.
@ -162,13 +166,18 @@ func Uint32s(any interface{}) []uint32 {
if any == nil {
return nil
}
var array []uint32
var (
array []uint32 = nil
)
switch value := any.(type) {
case string:
value = strings.TrimSpace(value)
if value == "" {
return []uint32{}
}
return []uint32{Uint32(value)}
if utils.IsNumeric(value) {
return []uint32{Uint32(value)}
}
case []string:
array = make([]uint32, len(value))
for k, v := range value {
@ -245,49 +254,41 @@ func Uint32s(any interface{}) []uint32 {
for k, v := range value {
array[k] = Uint32(v)
}
default:
if v, ok := any.(iUints); ok {
return Uint32s(v.Uints())
}
if v, ok := any.(iInterfaces); ok {
return Uint32s(v.Interfaces())
}
// JSON format string value converting.
var result []uint32
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]uint32, length)
)
for i := 0; i < length; i++ {
slice[i] = Uint32(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []uint32{}
}
return []uint32{Uint32(any)}
}
}
return array
if array != nil {
return array
}
// Default handler.
if v, ok := any.(iUints); ok {
return Uint32s(v.Uints())
}
if v, ok := any.(iInterfaces); ok {
return Uint32s(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]uint32, length)
)
for i := 0; i < length; i++ {
slice[i] = Uint32(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []uint32{}
}
return []uint32{Uint32(any)}
}
}
// Uint64s converts `any` to []uint64.
@ -295,13 +296,19 @@ func Uint64s(any interface{}) []uint64 {
if any == nil {
return nil
}
var array []uint64
var (
array []uint64 = nil
)
switch value := any.(type) {
case string:
value = strings.TrimSpace(value)
if value == "" {
return []uint64{}
}
return []uint64{Uint64(value)}
if utils.IsNumeric(value) {
return []uint64{Uint64(value)}
}
case []string:
array = make([]uint64, len(value))
for k, v := range value {
@ -378,47 +385,38 @@ func Uint64s(any interface{}) []uint64 {
for k, v := range value {
array[k] = Uint64(v)
}
default:
if v, ok := any.(iUints); ok {
return Uint64s(v.Uints())
}
if v, ok := any.(iInterfaces); ok {
return Uint64s(v.Interfaces())
}
// JSON format string value converting.
var result []uint64
if checkJsonAndUnmarshalUseNumber(any, &result) {
return result
}
// 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([]uint64, length)
)
for i := 0; i < length; i++ {
slice[i] = Uint64(reflectValue.Index(i).Interface())
}
return slice
default:
if reflectValue.IsZero() {
return []uint64{}
}
return []uint64{Uint64(any)}
}
}
return array
if array != nil {
return array
}
// Default handler.
if v, ok := any.(iUints); ok {
return Uint64s(v.Uints())
}
if v, ok := any.(iInterfaces); ok {
return Uint64s(v.Interfaces())
}
// JSON format string value converting.
if checkJsonAndUnmarshalUseNumber(any, &array) {
return array
}
// Not a common type, it then uses reflection for conversion.
originValueAndKind := utils.OriginValueAndKind(any)
switch originValueAndKind.OriginKind {
case reflect.Slice, reflect.Array:
var (
length = originValueAndKind.OriginValue.Len()
slice = make([]uint64, length)
)
for i := 0; i < length; i++ {
slice[i] = Uint64(originValueAndKind.OriginValue.Index(i).Interface())
}
return slice
default:
if originValueAndKind.OriginValue.IsZero() {
return []uint64{}
}
return []uint64{Uint64(any)}
}
}

View File

@ -7,9 +7,10 @@
package gconv_test
import (
"github.com/gogf/gf/v2/container/gvar"
"testing"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
@ -35,6 +36,22 @@ func Test_Slice(t *testing.T) {
})
}
func Test_Slice_Ints(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Ints(nil), nil)
t.AssertEQ(gconv.Ints("[26, 27]"), []int{26, 27})
t.AssertEQ(gconv.Ints(" [26, 27] "), []int{26, 27})
})
}
func Test_Slice_Uint64s(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Uint64s(nil), nil)
t.AssertEQ(gconv.Uint64s("[26, 27]"), []uint64{26, 27})
t.AssertEQ(gconv.Uint64s(" [26, 27] "), []uint64{26, 27})
})
}
func Test_Slice_Empty(t *testing.T) {
// Int.
gtest.C(t, func(t *gtest.T) {