[#168] session: Implement binary/JSON encoders/decoders on ResponseMeta

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 17:11:09 +03:00 committed by Alex Vanin
parent 986bc1b0ea
commit edf6ec05b3
4 changed files with 45 additions and 4 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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

View file

@ -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)
})
}