redis add get message

This commit is contained in:
Gordon 2022-05-24 19:25:49 +08:00
parent 79ff36d49a
commit ae0cfe1575

View File

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
@ -144,6 +145,7 @@ func Pb2String(pb proto.Message) (string, error) {
return marshaler.MarshalToString(pb) return marshaler.MarshalToString(pb)
} }
func Map2Pb(m map[string]interface{}) (pb proto.Message, err error) { func Map2Pb(m map[string]interface{}) (pb proto.Message, err error) {
b, err := json.Marshal(m) b, err := json.Marshal(m)
if err != nil { if err != nil {
@ -155,3 +157,16 @@ func Map2Pb(m map[string]interface{}) (pb proto.Message, err error) {
} }
return pb, nil return pb, nil
} }
func Pb2Map(pb proto.Message) (map[string]interface{}, error) {
_buffer := bytes.Buffer{}
jsonbMarshaller := &jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: true,
EmitDefaults: true,
}
_ = jsonbMarshaller.Marshal(&_buffer, pb)
jsonCnt := _buffer.Bytes()
var out map[string]interface{}
err := json.Unmarshal(jsonCnt, &out)
return out, err
}