diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index e4383263c..4c3e9e5f3 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "bytes" "encoding/json" "github.com/gogo/protobuf/jsonpb" "github.com/golang/protobuf/proto" @@ -144,6 +145,7 @@ func Pb2String(pb proto.Message) (string, error) { return marshaler.MarshalToString(pb) } + func Map2Pb(m map[string]interface{}) (pb proto.Message, err error) { b, err := json.Marshal(m) if err != nil { @@ -155,3 +157,16 @@ func Map2Pb(m map[string]interface{}) (pb proto.Message, err error) { } 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 +}