[#309] pkg/session: Add marshal-unmarshal

Added Marshal, Unmarshal, MarshalJSON, UnmarshalJSON methods
to ContainerContext

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
Angira Kekteeva 2021-06-16 22:04:14 +03:00 committed by Leonard Lyubich
parent 86d446f54c
commit 8ea9993577
2 changed files with 49 additions and 0 deletions

View file

@ -113,3 +113,27 @@ func (x *ContainerContext) ForSetEACL() {
func (x *ContainerContext) IsForSetEACL() bool { func (x *ContainerContext) IsForSetEACL() bool {
return x.isForVerb(session.ContainerVerbSetEACL) 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)
}

View file

@ -5,6 +5,7 @@ import (
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test" cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
"github.com/nspcc-dev/neofs-api-go/pkg/session" "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" v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -85,3 +86,27 @@ func TestFilter_ToV2(t *testing.T) {
require.Nil(t, cV2.ContainerID()) 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)
})
}