From 4e163617d145928b7f7428fe38a11dc1201609d1 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 16:49:46 +0300 Subject: [PATCH] [#168] session: Implement binary/JSON encoders/decoders on TokenBody Signed-off-by: Leonard Lyubich --- v2/session/json.go | 20 ++++++++++++++++++++ v2/session/json_test.go | 12 ++++++++++++ v2/session/marshal.go | 11 +++++++++++ v2/session/marshal_test.go | 6 ++---- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/v2/session/json.go b/v2/session/json.go index f8cf61dd..d192b657 100644 --- a/v2/session/json.go +++ b/v2/session/json.go @@ -44,3 +44,23 @@ func (l *TokenLifetime) UnmarshalJSON(data []byte) error { return nil } + +func (t *SessionTokenBody) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + SessionTokenBodyToGRPCMessage(t), + ) +} + +func (t *SessionTokenBody) UnmarshalJSON(data []byte) error { + msg := new(session.SessionToken_Body) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *t = *SessionTokenBodyFromGRPCMessage(msg) + + return nil +} diff --git a/v2/session/json_test.go b/v2/session/json_test.go index add76148..10eda4e1 100644 --- a/v2/session/json_test.go +++ b/v2/session/json_test.go @@ -30,3 +30,15 @@ func TestTokenLifetimeJSON(t *testing.T) { require.Equal(t, l, l2) } + +func TestSessionTokenBodyJSON(t *testing.T) { + b := generateSessionTokenBody("id") + + data, err := b.MarshalJSON() + require.NoError(t, err) + + b2 := new(session.SessionTokenBody) + require.NoError(t, b2.UnmarshalJSON(data)) + + require.Equal(t, b, b2) +} diff --git a/v2/session/marshal.go b/v2/session/marshal.go index 2e103a04..437147d2 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -366,6 +366,17 @@ func (t *SessionTokenBody) StableSize() (size int) { return size } +func (t *SessionTokenBody) Unmarshal(data []byte) error { + m := new(session.SessionToken_Body) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *t = *SessionTokenBodyFromGRPCMessage(m) + + return nil +} + func (t *SessionToken) StableMarshal(buf []byte) ([]byte, error) { if t == nil { return []byte{}, nil diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go index 059aa849..1dc5397e 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -90,16 +90,14 @@ func TestObjectSessionContext_StableMarshal(t *testing.T) { func TestSessionTokenBody_StableMarshal(t *testing.T) { sessionTokenBodyFrom := generateSessionTokenBody("Session Token Body") - transport := new(grpc.SessionToken_Body) t.Run("non empty", func(t *testing.T) { wire, err := sessionTokenBodyFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + sessionTokenBodyTo := new(session.SessionTokenBody) + require.NoError(t, sessionTokenBodyTo.Unmarshal(wire)) - sessionTokenBodyTo := session.SessionTokenBodyFromGRPCMessage(transport) require.Equal(t, sessionTokenBodyFrom, sessionTokenBodyTo) }) }