1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2023-08-25 15:31:00 +08:00
parent 04aecd4abc
commit b0a97978d8
58 changed files with 1330 additions and 476 deletions

43
cache/msg_pack.go vendored Normal file
View File

@ -0,0 +1,43 @@
package cache
import (
"github.com/vmihailenco/msgpack/v5"
)
func Marshal(v any) ([]byte, error) {
switch v := v.(type) {
case nil:
return nil, nil
case []byte:
return v, nil
case string:
return []byte(v), nil
}
b, err := msgpack.Marshal(v)
if err != nil {
return nil, err
}
return b, err
}
func Unmarshal(b []byte, v any) error {
if len(b) == 0 {
return nil
}
switch v := v.(type) {
case nil:
return nil
case *[]byte:
clone := make([]byte, len(b))
copy(clone, b)
*v = clone
case *string:
*v = string(b)
return nil
}
return msgpack.Unmarshal(b, v)
}