This commit is contained in:
wangchuxiao
2022-04-28 15:13:39 +08:00
parent 59468eb162
commit 65f02e5139
8 changed files with 269 additions and 153 deletions
+19
View File
@@ -1,6 +1,10 @@
package utils
import (
"bytes"
"encoding/json"
"github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"math/rand"
@@ -131,3 +135,18 @@ func RemoveRepeatedStringInList(slc []string) []string {
}
return result
}
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
}