You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.go 3.1 kB

11 months ago
11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package common
  13. import (
  14. "encoding/json"
  15. "github.com/go-resty/resty/v2"
  16. "math"
  17. "math/rand"
  18. "strconv"
  19. "time"
  20. )
  21. // 求交集
  22. func Intersect(slice1, slice2 []int64) []int64 {
  23. m := make(map[int64]int)
  24. nn := make([]int64, 0)
  25. for _, v := range slice1 {
  26. m[v]++
  27. }
  28. for _, v := range slice2 {
  29. times, _ := m[v]
  30. if times == 1 {
  31. nn = append(nn, v)
  32. }
  33. }
  34. return nn
  35. }
  36. func IntersectString(slice1, slice2 []string) []string {
  37. k := make(map[string]int)
  38. for _, num := range slice1 {
  39. k[num]++
  40. }
  41. var output []string
  42. for _, num := range slice2 {
  43. if k[num] > 0 {
  44. output = append(output, num)
  45. k[num]--
  46. }
  47. }
  48. return output
  49. }
  50. func RemoveDuplicates(slc []string) []string {
  51. keys := make(map[string]bool)
  52. list := []string{}
  53. for _, entry := range slc {
  54. if _, value := keys[entry]; !value {
  55. keys[entry] = true
  56. list = append(list, entry)
  57. }
  58. }
  59. return list
  60. }
  61. func MicsSlice(origin []int64, count int) []int64 {
  62. tmpOrigin := make([]int64, len(origin))
  63. copy(tmpOrigin, origin)
  64. //一定要seed
  65. rand.Seed(time.Now().Unix())
  66. rand.Shuffle(len(tmpOrigin), func(i int, j int) {
  67. tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
  68. })
  69. result := make([]int64, 0, count)
  70. for index, value := range tmpOrigin {
  71. if index == count {
  72. break
  73. }
  74. result = append(result, value)
  75. }
  76. return result
  77. }
  78. func RoundFloat(val float64, precision uint) float64 {
  79. ratio := math.Pow(10, float64(precision))
  80. return math.Round(val*ratio) / ratio
  81. }
  82. func Contains(s []string, e string) bool {
  83. for _, a := range s {
  84. if a == e {
  85. return true
  86. }
  87. }
  88. return false
  89. }
  90. func ConcatMultipleSlices[T any](slices [][]T) []T {
  91. var totalLen int
  92. for _, s := range slices {
  93. totalLen += len(s)
  94. }
  95. result := make([]T, totalLen)
  96. var i int
  97. for _, s := range slices {
  98. i += copy(result[i:], s)
  99. }
  100. return result
  101. }
  102. func GetRestyRequest(timeoutSeconds int64) *resty.Request {
  103. client := resty.New().SetTimeout(time.Duration(timeoutSeconds) * time.Second)
  104. request := client.R()
  105. return request
  106. }
  107. func Unique(s []string) []string {
  108. inResult := make(map[string]bool)
  109. var result []string
  110. for _, str := range s {
  111. if _, ok := inResult[str]; !ok {
  112. inResult[str] = true
  113. result = append(result, str)
  114. }
  115. }
  116. return result
  117. }
  118. func ConvertTypeToString(v interface{}) string {
  119. switch v.(type) {
  120. case int:
  121. s := v.(int)
  122. return strconv.Itoa(s)
  123. case string:
  124. s := v.(string)
  125. return s
  126. case float64:
  127. s := strconv.FormatFloat(v.(float64), 'f', -1, 64)
  128. return s
  129. case int64:
  130. s := v.(int64)
  131. return strconv.FormatInt(s, 64)
  132. case json.Number:
  133. return v.(json.Number).String()
  134. default:
  135. return ""
  136. }
  137. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.