From 55948c2ab16a78f32e49304e8a03b3dae99d2588 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 17:20:24 +0300 Subject: [PATCH] [#168] storageGroup: Implement binary/JSON encoders/decoders on SG Signed-off-by: Leonard Lyubich --- v2/storagegroup/json.go | 26 ++++++++++++++++++++++ v2/storagegroup/json_test.go | 20 +++++++++++++++++ v2/storagegroup/marshal.go | 13 +++++++++++ v2/storagegroup/marshal_test.go | 38 +++++++++++++++++---------------- 4 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 v2/storagegroup/json.go create mode 100644 v2/storagegroup/json_test.go diff --git a/v2/storagegroup/json.go b/v2/storagegroup/json.go new file mode 100644 index 0000000..efdb311 --- /dev/null +++ b/v2/storagegroup/json.go @@ -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 +} diff --git a/v2/storagegroup/json_test.go b/v2/storagegroup/json_test.go new file mode 100644 index 0000000..9da4920 --- /dev/null +++ b/v2/storagegroup/json_test.go @@ -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) +} diff --git a/v2/storagegroup/marshal.go b/v2/storagegroup/marshal.go index 33b0c15..bfcb896 100644 --- a/v2/storagegroup/marshal.go +++ b/v2/storagegroup/marshal.go @@ -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 +} diff --git a/v2/storagegroup/marshal_test.go b/v2/storagegroup/marshal_test.go index f3f79b8..15f12c9 100644 --- a/v2/storagegroup/marshal_test.go +++ b/v2/storagegroup/marshal_test.go @@ -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 +}