mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
122 lines
2.6 KiB
Go
122 lines
2.6 KiB
Go
// Copyright 2019 gf Author(https://github.com/gogf/gf). 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(i interface{}) []string {
|
|
return Strings(i)
|
|
}
|
|
|
|
// Strings converts <i> to []string.
|
|
func Strings(i interface{}) []string {
|
|
if i == nil {
|
|
return nil
|
|
}
|
|
var array []string
|
|
switch value := i.(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 := i.(apiStrings); ok {
|
|
return v.Strings()
|
|
}
|
|
if v, ok := i.(apiInterfaces); ok {
|
|
return Strings(v.Interfaces())
|
|
}
|
|
// Use reflect feature at last.
|
|
rv := reflect.ValueOf(i)
|
|
switch rv.Kind() {
|
|
case reflect.Slice, reflect.Array:
|
|
length := rv.Len()
|
|
array = make([]string, length)
|
|
for n := 0; n < length; n++ {
|
|
array[n] = String(rv.Index(n).Interface())
|
|
}
|
|
default:
|
|
return []string{String(i)}
|
|
}
|
|
}
|
|
return array
|
|
}
|