forked from TrueCloudLab/frostfs-sdk-go
[#276] Merge repo with frostfs-api-go
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
This commit is contained in:
parent
5361f0eceb
commit
6ce73790ea
337 changed files with 66666 additions and 283 deletions
40
api/rpc/message/encoding.go
Normal file
40
api/rpc/message/encoding.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// GRPCConvertedMessage is an interface
|
||||
// of the gRPC message that is used
|
||||
// for Message encoding/decoding.
|
||||
type GRPCConvertedMessage interface {
|
||||
UnmarshalProtobuf([]byte) error
|
||||
}
|
||||
|
||||
// Unmarshal decodes m from its Protobuf binary representation
|
||||
// via related gRPC message.
|
||||
//
|
||||
// gm should be tof the same type as the m.ToGRPCMessage() return.
|
||||
func Unmarshal(m Message, data []byte, gm GRPCConvertedMessage) error {
|
||||
if err := gm.UnmarshalProtobuf(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.FromGRPCMessage(gm)
|
||||
}
|
||||
|
||||
// MarshalJSON encodes m to Protobuf JSON representation.
|
||||
func MarshalJSON(m Message) ([]byte, error) {
|
||||
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 any) error {
|
||||
if err := json.Unmarshal(data, gm); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.FromGRPCMessage(gm)
|
||||
}
|
43
api/rpc/message/message.go
Normal file
43
api/rpc/message/message.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/grpc"
|
||||
)
|
||||
|
||||
// Message represents raw Protobuf message
|
||||
// that can be transmitted via several
|
||||
// transport protocols.
|
||||
type Message interface {
|
||||
// Must return gRPC message that can
|
||||
// be used for gRPC protocol transmission.
|
||||
ToGRPCMessage() grpc.Message
|
||||
|
||||
// Must restore the message from related
|
||||
// gRPC message.
|
||||
//
|
||||
// If gRPC message is not a related one,
|
||||
// ErrUnexpectedMessageType can be returned
|
||||
// to indicate this.
|
||||
FromGRPCMessage(grpc.Message) error
|
||||
}
|
||||
|
||||
// ErrUnexpectedMessageType is an error that
|
||||
// is used to indicate message mismatch.
|
||||
type ErrUnexpectedMessageType struct {
|
||||
exp, act any
|
||||
}
|
||||
|
||||
// NewUnexpectedMessageType initializes an error about message mismatch
|
||||
// between act and exp.
|
||||
func NewUnexpectedMessageType(act, exp any) ErrUnexpectedMessageType {
|
||||
return ErrUnexpectedMessageType{
|
||||
exp: exp,
|
||||
act: act,
|
||||
}
|
||||
}
|
||||
|
||||
func (e ErrUnexpectedMessageType) Error() string {
|
||||
return fmt.Sprintf("unexpected message type %T: expected %T", e.act, e.exp)
|
||||
}
|
126
api/rpc/message/test/message.go
Normal file
126
api/rpc/message/test/message.go
Normal file
|
@ -0,0 +1,126 @@
|
|||
package messagetest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/message"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/util/proto/encoding"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type jsonMessage interface {
|
||||
json.Marshaler
|
||||
json.Unmarshaler
|
||||
}
|
||||
|
||||
type binaryMessage interface {
|
||||
StableMarshal([]byte) []byte
|
||||
StableSize() int
|
||||
Unmarshal([]byte) error
|
||||
}
|
||||
|
||||
func TestRPCMessage(t *testing.T, msgGens ...func(empty bool) message.Message) {
|
||||
for _, msgGen := range msgGens {
|
||||
msg := msgGen(false)
|
||||
|
||||
t.Run(fmt.Sprintf("convert_%T", msg), func(t *testing.T) {
|
||||
msg := msgGen(false)
|
||||
|
||||
err := msg.FromGRPCMessage(100)
|
||||
|
||||
require.True(t, errors.As(err, new(message.ErrUnexpectedMessageType)))
|
||||
|
||||
msg2 := msgGen(true)
|
||||
|
||||
err = msg2.FromGRPCMessage(msg.ToGRPCMessage())
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, msg, msg2)
|
||||
})
|
||||
|
||||
t.Run("encoding", func(t *testing.T) {
|
||||
if jm, ok := msg.(jsonMessage); ok {
|
||||
t.Run(fmt.Sprintf("JSON_%T", msg), func(t *testing.T) {
|
||||
data, err := jm.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
jm2 := msgGen(true).(jsonMessage)
|
||||
require.NoError(t, jm2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, jm, jm2)
|
||||
})
|
||||
}
|
||||
|
||||
if bm, ok := msg.(binaryMessage); ok {
|
||||
t.Run(fmt.Sprintf("%T.StableSize() does no allocations", bm), func(t *testing.T) {
|
||||
require.Zero(t, testing.AllocsPerRun(1000, func() {
|
||||
_ = bm.StableSize()
|
||||
}))
|
||||
})
|
||||
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
|
||||
data := bm.StableMarshal(nil)
|
||||
|
||||
bm2 := msgGen(true).(binaryMessage)
|
||||
require.NoError(t, bm2.Unmarshal(data))
|
||||
|
||||
require.Equal(t, bm, bm2)
|
||||
})
|
||||
}
|
||||
t.Run("compatibility", func(t *testing.T) {
|
||||
testCompatibility(t, msgGen)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testCompatibility(t *testing.T, msgGen func(empty bool) message.Message) {
|
||||
compareBinary := func(t *testing.T, msg message.Message) {
|
||||
am, ok := msg.(binaryMessage)
|
||||
if !ok {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
a := am.StableMarshal(nil)
|
||||
b := msg.ToGRPCMessage().(encoding.ProtoMarshaler).MarshalProtobuf(nil)
|
||||
if len(a) == 0 {
|
||||
require.Empty(t, b)
|
||||
} else {
|
||||
require.Equal(t, a, b)
|
||||
}
|
||||
}
|
||||
compareJSON := func(t *testing.T, msg message.Message) {
|
||||
am, ok := msg.(jsonMessage)
|
||||
if !ok {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
a, err := am.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
b, err := json.Marshal(msg.ToGRPCMessage())
|
||||
require.NoError(t, err)
|
||||
|
||||
require.JSONEq(t, string(a), string(b))
|
||||
}
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
msg := msgGen(true)
|
||||
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
|
||||
compareBinary(t, msg)
|
||||
})
|
||||
t.Run(fmt.Sprintf("JSON_%T", msg), func(t *testing.T) {
|
||||
compareJSON(t, msg)
|
||||
})
|
||||
})
|
||||
t.Run("not empty", func(t *testing.T) {
|
||||
msg := msgGen(false)
|
||||
t.Run(fmt.Sprintf("Binary_%T", msg), func(t *testing.T) {
|
||||
compareBinary(t, msg)
|
||||
})
|
||||
t.Run(fmt.Sprintf("JSON_%T", msg), func(t *testing.T) {
|
||||
compareJSON(t, msg)
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue