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.8 kB

10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "fmt"
  16. "github.com/go-resty/resty/v2"
  17. "math"
  18. "math/rand"
  19. "reflect"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. // 求交集
  25. func Intersect(slice1, slice2 []int64) []int64 {
  26. m := make(map[int64]int)
  27. nn := make([]int64, 0)
  28. for _, v := range slice1 {
  29. m[v]++
  30. }
  31. for _, v := range slice2 {
  32. times, _ := m[v]
  33. if times == 1 {
  34. nn = append(nn, v)
  35. }
  36. }
  37. return nn
  38. }
  39. func IntersectString(slice1, slice2 []string) []string {
  40. k := make(map[string]int)
  41. for _, num := range slice1 {
  42. k[num]++
  43. }
  44. var output []string
  45. for _, num := range slice2 {
  46. if k[num] > 0 {
  47. output = append(output, num)
  48. k[num]--
  49. }
  50. }
  51. return output
  52. }
  53. func RemoveDuplicates(slc []string) []string {
  54. keys := make(map[string]bool)
  55. list := []string{}
  56. for _, entry := range slc {
  57. if _, value := keys[entry]; !value {
  58. keys[entry] = true
  59. list = append(list, entry)
  60. }
  61. }
  62. return list
  63. }
  64. func MicsSlice(origin []int64, count int) []int64 {
  65. tmpOrigin := make([]int64, len(origin))
  66. copy(tmpOrigin, origin)
  67. //一定要seed
  68. rand.Seed(time.Now().Unix())
  69. rand.Shuffle(len(tmpOrigin), func(i int, j int) {
  70. tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
  71. })
  72. result := make([]int64, 0, count)
  73. for index, value := range tmpOrigin {
  74. if index == count {
  75. break
  76. }
  77. result = append(result, value)
  78. }
  79. return result
  80. }
  81. func RoundFloat(val float64, precision uint) float64 {
  82. ratio := math.Pow(10, float64(precision))
  83. return math.Round(val*ratio) / ratio
  84. }
  85. func Contains(s []string, e string) bool {
  86. for _, a := range s {
  87. if a == e {
  88. return true
  89. }
  90. }
  91. return false
  92. }
  93. func ConcatMultipleSlices[T any](slices [][]T) []T {
  94. var totalLen int
  95. for _, s := range slices {
  96. totalLen += len(s)
  97. }
  98. result := make([]T, totalLen)
  99. var i int
  100. for _, s := range slices {
  101. i += copy(result[i:], s)
  102. }
  103. return result
  104. }
  105. func GetRestyRequest(timeoutSeconds int64) *resty.Request {
  106. client := resty.New().SetTimeout(time.Duration(timeoutSeconds) * time.Second)
  107. request := client.R()
  108. return request
  109. }
  110. func Unique(s []string) []string {
  111. inResult := make(map[string]bool)
  112. var result []string
  113. for _, str := range s {
  114. if _, ok := inResult[str]; !ok {
  115. inResult[str] = true
  116. result = append(result, str)
  117. }
  118. }
  119. return result
  120. }
  121. func ConvertTypeToString(v interface{}) string {
  122. switch v.(type) {
  123. case int:
  124. s := v.(int)
  125. return strconv.Itoa(s)
  126. case string:
  127. s := v.(string)
  128. return s
  129. case float64:
  130. s := strconv.FormatFloat(v.(float64), 'f', -1, 64)
  131. return s
  132. case int64:
  133. s := v.(int64)
  134. return strconv.FormatInt(s, 64)
  135. case json.Number:
  136. return v.(json.Number).String()
  137. default:
  138. return ""
  139. }
  140. }
  141. func GetJSONTag(structObj interface{}, fieldName string) (string, error) {
  142. // 获取结构体的反射类型
  143. t := reflect.TypeOf(structObj)
  144. // 确保传入的是结构体
  145. if t.Kind() != reflect.Struct {
  146. return "", fmt.Errorf("expected a struct, got %T", structObj)
  147. }
  148. // 查找字段
  149. field, found := t.FieldByName(fieldName)
  150. if !found {
  151. return "", fmt.Errorf("field '%s' not found", fieldName)
  152. }
  153. // 获取 `json` 标签
  154. jsonTag := field.Tag.Get("json")
  155. if jsonTag == "" {
  156. return field.Name, nil // 默认使用字段名(小写)
  157. }
  158. // 去掉可能的 `omitempty` 等选项
  159. return strings.Split(jsonTag, ",")[0], nil
  160. }

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.