[#77] protogen: Initial implementation

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-04-25 15:13:10 +03:00
parent a28ceb251a
commit adb7c602d7
115 changed files with 36376 additions and 20397 deletions

View file

@ -1,17 +1,14 @@
package message
import (
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/grpc"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"encoding/json"
)
// GRPCConvertedMessage is an interface
// of the gRPC message that is used
// for Message encoding/decoding.
type GRPCConvertedMessage interface {
grpc.Message
proto.Message
UnmarshalProtobuf([]byte) error
}
// Unmarshal decodes m from its Protobuf binary representation
@ -19,7 +16,7 @@ type GRPCConvertedMessage interface {
//
// gm should be tof the same type as the m.ToGRPCMessage() return.
func Unmarshal(m Message, data []byte, gm GRPCConvertedMessage) error {
if err := proto.Unmarshal(data, gm); err != nil {
if err := gm.UnmarshalProtobuf(data); err != nil {
return err
}
@ -28,21 +25,16 @@ func Unmarshal(m Message, data []byte, gm GRPCConvertedMessage) error {
// MarshalJSON encodes m to Protobuf JSON representation.
func MarshalJSON(m Message) ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
m.ToGRPCMessage().(proto.Message),
)
return json.Marshal(m.ToGRPCMessage())
}
// UnmarshalJSON decodes m from its Protobuf JSON representation
// via related gRPC message.
//
// gm should be tof the same type as the m.ToGRPCMessage() return.
func UnmarshalJSON(m Message, data []byte, gm GRPCConvertedMessage) error {
if err := protojson.Unmarshal(data, gm); err != nil {
func UnmarshalJSON(m Message, data []byte, gm any) error {
if err := json.Unmarshal(data, gm); err != nil {
return err
}
return m.FromGRPCMessage(gm)
}