github.com/gogf/gf@v1.16.9/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/gogf/gf.
     6  
     7  package gconv
     8  
     9  import (
    10  	"reflect"
    11  )
    12  
    13  // SliceAny is alias of Interfaces.
    14  func SliceAny(any interface{}) []interface{} {
    15  	return Interfaces(any)
    16  }
    17  
    18  // Interfaces converts `any` to []interface{}.
    19  func Interfaces(any interface{}) []interface{} {
    20  	if any == nil {
    21  		return nil
    22  	}
    23  	if r, ok := any.([]interface{}); ok {
    24  		return r
    25  	} else if r, ok := any.(apiInterfaces); ok {
    26  		return r.Interfaces()
    27  	} else {
    28  		var array []interface{}
    29  		switch value := any.(type) {
    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  			array = make([]interface{}, len(value))
    67  			for k, v := range value {
    68  				array[k] = v
    69  			}
    70  		case []uint16:
    71  			array = make([]interface{}, len(value))
    72  			for k, v := range value {
    73  				array[k] = v
    74  			}
    75  		case []uint32:
    76  			for _, v := range value {
    77  				array = append(array, v)
    78  			}
    79  		case []uint64:
    80  			array = make([]interface{}, len(value))
    81  			for k, v := range value {
    82  				array[k] = v
    83  			}
    84  		case []bool:
    85  			array = make([]interface{}, len(value))
    86  			for k, v := range value {
    87  				array[k] = v
    88  			}
    89  		case []float32:
    90  			array = make([]interface{}, len(value))
    91  			for k, v := range value {
    92  				array[k] = v
    93  			}
    94  		case []float64:
    95  			array = make([]interface{}, len(value))
    96  			for k, v := range value {
    97  				array[k] = v
    98  			}
    99  		default:
   100  			// Finally we use reflection.
   101  			var (
   102  				reflectValue = reflect.ValueOf(any)
   103  				reflectKind  = reflectValue.Kind()
   104  			)
   105  			for reflectKind == reflect.Ptr {
   106  				reflectValue = reflectValue.Elem()
   107  				reflectKind = reflectValue.Kind()
   108  			}
   109  			switch reflectKind {
   110  			case reflect.Slice, reflect.Array:
   111  				array = make([]interface{}, reflectValue.Len())
   112  				for i := 0; i < reflectValue.Len(); i++ {
   113  					array[i] = reflectValue.Index(i).Interface()
   114  				}
   115  			// Deprecated.
   116  			//// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
   117  			//case reflect.Map:
   118  			//	array = make([]interface{}, 0)
   119  			//	for _, key := range reflectValue.MapKeys() {
   120  			//		array = append(array, key.Interface())
   121  			//		array = append(array, reflectValue.MapIndex(key).Interface())
   122  			//	}
   123  			//// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
   124  			//case reflect.Struct:
   125  			//	array = make([]interface{}, 0)
   126  			//	// Note that, it uses the gconv tag name instead of the attribute name if
   127  			//	// the gconv tag is fined in the struct attributes.
   128  			//	for k, v := range Map(reflectValue) {
   129  			//		array = append(array, k)
   130  			//		array = append(array, v)
   131  			//	}
   132  			default:
   133  				return []interface{}{any}
   134  			}
   135  		}
   136  		return array
   137  	}
   138  }