forked from TrueCloudLab/frostfs-api-go
[#168] session: Implement binary/JSON encoders/decoders on ResponseMeta
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>support/v2.15
parent
986bc1b0ea
commit
edf6ec05b3
|
@ -144,3 +144,23 @@ func (r *RequestVerificationHeader) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ResponseMetaHeader) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
ResponseMetaHeaderToGRPCMessage(r),
|
||||
)
|
||||
}
|
||||
|
||||
func (r *ResponseMetaHeader) UnmarshalJSON(data []byte) error {
|
||||
msg := new(session.ResponseMetaHeader)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*r = *ResponseMetaHeaderFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -90,3 +90,15 @@ func TestRequestVerificationHeaderJSON(t *testing.T) {
|
|||
|
||||
require.Equal(t, r, r2)
|
||||
}
|
||||
|
||||
func TestResponseMetaHeaderJSON(t *testing.T) {
|
||||
r := generateResponseMetaHeader(1)
|
||||
|
||||
data, err := r.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
r2 := new(session.ResponseMetaHeader)
|
||||
require.NoError(t, r2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, r, r2)
|
||||
}
|
||||
|
|
|
@ -679,6 +679,17 @@ func (r *ResponseMetaHeader) StableSize() (size int) {
|
|||
return size
|
||||
}
|
||||
|
||||
func (r *ResponseMetaHeader) Unmarshal(data []byte) error {
|
||||
m := new(session.ResponseMetaHeader)
|
||||
if err := goproto.Unmarshal(data, m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*r = *ResponseMetaHeaderFromGRPCMessage(m)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ResponseVerificationHeader) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if r == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -150,16 +150,14 @@ func TestResponseMetaHeader_StableMarshal(t *testing.T) {
|
|||
metaHeaderOrigin := generateResponseMetaHeader(10)
|
||||
metaHeaderFrom := generateResponseMetaHeader(20)
|
||||
metaHeaderFrom.SetOrigin(metaHeaderOrigin)
|
||||
transport := new(grpc.ResponseMetaHeader)
|
||||
|
||||
t.Run("non empty", func(t *testing.T) {
|
||||
wire, err := metaHeaderFrom.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = goproto.Unmarshal(wire, transport)
|
||||
require.NoError(t, err)
|
||||
metaHeaderTo := new(session.ResponseMetaHeader)
|
||||
require.NoError(t, metaHeaderTo.Unmarshal(wire))
|
||||
|
||||
metaHeaderTo := session.ResponseMetaHeaderFromGRPCMessage(transport)
|
||||
require.Equal(t, metaHeaderFrom, metaHeaderTo)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue