[#168] storageGroup: Implement binary/JSON encoders/decoders on SG

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 17:20:24 +03:00 committed by Alex Vanin
parent 6456fcf8fa
commit 55948c2ab1
4 changed files with 79 additions and 18 deletions

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

@ -0,0 +1,26 @@
package storagegroup
import (
storagegroup "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func (s *StorageGroup) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
StorageGroupToGRPCMessage(s),
)
}
func (s *StorageGroup) UnmarshalJSON(data []byte) error {
msg := new(storagegroup.StorageGroup)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*s = *StorageGroupFromGRPCMessage(msg)
return nil
}

View file

@ -0,0 +1,20 @@
package storagegroup_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/storagegroup"
"github.com/stretchr/testify/require"
)
func TestStorageGroupJSON(t *testing.T) {
sg := generateSG()
data, err := sg.MarshalJSON()
require.NoError(t, err)
sg2 := new(storagegroup.StorageGroup)
require.NoError(t, sg2.UnmarshalJSON(data))
require.Equal(t, sg, sg2)
}

View file

@ -2,6 +2,8 @@ package storagegroup
import (
"github.com/nspcc-dev/neofs-api-go/util/proto"
storagegroup "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/grpc"
goproto "google.golang.org/protobuf/proto"
)
const (
@ -76,3 +78,14 @@ func (s *StorageGroup) StableSize() (size int) {
return size
}
func (s *StorageGroup) Unmarshal(data []byte) error {
m := new(storagegroup.StorageGroup)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*s = *StorageGroupFromGRPCMessage(m)
return nil
}

View file

@ -5,34 +5,19 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-api-go/v2/storagegroup"
grpc "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/grpc"
"github.com/stretchr/testify/require"
goproto "google.golang.org/protobuf/proto"
)
func TestStorageGroup_StableMarshal(t *testing.T) {
ownerID1 := new(refs.ObjectID)
ownerID1.SetValue([]byte("Object ID 1"))
ownerID2 := new(refs.ObjectID)
ownerID2.SetValue([]byte("Object ID 2"))
storageGroupFrom := new(storagegroup.StorageGroup)
transport := new(grpc.StorageGroup)
storageGroupFrom := generateSG()
t.Run("non empty", func(t *testing.T) {
storageGroupFrom.SetValidationDataSize(300)
storageGroupFrom.SetValidationHash(generateChecksum("Homomorphic hash"))
storageGroupFrom.SetExpirationEpoch(100)
storageGroupFrom.SetMembers([]*refs.ObjectID{ownerID1, ownerID2})
wire, err := storageGroupFrom.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, transport)
require.NoError(t, err)
storageGroupTo := new(storagegroup.StorageGroup)
require.NoError(t, storageGroupTo.Unmarshal(wire))
storageGroupTo := storagegroup.StorageGroupFromGRPCMessage(transport)
require.Equal(t, storageGroupFrom, storageGroupTo)
})
}
@ -44,3 +29,20 @@ func generateChecksum(data string) *refs.Checksum {
return checksum
}
func generateSG() *storagegroup.StorageGroup {
sg := new(storagegroup.StorageGroup)
oid1 := new(refs.ObjectID)
oid1.SetValue([]byte("Object ID 1"))
oid2 := new(refs.ObjectID)
oid2.SetValue([]byte("Object ID 2"))
sg.SetValidationDataSize(300)
sg.SetValidationHash(generateChecksum("Homomorphic hash"))
sg.SetExpirationEpoch(100)
sg.SetMembers([]*refs.ObjectID{oid1, oid2})
return sg
}