You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/utils/strings.go

97 lines
2.0 KiB

/*
** description("").
** copyright('tuoyun,www.tuoyun.net').
** author("fg,Gordon@tuoyun.net").
** time(2021/4/8 15:09).
*/
package utils
import (
"Open_IM/pkg/common/constant"
"encoding/json"
"math/rand"
"strconv"
)
func IntToString(i int) string {
return strconv.FormatInt(int64(i), 10)
}
func StringToInt(i string) int {
j, _ := strconv.Atoi(i)
return j
}
func StringToInt64(i string) int64 {
j, _ := strconv.ParseInt(i, 10, 64)
return j
}
func StringToInt32(i string) int32 {
j, _ := strconv.ParseInt(i, 10, 64)
return int32(j)
}
func Int32ToString(i int32) string {
return strconv.FormatInt(int64(i), 10)
}
//judge a string whether in the string list
func IsContain(target string, List []string) bool {
for _, element := range List {
if target == element {
return true
}
}
return false
}
func IsContainInt32(target int32, List []int32) bool {
for _, element := range List {
if target == element {
return true
}
}
return false
}
func InterfaceArrayToStringArray(data []interface{}) (i []string) {
for _, param := range data {
i = append(i, param.(string))
}
return i
}
func StructToJsonString(param interface{}) string {
dataType, _ := json.Marshal(param)
dataString := string(dataType)
return dataString
}
func StructToJsonBytes(param interface{}) []byte {
dataType, _ := json.Marshal(param)
return dataType
}
//The incoming parameter must be a pointer
func JsonStringToStruct(s string, args interface{}) error {
err := json.Unmarshal([]byte(s), args)
return err
}
func GetMsgID(sendID string) string {
t := int64ToString(GetCurrentTimestampByNano())
return Md5(t + sendID + int64ToString(rand.Int63n(GetCurrentTimestampByNano())))
}
func GetConversationIDBySessionType(sourceID string, sessionType int) string {
switch sessionType {
case constant.SingleChatType:
return "single_" + sourceID
case constant.GroupChatType:
return "group_" + sourceID
}
return ""
}
func int64ToString(i int64) string {
return strconv.FormatInt(i, 10)
}
func Int64ToString(i int64) string {
return strconv.FormatInt(i, 10)
}