github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_slice_str.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/gogf/gf/v2/internal/json"
    13  	"github.com/gogf/gf/v2/internal/reflection"
    14  	"github.com/gogf/gf/v2/util/gconv/internal/localinterface"
    15  )
    16  
    17  // SliceStr is alias of Strings.
    18  func SliceStr(any interface{}) []string {
    19  	return Strings(any)
    20  }
    21  
    22  // Strings converts `any` to []string.
    23  func Strings(any interface{}) []string {
    24  	if any == nil {
    25  		return nil
    26  	}
    27  	var (
    28  		array []string = nil
    29  	)
    30  	switch value := any.(type) {
    31  	case []int:
    32  		array = make([]string, len(value))
    33  		for k, v := range value {
    34  			array[k] = String(v)
    35  		}
    36  	case []int8:
    37  		array = make([]string, len(value))
    38  		for k, v := range value {
    39  			array[k] = String(v)
    40  		}
    41  	case []int16:
    42  		array = make([]string, len(value))
    43  		for k, v := range value {
    44  			array[k] = String(v)
    45  		}
    46  	case []int32:
    47  		array = make([]string, len(value))
    48  		for k, v := range value {
    49  			array[k] = String(v)
    50  		}
    51  	case []int64:
    52  		array = make([]string, len(value))
    53  		for k, v := range value {
    54  			array[k] = String(v)
    55  		}
    56  	case []uint:
    57  		array = make([]string, len(value))
    58  		for k, v := range value {
    59  			array[k] = String(v)
    60  		}
    61  	case []uint8:
    62  		if json.Valid(value) {
    63  			_ = json.UnmarshalUseNumber(value, &array)
    64  		}
    65  		if array == nil {
    66  			array = make([]string, len(value))
    67  			for k, v := range value {
    68  				array[k] = String(v)
    69  			}
    70  			return array
    71  		}
    72  	case string:
    73  		byteValue := []byte(value)
    74  		if json.Valid(byteValue) {
    75  			_ = json.UnmarshalUseNumber(byteValue, &array)
    76  		}
    77  		if array == nil {
    78  			if value == "" {
    79  				return []string{}
    80  			}
    81  			// Prevent strings from being null
    82  			// See Issue 3465 for details
    83  			return []string{value}
    84  		}
    85  	case []uint16:
    86  		array = make([]string, len(value))
    87  		for k, v := range value {
    88  			array[k] = String(v)
    89  		}
    90  	case []uint32:
    91  		array = make([]string, len(value))
    92  		for k, v := range value {
    93  			array[k] = String(v)
    94  		}
    95  	case []uint64:
    96  		array = make([]string, len(value))
    97  		for k, v := range value {
    98  			array[k] = String(v)
    99  		}
   100  	case []bool:
   101  		array = make([]string, len(value))
   102  		for k, v := range value {
   103  			array[k] = String(v)
   104  		}
   105  	case []float32:
   106  		array = make([]string, len(value))
   107  		for k, v := range value {
   108  			array[k] = String(v)
   109  		}
   110  	case []float64:
   111  		array = make([]string, len(value))
   112  		for k, v := range value {
   113  			array[k] = String(v)
   114  		}
   115  	case []interface{}:
   116  		array = make([]string, len(value))
   117  		for k, v := range value {
   118  			array[k] = String(v)
   119  		}
   120  	case []string:
   121  		array = value
   122  	case [][]byte:
   123  		array = make([]string, len(value))
   124  		for k, v := range value {
   125  			array[k] = String(v)
   126  		}
   127  	}
   128  	if array != nil {
   129  		return array
   130  	}
   131  	if v, ok := any.(localinterface.IStrings); ok {
   132  		return v.Strings()
   133  	}
   134  	if v, ok := any.(localinterface.IInterfaces); ok {
   135  		return Strings(v.Interfaces())
   136  	}
   137  	// JSON format string value converting.
   138  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   139  		return array
   140  	}
   141  	// Not a common type, it then uses reflection for conversion.
   142  	originValueAndKind := reflection.OriginValueAndKind(any)
   143  	switch originValueAndKind.OriginKind {
   144  	case reflect.Slice, reflect.Array:
   145  		var (
   146  			length = originValueAndKind.OriginValue.Len()
   147  			slice  = make([]string, length)
   148  		)
   149  		for i := 0; i < length; i++ {
   150  			slice[i] = String(originValueAndKind.OriginValue.Index(i).Interface())
   151  		}
   152  		return slice
   153  
   154  	default:
   155  		if originValueAndKind.OriginValue.IsZero() {
   156  			return []string{}
   157  		}
   158  		return []string{String(any)}
   159  	}
   160  }