mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
146 lines
3.2 KiB
Go
146 lines
3.2 KiB
Go
// 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
|
|
|
|
import (
|
|
"reflect"
|
|
)
|
|
|
|
// SliceStr is alias of Strings.
|
|
func SliceStr(any interface{}) []string {
|
|
return Strings(any)
|
|
}
|
|
|
|
// Strings converts `any` to []string.
|
|
func Strings(any interface{}) []string {
|
|
if any == nil {
|
|
return nil
|
|
}
|
|
var array []string
|
|
switch value := any.(type) {
|
|
case []int:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []int8:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []int16:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []int32:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []int64:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []uint:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []uint8:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []uint16:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []uint32:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []uint64:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []bool:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []float32:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []float64:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []interface{}:
|
|
array = make([]string, len(value))
|
|
for k, v := range value {
|
|
array[k] = String(v)
|
|
}
|
|
case []string:
|
|
array = value
|
|
case [][]byte:
|
|
array = make([]string, len(value))
|
|
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
|
|
}
|