github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_slice_any.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/wangyougui/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/wangyougui/gf/v2/internal/json"
    13  	"github.com/wangyougui/gf/v2/internal/reflection"
    14  )
    15  
    16  // SliceAny is alias of Interfaces.
    17  func SliceAny(any interface{}) []interface{} {
    18  	return Interfaces(any)
    19  }
    20  
    21  // Interfaces converts `any` to []interface{}.
    22  func Interfaces(any interface{}) []interface{} {
    23  	if any == nil {
    24  		return nil
    25  	}
    26  	var array []interface{}
    27  	switch value := any.(type) {
    28  	case []interface{}:
    29  		array = value
    30  	case []string:
    31  		array = make([]interface{}, len(value))
    32  		for k, v := range value {
    33  			array[k] = v
    34  		}
    35  	case []int:
    36  		array = make([]interface{}, len(value))
    37  		for k, v := range value {
    38  			array[k] = v
    39  		}
    40  	case []int8:
    41  		array = make([]interface{}, len(value))
    42  		for k, v := range value {
    43  			array[k] = v
    44  		}
    45  	case []int16:
    46  		array = make([]interface{}, len(value))
    47  		for k, v := range value {
    48  			array[k] = v
    49  		}
    50  	case []int32:
    51  		array = make([]interface{}, len(value))
    52  		for k, v := range value {
    53  			array[k] = v
    54  		}
    55  	case []int64:
    56  		array = make([]interface{}, len(value))
    57  		for k, v := range value {
    58  			array[k] = v
    59  		}
    60  	case []uint:
    61  		array = make([]interface{}, len(value))
    62  		for k, v := range value {
    63  			array[k] = v
    64  		}
    65  	case []uint8:
    66  		if json.Valid(value) {
    67  			_ = json.UnmarshalUseNumber(value, &array)
    68  		} else {
    69  			array = make([]interface{}, len(value))
    70  			for k, v := range value {
    71  				array[k] = v
    72  			}
    73  		}
    74  	case []uint16:
    75  		array = make([]interface{}, len(value))
    76  		for k, v := range value {
    77  			array[k] = v
    78  		}
    79  	case []uint32:
    80  		for _, v := range value {
    81  			array = append(array, v)
    82  		}
    83  	case []uint64:
    84  		array = make([]interface{}, len(value))
    85  		for k, v := range value {
    86  			array[k] = v
    87  		}
    88  	case []bool:
    89  		array = make([]interface{}, len(value))
    90  		for k, v := range value {
    91  			array[k] = v
    92  		}
    93  	case []float32:
    94  		array = make([]interface{}, len(value))
    95  		for k, v := range value {
    96  			array[k] = v
    97  		}
    98  	case []float64:
    99  		array = make([]interface{}, len(value))
   100  		for k, v := range value {
   101  			array[k] = v
   102  		}
   103  	}
   104  	if array != nil {
   105  		return array
   106  	}
   107  	if v, ok := any.(iInterfaces); ok {
   108  		return v.Interfaces()
   109  	}
   110  	// JSON format string value converting.
   111  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   112  		return array
   113  	}
   114  	// Not a common type, it then uses reflection for conversion.
   115  	originValueAndKind := reflection.OriginValueAndKind(any)
   116  	switch originValueAndKind.OriginKind {
   117  	case reflect.Slice, reflect.Array:
   118  		var (
   119  			length = originValueAndKind.OriginValue.Len()
   120  			slice  = make([]interface{}, length)
   121  		)
   122  		for i := 0; i < length; i++ {
   123  			slice[i] = originValueAndKind.OriginValue.Index(i).Interface()
   124  		}
   125  		return slice
   126  
   127  	default:
   128  		return []interface{}{any}
   129  	}
   130  }