array.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. package dara
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sort"
  6. "strings"
  7. )
  8. // ArrContains checks if an element is in the array
  9. func ArrContains(arr interface{}, value interface{}) bool {
  10. arrValue := reflect.ValueOf(arr)
  11. valueValue := reflect.ValueOf(value)
  12. // Ensure arr is a slice
  13. if arrValue.Kind() != reflect.Slice {
  14. return false
  15. }
  16. for i := 0; i < arrValue.Len(); i++ {
  17. elem := arrValue.Index(i)
  18. // Ensure the array element is a pointer
  19. if elem.Kind() == reflect.Ptr {
  20. if valueValue.Kind() == reflect.Ptr && elem.Pointer() == valueValue.Pointer() {
  21. return true
  22. }
  23. if elem.Elem().Interface() == valueValue.Interface() {
  24. return true
  25. }
  26. } else if elem.Kind() == reflect.Interface {
  27. elem = elem.Elem()
  28. if valueValue.Kind() == reflect.Ptr && elem.Interface() == valueValue.Pointer() {
  29. return true
  30. }
  31. if elem.Interface() == valueValue.Interface() {
  32. return true // Return the index if found
  33. }
  34. }
  35. }
  36. return false
  37. }
  38. // ArrIndex returns the index of the element in the array
  39. func ArrIndex(arr interface{}, value interface{}) int {
  40. arrValue := reflect.ValueOf(arr)
  41. valueValue := reflect.ValueOf(value)
  42. // Ensure arr is a slice
  43. if arrValue.Kind() != reflect.Slice {
  44. return -1
  45. }
  46. for i := 0; i < arrValue.Len(); i++ {
  47. elem := arrValue.Index(i)
  48. // Ensure the array element is a pointer
  49. if elem.Kind() == reflect.Ptr {
  50. // Dereference the pointer to get the underlying value
  51. if valueValue.Kind() == reflect.Ptr && elem.Pointer() == valueValue.Pointer() {
  52. return i
  53. }
  54. if elem.Elem().Interface() == valueValue.Interface() {
  55. return i // Return the index if found
  56. }
  57. } else if elem.Kind() == reflect.Interface {
  58. elem = elem.Elem()
  59. if valueValue.Kind() == reflect.Ptr && elem.Interface() == valueValue.Pointer() {
  60. return i
  61. }
  62. if elem.Interface() == valueValue.Interface() {
  63. return i // Return the index if found
  64. }
  65. }
  66. }
  67. return -1 // Return -1 if not found
  68. }
  69. func handlePointer(elem reflect.Value) string {
  70. if elem.IsNil() {
  71. return "" // Skip nil pointers
  72. }
  73. // Dereference the pointer
  74. elem = elem.Elem()
  75. return handleValue(elem)
  76. }
  77. func handleValue(elem reflect.Value) string {
  78. switch elem.Kind() {
  79. case reflect.String:
  80. return elem.String()
  81. case reflect.Int:
  82. return fmt.Sprintf("%d", elem.Interface())
  83. case reflect.Float64:
  84. return fmt.Sprintf("%f", elem.Interface())
  85. case reflect.Bool:
  86. return fmt.Sprintf("%t", elem.Interface())
  87. default:
  88. return "" // Skip unsupported types
  89. }
  90. }
  91. func ArrJoin(arr interface{}, sep string) string {
  92. var strSlice []string
  93. var str string
  94. arrValue := reflect.ValueOf(arr)
  95. // Ensure arr is a slice
  96. if arrValue.Kind() != reflect.Slice {
  97. return ""
  98. }
  99. for i := 0; i < arrValue.Len(); i++ {
  100. elem := arrValue.Index(i)
  101. if elem.Kind() == reflect.Ptr {
  102. str = handlePointer(elem)
  103. } else if elem.Kind() == reflect.Interface {
  104. str = handleValue(elem.Elem())
  105. } else {
  106. str = handleValue(elem)
  107. }
  108. if str != "" {
  109. strSlice = append(strSlice, str)
  110. }
  111. }
  112. return strings.Join(strSlice, sep)
  113. }
  114. // ArrShift removes the first element from the array
  115. func ArrShift(arr interface{}) interface{} {
  116. arrValue := reflect.ValueOf(arr)
  117. // Ensure arr is a pointer to a slice
  118. if arrValue.Kind() != reflect.Ptr || arrValue.Elem().Kind() != reflect.Slice {
  119. return nil
  120. }
  121. // Get the slice from the pointer
  122. sliceValue := arrValue.Elem()
  123. // Ensure the slice is not empty
  124. if sliceValue.Len() == 0 {
  125. return nil
  126. }
  127. // Get the first element
  128. firstElem := sliceValue.Index(0)
  129. // Create a new slice with one less element
  130. newArrValue := reflect.MakeSlice(sliceValue.Type(), sliceValue.Len()-1, sliceValue.Cap())
  131. // Copy the elements after the first one to the new slice
  132. reflect.Copy(newArrValue, sliceValue.Slice(1, sliceValue.Len()))
  133. // Set the original slice to the new slice
  134. sliceValue.Set(newArrValue)
  135. // Return the removed first element
  136. return firstElem.Interface()
  137. }
  138. // ArrPop removes the last element from the array
  139. func ArrPop(arr interface{}) interface{} {
  140. arrValue := reflect.ValueOf(arr)
  141. // Ensure arr is a pointer to a slice
  142. if arrValue.Kind() != reflect.Ptr || arrValue.Elem().Kind() != reflect.Slice {
  143. return nil
  144. }
  145. // Get the slice from the pointer
  146. sliceValue := arrValue.Elem()
  147. // Ensure the slice is not empty
  148. if sliceValue.Len() == 0 {
  149. return nil
  150. }
  151. // Get the last element
  152. lastIndex := sliceValue.Len() - 1
  153. lastElem := sliceValue.Index(lastIndex)
  154. // Create a new slice with one less element
  155. newArrValue := reflect.MakeSlice(sliceValue.Type(), sliceValue.Len()-1, sliceValue.Cap()-1)
  156. // Copy the elements before the last one to the new slice
  157. reflect.Copy(newArrValue, sliceValue.Slice(0, lastIndex))
  158. // Set the original slice to the new slice
  159. sliceValue.Set(newArrValue)
  160. // Return the removed last element
  161. return lastElem.Interface()
  162. }
  163. // ArrUnshift adds an element to the beginning of the array
  164. func ArrUnshift(arr interface{}, value interface{}) int {
  165. arrValue := reflect.ValueOf(arr)
  166. // Ensure arr is a pointer to a slice
  167. if arrValue.Kind() != reflect.Ptr || arrValue.Elem().Kind() != reflect.Slice {
  168. return 0
  169. }
  170. // Get the slice from the pointer
  171. sliceValue := arrValue.Elem()
  172. // Create a new slice with one additional element
  173. newArrValue := reflect.MakeSlice(sliceValue.Type(), sliceValue.Len()+1, sliceValue.Cap()+1)
  174. // Set the new element as the first element
  175. newArrValue.Index(0).Set(reflect.ValueOf(value))
  176. // Copy the old elements to the new slice, starting at index 1
  177. reflect.Copy(newArrValue.Slice(1, newArrValue.Len()), sliceValue)
  178. // Set the original slice to the new slice
  179. sliceValue.Set(newArrValue)
  180. // Return the new length of the slice
  181. return newArrValue.Len()
  182. }
  183. // ArrPush adds an element to the end of the array
  184. func ArrPush(arr interface{}, value interface{}) int {
  185. arrValue := reflect.ValueOf(arr)
  186. // Ensure arr is a pointer to a slice
  187. if arrValue.Kind() != reflect.Ptr || arrValue.Elem().Kind() != reflect.Slice {
  188. return 0
  189. }
  190. // Get the slice from the pointer
  191. sliceValue := arrValue.Elem()
  192. // Create a new slice with one additional element
  193. newArrValue := reflect.MakeSlice(sliceValue.Type(), sliceValue.Len()+1, sliceValue.Cap()+1)
  194. // Copy the old elements to the new slice
  195. reflect.Copy(newArrValue, sliceValue)
  196. // Set the new element as the last element
  197. newArrValue.Index(sliceValue.Len()).Set(reflect.ValueOf(value))
  198. // Set the original slice to the new slice
  199. sliceValue.Set(newArrValue)
  200. // Return the new length of the slice
  201. return newArrValue.Len()
  202. }
  203. // ConcatArr concatenates two arrays
  204. func ConcatArr(arr1 interface{}, arr2 interface{}) interface{} {
  205. var result []interface{}
  206. value1 := reflect.ValueOf(arr1)
  207. value2 := reflect.ValueOf(arr2)
  208. // 检查 arr1 和 arr2 是否为切片
  209. if value1.Kind() != reflect.Slice || value2.Kind() != reflect.Slice {
  210. panic("ConcatArr: both inputs must be slices")
  211. }
  212. // 如果两个切片的类型相同
  213. if value1.Type() == value2.Type() {
  214. // 创建一个新的切片,类型与输入切片相同
  215. result := reflect.MakeSlice(value1.Type(), 0, value1.Len()+value2.Len())
  216. // 复制第一个切片的元素
  217. for i := 0; i < value1.Len(); i++ {
  218. result = reflect.Append(result, value1.Index(i))
  219. }
  220. // 复制第二个切片的元素
  221. for i := 0; i < value2.Len(); i++ {
  222. result = reflect.Append(result, value2.Index(i))
  223. }
  224. return result.Interface() // 返回类型相同的切片
  225. }
  226. // 否则返回 []interface{}
  227. for i := 0; i < value1.Len(); i++ {
  228. result = append(result, value1.Index(i).Interface())
  229. }
  230. for i := 0; i < value2.Len(); i++ {
  231. result = append(result, value2.Index(i).Interface())
  232. }
  233. return result
  234. }
  235. // ArrAppend inserts a new pointer at a specified index in a pointer array.
  236. func ArrAppend(arr interface{}, value interface{}, index int) {
  237. arrV := reflect.ValueOf(arr)
  238. if arrV.Kind() != reflect.Ptr || arrV.Elem().Kind() != reflect.Slice {
  239. return
  240. }
  241. sliceV := arrV.Elem()
  242. if index < 0 || index > sliceV.Len() {
  243. return
  244. }
  245. valueV := reflect.ValueOf(value)
  246. // 创建一个容纳新值的切片
  247. newSlice := reflect.Append(sliceV, reflect.Zero(sliceV.Type().Elem()))
  248. reflect.Copy(newSlice.Slice(index+1, newSlice.Len()), newSlice.Slice(index, newSlice.Len()-1))
  249. newSlice.Index(index).Set(valueV)
  250. // 更新原始切片
  251. sliceV.Set(newSlice)
  252. return
  253. }
  254. // ArrRemove removes an element from the array
  255. func ArrRemove(arr interface{}, value interface{}) {
  256. arrValue := reflect.ValueOf(arr)
  257. // Ensure arr is a pointer to a slice
  258. if arrValue.Kind() != reflect.Ptr || arrValue.Elem().Kind() != reflect.Slice {
  259. return
  260. }
  261. // Get the slice from the pointer
  262. slice := arrValue.Elem()
  263. index := ArrIndex(slice.Interface(), value)
  264. // If index is found, remove the element at that index
  265. if index != -1 {
  266. // Remove the element at the specified index
  267. newSlice := reflect.MakeSlice(slice.Type(), 0, slice.Len()-1)
  268. // Copy elements before the index
  269. newSlice = reflect.AppendSlice(slice.Slice(0, index), slice.Slice(index+1, slice.Len()))
  270. // Set the new slice back to the original reference
  271. slice.Set(newSlice)
  272. }
  273. }
  274. func SortArr(arr interface{}, order string) interface{} {
  275. v := reflect.ValueOf(arr)
  276. if v.Kind() != reflect.Slice {
  277. panic("SortArr: input must be a slice")
  278. }
  279. // 创建一个新的切片来存储排序结果
  280. newSlice := reflect.MakeSlice(v.Type(), v.Len(), v.Cap())
  281. for i := 0; i < v.Len(); i++ {
  282. newSlice.Index(i).Set(v.Index(i))
  283. }
  284. order = strings.ToLower(order)
  285. sort.SliceStable(newSlice.Interface(), func(i, j int) bool {
  286. return compare(newSlice.Index(i), newSlice.Index(j), order)
  287. })
  288. return newSlice.Interface()
  289. }
  290. func compare(elemI, elemJ reflect.Value, order string) bool {
  291. valI := reflect.Indirect(elemI)
  292. valJ := reflect.Indirect(elemJ)
  293. // 对interface{}类型处理实际类型
  294. if elemI.Kind() == reflect.Interface {
  295. valI = reflect.Indirect(elemI.Elem())
  296. }
  297. if elemJ.Kind() == reflect.Interface {
  298. valJ = reflect.Indirect(elemJ.Elem())
  299. }
  300. if valI.Kind() != valJ.Kind() {
  301. if order == "asc" {
  302. return valI.Kind() < valJ.Kind()
  303. }
  304. return valI.Kind() > valJ.Kind()
  305. }
  306. switch kind := valI.Kind(); kind {
  307. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  308. return compareNumbers(valI.Int(), valJ.Int(), order)
  309. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  310. return compareNumbers(int64(valI.Uint()), int64(valJ.Uint()), order)
  311. case reflect.Float32, reflect.Float64:
  312. return compareNumbers(valI.Float(), valJ.Float(), order)
  313. case reflect.String:
  314. return compareStrings(valI.String(), valJ.String(), order)
  315. case reflect.Struct:
  316. return compareStructs(valI, valJ, order)
  317. default:
  318. panic("SortArr: unsupported element type")
  319. }
  320. }
  321. func compareNumbers(a, b interface{}, order string) bool {
  322. switch order {
  323. case "asc":
  324. return a.(int64) < b.(int64)
  325. case "desc":
  326. return a.(int64) > b.(int64)
  327. default:
  328. return a.(int64) > b.(int64)
  329. }
  330. }
  331. func compareStrings(a, b string, order string) bool {
  332. switch order {
  333. case "asc":
  334. return a < b
  335. case "desc":
  336. return a > b
  337. default:
  338. return a > b
  339. }
  340. }
  341. func compareStructs(valI, valJ reflect.Value, order string) bool {
  342. if valI.NumField() > 0 && valJ.NumField() > 0 {
  343. fieldI := reflect.Indirect(valI.Field(0))
  344. fieldJ := reflect.Indirect(valJ.Field(0))
  345. if fieldI.Kind() == fieldJ.Kind() {
  346. switch fieldI.Kind() {
  347. case reflect.String:
  348. return compareStrings(fieldI.String(), fieldJ.String(), order)
  349. case reflect.Int:
  350. return compareNumbers(fieldI.Int(), fieldJ.Int(), order)
  351. }
  352. }
  353. }
  354. return false
  355. }