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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 16:53:42 +03:00 committed by Alex Vanin
parent 4e163617d1
commit 85d4713348
6 changed files with 114 additions and 4 deletions

View file

@ -76,3 +76,47 @@ func (t *SessionToken) Signature() *pkg.Signature {
GetSignature(), GetSignature(),
) )
} }
// Marshal marshals SessionToken into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (t *SessionToken) Marshal(bs ...[]byte) ([]byte, error) {
var buf []byte
if len(bs) > 0 {
buf = bs[0]
}
return (*session.SessionToken)(t).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of SessionToken.
func (t *SessionToken) Unmarshal(data []byte) error {
tV2 := new(session.SessionToken)
if err := tV2.Unmarshal(data); err != nil {
return err
}
*t = *NewSessionTokenFromV2(tV2)
return nil
}
// MarshalJSON encodes SessionToken to protobuf JSON format.
func (t *SessionToken) MarshalJSON() ([]byte, error) {
return (*session.SessionToken)(t).
MarshalJSON()
}
// UnmarshalJSON decodes SessionToken from protobuf JSON format.
func (t *SessionToken) UnmarshalJSON(data []byte) error {
tV2 := new(session.SessionToken)
if err := tV2.UnmarshalJSON(data); err != nil {
return err
}
*t = *NewSessionTokenFromV2(tV2)
return nil
}

View file

@ -40,3 +40,28 @@ func TestSessionToken_SetSessionKey(t *testing.T) {
require.Equal(t, key, token.SessionKey()) require.Equal(t, key, token.SessionKey())
} }
func TestSessionTokenEncoding(t *testing.T) {
tok := NewSessionToken()
tok.SetID([]byte("id"))
t.Run("binary", func(t *testing.T) {
data, err := tok.Marshal()
require.NoError(t, err)
tok2 := NewSessionToken()
require.NoError(t, tok2.Unmarshal(data))
require.Equal(t, tok, tok2)
})
t.Run("json", func(t *testing.T) {
data, err := tok.MarshalJSON()
require.NoError(t, err)
tok2 := NewSessionToken()
require.NoError(t, tok2.UnmarshalJSON(data))
require.Equal(t, tok, tok2)
})
}

View file

@ -64,3 +64,23 @@ func (t *SessionTokenBody) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (t *SessionToken) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
SessionTokenToGRPCMessage(t),
)
}
func (t *SessionToken) UnmarshalJSON(data []byte) error {
msg := new(session.SessionToken)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*t = *SessionTokenFromGRPCMessage(msg)
return nil
}

View file

@ -42,3 +42,15 @@ func TestSessionTokenBodyJSON(t *testing.T) {
require.Equal(t, b, b2) require.Equal(t, b, b2)
} }
func TestSessionTokenJSON(t *testing.T) {
tok := generateSessionToken("id")
data, err := tok.MarshalJSON()
require.NoError(t, err)
tok2 := new(session.SessionToken)
require.NoError(t, tok2.UnmarshalJSON(data))
require.Equal(t, tok, tok2)
}

View file

@ -417,6 +417,17 @@ func (t *SessionToken) StableSize() (size int) {
return size return size
} }
func (t *SessionToken) Unmarshal(data []byte) error {
m := new(session.SessionToken)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*t = *SessionTokenFromGRPCMessage(m)
return nil
}
func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) { func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) {
if r == nil { if r == nil {
return []byte{}, nil return []byte{}, nil

View file

@ -104,16 +104,14 @@ func TestSessionTokenBody_StableMarshal(t *testing.T) {
func TestSessionToken_StableMarshal(t *testing.T) { func TestSessionToken_StableMarshal(t *testing.T) {
sessionTokenFrom := generateSessionToken("Session Token") sessionTokenFrom := generateSessionToken("Session Token")
transport := new(grpc.SessionToken)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
wire, err := sessionTokenFrom.StableMarshal(nil) wire, err := sessionTokenFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
err = goproto.Unmarshal(wire, transport) sessionTokenTo := new(session.SessionToken)
require.NoError(t, err) require.NoError(t, sessionTokenTo.Unmarshal(wire))
sessionTokenTo := session.SessionTokenFromGRPCMessage(transport)
require.Equal(t, sessionTokenFrom, sessionTokenTo) require.Equal(t, sessionTokenFrom, sessionTokenTo)
}) })
} }