From 6a29d9c360a9baa52ddfa1c21020fdd7dedb7af9 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 5 Nov 2020 13:28:29 +0300 Subject: [PATCH] [#189] sdk/netmap: Refactor Replica type Replace alias to v2 type Replica with v2-compatible implementation. Signed-off-by: Leonard Lyubich --- pkg/netmap/policy.go | 1 - pkg/netmap/replica.go | 47 ++++++++++++++++++++++++++++++++++++++ pkg/netmap/replica_test.go | 36 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 pkg/netmap/replica.go create mode 100644 pkg/netmap/replica_test.go diff --git a/pkg/netmap/policy.go b/pkg/netmap/policy.go index b0e46d6e..cde2a162 100644 --- a/pkg/netmap/policy.go +++ b/pkg/netmap/policy.go @@ -6,7 +6,6 @@ import ( // fixme: make types instead of aliases to v2 structures type PlacementPolicy = netmap.PlacementPolicy -type Replica = netmap.Replica func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) { return netmap.PlacementPolicyToJSON(p) diff --git a/pkg/netmap/replica.go b/pkg/netmap/replica.go new file mode 100644 index 00000000..153a2691 --- /dev/null +++ b/pkg/netmap/replica.go @@ -0,0 +1,47 @@ +package netmap + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/netmap" +) + +// Replica represents v2-compatible object replica descriptor. +type Replica netmap.Replica + +// NewReplica creates and returns new Replica instance. +func NewReplica() *Replica { + return NewReplicaFromV2(new(netmap.Replica)) +} + +// NewReplicaFromV2 converts v2 Replica to Replica. +func NewReplicaFromV2(f *netmap.Replica) *Replica { + return (*Replica)(f) +} + +// ToV2 converts Replica to v2 Replica. +func (r *Replica) ToV2() *netmap.Replica { + return (*netmap.Replica)(r) +} + +// Count returns number of object replicas. +func (r *Replica) Count() uint32 { + return (*netmap.Replica)(r). + GetCount() +} + +// SetCount sets number of object replicas. +func (r *Replica) SetCount(c uint32) { + (*netmap.Replica)(r). + SetCount(c) +} + +// Selector returns name of selector bucket to put replicas. +func (r *Replica) Selector() string { + return (*netmap.Replica)(r). + GetSelector() +} + +// SetSelector sets name of selector bucket to put replicas. +func (r *Replica) SetSelector(s string) { + (*netmap.Replica)(r). + SetSelector(s) +} diff --git a/pkg/netmap/replica_test.go b/pkg/netmap/replica_test.go new file mode 100644 index 00000000..b5d4dc05 --- /dev/null +++ b/pkg/netmap/replica_test.go @@ -0,0 +1,36 @@ +package netmap + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/netmap" + "github.com/stretchr/testify/require" +) + +func TestReplicaFromV2(t *testing.T) { + rV2 := new(netmap.Replica) + rV2.SetCount(3) + rV2.SetSelector("selector") + + r := NewReplicaFromV2(rV2) + + require.Equal(t, rV2, r.ToV2()) +} + +func TestReplica_Count(t *testing.T) { + r := NewReplica() + c := uint32(3) + + r.SetCount(c) + + require.Equal(t, c, r.Count()) +} + +func TestReplica_Selector(t *testing.T) { + r := NewReplica() + s := "some selector" + + r.SetSelector(s) + + require.Equal(t, s, r.Selector()) +}