Files
open-im-server/pkg/utils/utils.go
T

182 lines
3.6 KiB
Go
Raw Normal View History

2021-12-07 15:44:12 +08:00
package utils
import (
2022-05-24 19:25:49 +08:00
"bytes"
2022-04-28 15:13:39 +08:00
"encoding/json"
"github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
2021-12-28 16:23:14 +08:00
"github.com/jinzhu/copier"
2022-01-04 12:56:10 +08:00
"github.com/pkg/errors"
2022-03-17 13:49:21 +08:00
"math/rand"
2022-01-04 12:56:10 +08:00
"runtime"
"strconv"
2022-01-15 18:36:40 +08:00
"strings"
2022-03-17 13:49:21 +08:00
"time"
2021-12-07 15:44:12 +08:00
)
// copy a by b b->a
func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error) {
2021-12-28 17:49:52 +08:00
return copier.Copy(a, b)
2021-12-07 15:44:12 +08:00
}
2022-01-04 12:56:10 +08:00
func Wrap(err error, message string) error {
return errors.Wrap(err, "==> "+printCallerNameAndLine()+message)
}
func WithMessage(err error, message string) error {
return errors.WithMessage(err, "==> "+printCallerNameAndLine()+message)
}
func printCallerNameAndLine() string {
pc, _, line, _ := runtime.Caller(2)
return runtime.FuncForPC(pc).Name() + "()@" + strconv.Itoa(line) + ": "
}
2022-01-15 18:36:40 +08:00
func GetSelfFuncName() string {
pc, _, _, _ := runtime.Caller(1)
return cleanUpFuncName(runtime.FuncForPC(pc).Name())
}
2023-01-05 10:47:41 +08:00
func GetFuncName(skip int) string {
pc, _, _, _ := runtime.Caller(skip + 1)
return cleanUpFuncName(runtime.FuncForPC(pc).Name())
}
2022-01-15 18:36:40 +08:00
func cleanUpFuncName(funcName string) string {
end := strings.LastIndex(funcName, ".")
if end == -1 {
return ""
}
return funcName[end+1:]
}
2022-04-08 17:55:44 +08:00
2023-01-05 10:47:41 +08:00
// Get the intersection of two slices
2022-01-20 11:36:43 +08:00
func Intersect(slice1, slice2 []uint32) []uint32 {
m := make(map[uint32]bool)
n := make([]uint32, 0)
for _, v := range slice1 {
m[v] = true
}
for _, v := range slice2 {
flag, _ := m[v]
if flag {
n = append(n, v)
}
}
return n
}
2022-04-08 17:55:44 +08:00
2023-01-05 10:47:41 +08:00
// Get the diff of two slices
2022-01-20 11:36:43 +08:00
func Difference(slice1, slice2 []uint32) []uint32 {
m := make(map[uint32]bool)
n := make([]uint32, 0)
inter := Intersect(slice1, slice2)
for _, v := range inter {
m[v] = true
}
for _, v := range slice1 {
if !m[v] {
n = append(n, v)
}
}
for _, v := range slice2 {
if !m[v] {
n = append(n, v)
}
}
return n
}
2022-04-21 16:46:48 +08:00
2023-01-05 10:47:41 +08:00
// Get the intersection of two slices
2022-04-21 16:46:48 +08:00
func IntersectString(slice1, slice2 []string) []string {
m := make(map[string]bool)
n := make([]string, 0)
for _, v := range slice1 {
m[v] = true
}
for _, v := range slice2 {
flag, _ := m[v]
if flag {
n = append(n, v)
}
}
return n
}
2023-01-05 10:47:41 +08:00
// Get the diff of two slices
2022-04-21 16:46:48 +08:00
func DifferenceString(slice1, slice2 []string) []string {
m := make(map[string]bool)
n := make([]string, 0)
inter := IntersectString(slice1, slice2)
for _, v := range inter {
m[v] = true
}
for _, v := range slice1 {
if !m[v] {
n = append(n, v)
}
}
for _, v := range slice2 {
if !m[v] {
n = append(n, v)
}
}
return n
}
2022-03-17 13:49:21 +08:00
func OperationIDGenerator() string {
return strconv.FormatInt(time.Now().UnixNano()+int64(rand.Uint32()), 10)
}
2022-03-28 12:17:07 +08:00
2022-03-31 11:43:14 +08:00
func RemoveRepeatedStringInList(slc []string) []string {
2022-03-28 12:17:07 +08:00
var result []string
tempMap := map[string]byte{}
for _, e := range slc {
l := len(tempMap)
tempMap[e] = 0
if len(tempMap) != l {
result = append(result, e)
}
}
return result
}
2022-04-28 15:13:39 +08:00
2022-05-24 19:24:11 +08:00
func Pb2String(pb proto.Message) (string, error) {
marshaler := jsonpb.Marshaler{
2022-04-28 15:13:39 +08:00
OrigName: true,
2022-05-24 19:24:11 +08:00
EnumsAsInts: false,
EmitDefaults: false,
2022-04-28 15:13:39 +08:00
}
2022-05-24 19:24:11 +08:00
return marshaler.MarshalToString(pb)
2022-05-25 15:09:17 +08:00
}
2022-05-24 19:24:11 +08:00
2022-05-25 15:09:17 +08:00
func String2Pb(s string, pb proto.Message) error {
return proto.Unmarshal([]byte(s), pb)
2022-05-24 19:24:11 +08:00
}
2022-05-24 19:25:49 +08:00
2022-06-02 16:44:55 +08:00
func Map2Pb(m map[string]string) (pb proto.Message, err error) {
2022-05-24 19:24:11 +08:00
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
err = proto.Unmarshal(b, pb)
if err != nil {
return nil, err
}
return pb, nil
2022-04-28 15:13:39 +08:00
}
2022-05-24 19:25:49 +08:00
func Pb2Map(pb proto.Message) (map[string]interface{}, error) {
_buffer := bytes.Buffer{}
jsonbMarshaller := &jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: true,
2022-05-31 14:51:37 +08:00
EmitDefaults: false,
2022-05-24 19:25:49 +08:00
}
_ = jsonbMarshaller.Marshal(&_buffer, pb)
jsonCnt := _buffer.Bytes()
var out map[string]interface{}
err := json.Unmarshal(jsonCnt, &out)
return out, err
}