gconv_slice_any.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. package gconv
  7. import (
  8. "reflect"
  9. )
  10. // SliceAny is alias of Interfaces.
  11. func SliceAny(any interface{}) []interface{} {
  12. return Interfaces(any)
  13. }
  14. // Interfaces converts `any` to []interface{}.
  15. func Interfaces(any interface{}) []interface{} {
  16. if any == nil {
  17. return nil
  18. }
  19. if r, ok := any.([]interface{}); ok {
  20. return r
  21. } else if r, ok := any.(apiInterfaces); ok {
  22. return r.Interfaces()
  23. } else {
  24. var array []interface{}
  25. switch value := any.(type) {
  26. case []string:
  27. array = make([]interface{}, len(value))
  28. for k, v := range value {
  29. array[k] = v
  30. }
  31. case []int:
  32. array = make([]interface{}, len(value))
  33. for k, v := range value {
  34. array[k] = v
  35. }
  36. case []int8:
  37. array = make([]interface{}, len(value))
  38. for k, v := range value {
  39. array[k] = v
  40. }
  41. case []int16:
  42. array = make([]interface{}, len(value))
  43. for k, v := range value {
  44. array[k] = v
  45. }
  46. case []int32:
  47. array = make([]interface{}, len(value))
  48. for k, v := range value {
  49. array[k] = v
  50. }
  51. case []int64:
  52. array = make([]interface{}, len(value))
  53. for k, v := range value {
  54. array[k] = v
  55. }
  56. case []uint:
  57. array = make([]interface{}, len(value))
  58. for k, v := range value {
  59. array[k] = v
  60. }
  61. case []uint8:
  62. array = make([]interface{}, len(value))
  63. for k, v := range value {
  64. array[k] = v
  65. }
  66. case []uint16:
  67. array = make([]interface{}, len(value))
  68. for k, v := range value {
  69. array[k] = v
  70. }
  71. case []uint32:
  72. for _, v := range value {
  73. array = append(array, v)
  74. }
  75. case []uint64:
  76. array = make([]interface{}, len(value))
  77. for k, v := range value {
  78. array[k] = v
  79. }
  80. case []bool:
  81. array = make([]interface{}, len(value))
  82. for k, v := range value {
  83. array[k] = v
  84. }
  85. case []float32:
  86. array = make([]interface{}, len(value))
  87. for k, v := range value {
  88. array[k] = v
  89. }
  90. case []float64:
  91. array = make([]interface{}, len(value))
  92. for k, v := range value {
  93. array[k] = v
  94. }
  95. default:
  96. // Finally we use reflection.
  97. var (
  98. reflectValue = reflect.ValueOf(any)
  99. reflectKind = reflectValue.Kind()
  100. )
  101. for reflectKind == reflect.Ptr {
  102. reflectValue = reflectValue.Elem()
  103. reflectKind = reflectValue.Kind()
  104. }
  105. switch reflectKind {
  106. case reflect.Slice, reflect.Array:
  107. array = make([]interface{}, reflectValue.Len())
  108. for i := 0; i < reflectValue.Len(); i++ {
  109. array[i] = reflectValue.Index(i).Interface()
  110. }
  111. // Deprecated.
  112. //// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
  113. //case reflect.Map:
  114. // array = make([]interface{}, 0)
  115. // for _, key := range reflectValue.MapKeys() {
  116. // array = append(array, key.Interface())
  117. // array = append(array, reflectValue.MapIndex(key).Interface())
  118. // }
  119. //// Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]
  120. //case reflect.Struct:
  121. // array = make([]interface{}, 0)
  122. // // Note that, it uses the gconv tag name instead of the attribute name if
  123. // // the gconv tag is fined in the struct attributes.
  124. // for k, v := range Map(reflectValue) {
  125. // array = append(array, k)
  126. // array = append(array, v)
  127. // }
  128. default:
  129. return []interface{}{any}
  130. }
  131. }
  132. return array
  133. }
  134. }