From 86351a8f908da5d8e896b983d078eefe11322ba0 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 16:40:00 +0300 Subject: [PATCH] [#168] session: Implement binary/JSON encoders/decoders on SessionContext Signed-off-by: Leonard Lyubich --- v2/session/json.go | 26 ++++++++++++++++++++++++++ v2/session/json_test.go | 20 ++++++++++++++++++++ v2/session/marshal.go | 13 +++++++++++++ v2/session/marshal_test.go | 6 ++---- 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 v2/session/json.go create mode 100644 v2/session/json_test.go diff --git a/v2/session/json.go b/v2/session/json.go new file mode 100644 index 0000000..bb9489e --- /dev/null +++ b/v2/session/json.go @@ -0,0 +1,26 @@ +package session + +import ( + session "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" + "google.golang.org/protobuf/encoding/protojson" +) + +func (c *ObjectSessionContext) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + ObjectSessionContextToGRPCMessage(c), + ) +} + +func (c *ObjectSessionContext) UnmarshalJSON(data []byte) error { + msg := new(session.ObjectSessionContext) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *c = *ObjectSessionContextFromGRPCMessage(msg) + + return nil +} diff --git a/v2/session/json_test.go b/v2/session/json_test.go new file mode 100644 index 0000000..d76b6f4 --- /dev/null +++ b/v2/session/json_test.go @@ -0,0 +1,20 @@ +package session_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/stretchr/testify/require" +) + +func TestChecksumJSON(t *testing.T) { + ctx := generateObjectCtx("id") + + data, err := ctx.MarshalJSON() + require.NoError(t, err) + + ctx2 := new(session.ObjectSessionContext) + require.NoError(t, ctx2.UnmarshalJSON(data)) + + require.Equal(t, ctx, ctx2) +} diff --git a/v2/session/marshal.go b/v2/session/marshal.go index 9bd4eb1..34e038e 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -2,6 +2,8 @@ package session import ( "github.com/nspcc-dev/neofs-api-go/util/proto" + session "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" + goproto "google.golang.org/protobuf/proto" ) const ( @@ -263,6 +265,17 @@ func (c *ObjectSessionContext) StableSize() (size int) { return size } +func (c *ObjectSessionContext) Unmarshal(data []byte) error { + m := new(session.ObjectSessionContext) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *c = *ObjectSessionContextFromGRPCMessage(m) + + return nil +} + func (t *SessionTokenBody) 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 8e35d86..8fd889e 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -78,16 +78,14 @@ func TestTokenLifetime_StableMarshal(t *testing.T) { func TestObjectSessionContext_StableMarshal(t *testing.T) { objectCtxFrom := generateObjectCtx("Object ID") - transport := new(grpc.ObjectSessionContext) t.Run("non empty", func(t *testing.T) { wire, err := objectCtxFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + objectCtxTo := new(session.ObjectSessionContext) + require.NoError(t, objectCtxTo.Unmarshal(wire)) - objectCtxTo := session.ObjectSessionContextFromGRPCMessage(transport) require.Equal(t, objectCtxFrom, objectCtxTo) }) }