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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 16:40:00 +03:00 committed by Alex Vanin
parent 93df2fd765
commit 86351a8f90
4 changed files with 61 additions and 4 deletions

26
v2/session/json.go Normal file
View file

@ -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
}

20
v2/session/json_test.go Normal file
View file

@ -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)
}

View file

@ -2,6 +2,8 @@ package session
import ( import (
"github.com/nspcc-dev/neofs-api-go/util/proto" "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 ( const (
@ -263,6 +265,17 @@ func (c *ObjectSessionContext) StableSize() (size int) {
return size 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) { func (t *SessionTokenBody) StableMarshal(buf []byte) ([]byte, error) {
if t == nil { if t == nil {
return []byte{}, nil return []byte{}, nil

View file

@ -78,16 +78,14 @@ func TestTokenLifetime_StableMarshal(t *testing.T) {
func TestObjectSessionContext_StableMarshal(t *testing.T) { func TestObjectSessionContext_StableMarshal(t *testing.T) {
objectCtxFrom := generateObjectCtx("Object ID") objectCtxFrom := generateObjectCtx("Object ID")
transport := new(grpc.ObjectSessionContext)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
wire, err := objectCtxFrom.StableMarshal(nil) wire, err := objectCtxFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
err = goproto.Unmarshal(wire, transport) objectCtxTo := new(session.ObjectSessionContext)
require.NoError(t, err) require.NoError(t, objectCtxTo.Unmarshal(wire))
objectCtxTo := session.ObjectSessionContextFromGRPCMessage(transport)
require.Equal(t, objectCtxFrom, objectCtxTo) require.Equal(t, objectCtxFrom, objectCtxTo)
}) })
} }