From 8ea999357771723465a01607f3d88a6edafb582e Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Wed, 16 Jun 2021 22:04:14 +0300 Subject: [PATCH] [#309] pkg/session: Add marshal-unmarshal Added Marshal, Unmarshal, MarshalJSON, UnmarshalJSON methods to ContainerContext Signed-off-by: Angira Kekteeva --- pkg/session/container.go | 24 ++++++++++++++++++++++++ pkg/session/container_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/pkg/session/container.go b/pkg/session/container.go index bcf5c3d3..a3d2d91c 100644 --- a/pkg/session/container.go +++ b/pkg/session/container.go @@ -113,3 +113,27 @@ func (x *ContainerContext) ForSetEACL() { func (x *ContainerContext) IsForSetEACL() bool { return x.isForVerb(session.ContainerVerbSetEACL) } + +// Marshal marshals ContainerContext into a protobuf binary form. +func (x *ContainerContext) Marshal(bs ...[]byte) ([]byte, error) { + var buf []byte + if len(bs) > 0 { + buf = bs[0] + } + return x.ToV2().StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of ContainerContext. +func (x *ContainerContext) Unmarshal(data []byte) error { + return x.ToV2().Unmarshal(data) +} + +// MarshalJSON encodes ContainerContext to protobuf JSON format. +func (x *ContainerContext) MarshalJSON() ([]byte, error) { + return x.ToV2().MarshalJSON() +} + +// UnmarshalJSON decodes ContainerContext from protobuf JSON format. +func (x *ContainerContext) UnmarshalJSON(data []byte) error { + return x.ToV2().UnmarshalJSON(data) +} diff --git a/pkg/session/container_test.go b/pkg/session/container_test.go index 6dd2c1fa..38c97eea 100644 --- a/pkg/session/container_test.go +++ b/pkg/session/container_test.go @@ -5,6 +5,7 @@ import ( cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test" "github.com/nspcc-dev/neofs-api-go/pkg/session" + sessiontest "github.com/nspcc-dev/neofs-api-go/pkg/session/test" v2session "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/stretchr/testify/require" ) @@ -85,3 +86,27 @@ func TestFilter_ToV2(t *testing.T) { require.Nil(t, cV2.ContainerID()) }) } + +func TestContainerContextEncoding(t *testing.T) { + c := sessiontest.ContainerContext() + + t.Run("binary", func(t *testing.T) { + data, err := c.Marshal() + require.NoError(t, err) + + c2 := session.NewContainerContext() + require.NoError(t, c2.Unmarshal(data)) + + require.Equal(t, c, c2) + }) + + t.Run("json", func(t *testing.T) { + data, err := c.MarshalJSON() + require.NoError(t, err) + + c2 := session.NewContainerContext() + require.NoError(t, c2.UnmarshalJSON(data)) + + require.Equal(t, c, c2) + }) +}