[#168] session: Implement binary/JSON encoders/decoders on SessionContext
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
93df2fd765
commit
86351a8f90
4 changed files with 61 additions and 4 deletions
26
v2/session/json.go
Normal file
26
v2/session/json.go
Normal 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
20
v2/session/json_test.go
Normal 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)
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue