Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Parameterized constructor to the attribute #528

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions netmap/network_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"math"
"testing"

"github.com/nspcc-dev/neofs-api-go/v2/netmap"
. "github.com/nspcc-dev/neofs-sdk-go/netmap"
netmapv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test"
"github.com/stretchr/testify/require"
)

func TestNetworkInfo_CurrentEpoch(t *testing.T) {
var x NetworkInfo
var x netmap.NetworkInfo

require.Zero(t, x.CurrentEpoch())

Expand All @@ -22,14 +22,14 @@ func TestNetworkInfo_CurrentEpoch(t *testing.T) {

require.EqualValues(t, e, x.CurrentEpoch())

var m netmap.NetworkInfo
var m netmapv2.NetworkInfo
x.WriteToV2(&m)

require.EqualValues(t, e, m.GetCurrentEpoch())
}

func TestNetworkInfo_MagicNumber(t *testing.T) {
var x NetworkInfo
var x netmap.NetworkInfo

require.Zero(t, x.MagicNumber())

Expand All @@ -39,14 +39,14 @@ func TestNetworkInfo_MagicNumber(t *testing.T) {

require.EqualValues(t, magic, x.MagicNumber())

var m netmap.NetworkInfo
var m netmapv2.NetworkInfo
x.WriteToV2(&m)

require.EqualValues(t, magic, m.GetMagicNumber())
}

func TestNetworkInfo_MsPerBlock(t *testing.T) {
var x NetworkInfo
var x netmap.NetworkInfo

require.Zero(t, x.MsPerBlock())

Expand All @@ -56,31 +56,31 @@ func TestNetworkInfo_MsPerBlock(t *testing.T) {

require.EqualValues(t, ms, x.MsPerBlock())

var m netmap.NetworkInfo
var m netmapv2.NetworkInfo
x.WriteToV2(&m)

require.EqualValues(t, ms, m.GetMsPerBlock())
}

func testConfigValue(t *testing.T,
getter func(x NetworkInfo) any,
setter func(x *NetworkInfo, val any),
getter func(x netmap.NetworkInfo) any,
setter func(x *netmap.NetworkInfo, val any),
val1, val2 any,
v2Key string, v2Val func(val any) []byte,
) {
var x NetworkInfo
var x netmap.NetworkInfo

require.Zero(t, getter(x))

checkVal := func(exp any) {
require.EqualValues(t, exp, getter(x))

var m netmap.NetworkInfo
var m netmapv2.NetworkInfo
x.WriteToV2(&m)

require.EqualValues(t, 1, m.GetNetworkConfig().NumberOfParameters())
found := false
m.GetNetworkConfig().IterateParameters(func(prm *netmap.NetworkParameter) bool {
m.GetNetworkConfig().IterateParameters(func(prm *netmapv2.NetworkParameter) bool {
require.False(t, found)
require.Equal(t, []byte(v2Key), prm.GetKey())
require.Equal(t, v2Val(exp), prm.GetValue())
Expand All @@ -99,8 +99,8 @@ func testConfigValue(t *testing.T,

func TestNetworkInfo_AuditFee(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.AuditFee() },
func(info *NetworkInfo, val any) { info.SetAuditFee(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.AuditFee() },
func(info *netmap.NetworkInfo, val any) { info.SetAuditFee(val.(uint64)) },
uint64(1), uint64(2),
"AuditFee", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -112,8 +112,8 @@ func TestNetworkInfo_AuditFee(t *testing.T) {

func TestNetworkInfo_StoragePrice(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.StoragePrice() },
func(info *NetworkInfo, val any) { info.SetStoragePrice(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.StoragePrice() },
func(info *netmap.NetworkInfo, val any) { info.SetStoragePrice(val.(uint64)) },
uint64(1), uint64(2),
"BasicIncomeRate", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -125,8 +125,8 @@ func TestNetworkInfo_StoragePrice(t *testing.T) {

func TestNetworkInfo_ContainerFee(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.ContainerFee() },
func(info *NetworkInfo, val any) { info.SetContainerFee(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.ContainerFee() },
func(info *netmap.NetworkInfo, val any) { info.SetContainerFee(val.(uint64)) },
uint64(1), uint64(2),
"ContainerFee", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -138,8 +138,8 @@ func TestNetworkInfo_ContainerFee(t *testing.T) {

func TestNetworkInfo_NamedContainerFee(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.NamedContainerFee() },
func(info *NetworkInfo, val any) { info.SetNamedContainerFee(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.NamedContainerFee() },
func(info *netmap.NetworkInfo, val any) { info.SetNamedContainerFee(val.(uint64)) },
uint64(1), uint64(2),
"ContainerAliasFee", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -151,8 +151,8 @@ func TestNetworkInfo_NamedContainerFee(t *testing.T) {

func TestNetworkInfo_EigenTrustAlpha(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.EigenTrustAlpha() },
func(info *NetworkInfo, val any) { info.SetEigenTrustAlpha(val.(float64)) },
func(x netmap.NetworkInfo) any { return x.EigenTrustAlpha() },
func(info *netmap.NetworkInfo, val any) { info.SetEigenTrustAlpha(val.(float64)) },
0.1, 0.2,
"EigenTrustAlpha", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -164,8 +164,8 @@ func TestNetworkInfo_EigenTrustAlpha(t *testing.T) {

func TestNetworkInfo_NumberOfEigenTrustIterations(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.NumberOfEigenTrustIterations() },
func(info *NetworkInfo, val any) { info.SetNumberOfEigenTrustIterations(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.NumberOfEigenTrustIterations() },
func(info *netmap.NetworkInfo, val any) { info.SetNumberOfEigenTrustIterations(val.(uint64)) },
uint64(1), uint64(2),
"EigenTrustIterations", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -177,8 +177,8 @@ func TestNetworkInfo_NumberOfEigenTrustIterations(t *testing.T) {

func TestNetworkInfo_IRCandidateFee(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.IRCandidateFee() },
func(info *NetworkInfo, val any) { info.SetIRCandidateFee(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.IRCandidateFee() },
func(info *netmap.NetworkInfo, val any) { info.SetIRCandidateFee(val.(uint64)) },
uint64(1), uint64(2),
"InnerRingCandidateFee", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -190,8 +190,8 @@ func TestNetworkInfo_IRCandidateFee(t *testing.T) {

func TestNetworkInfo_MaxObjectSize(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.MaxObjectSize() },
func(info *NetworkInfo, val any) { info.SetMaxObjectSize(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.MaxObjectSize() },
func(info *netmap.NetworkInfo, val any) { info.SetMaxObjectSize(val.(uint64)) },
uint64(1), uint64(2),
"MaxObjectSize", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -203,8 +203,8 @@ func TestNetworkInfo_MaxObjectSize(t *testing.T) {

func TestNetworkInfo_WithdrawalFee(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.WithdrawalFee() },
func(info *NetworkInfo, val any) { info.SetWithdrawalFee(val.(uint64)) },
func(x netmap.NetworkInfo) any { return x.WithdrawalFee() },
func(info *netmap.NetworkInfo, val any) { info.SetWithdrawalFee(val.(uint64)) },
uint64(1), uint64(2),
"WithdrawFee", func(val any) []byte {
data := make([]byte, 8)
Expand All @@ -216,8 +216,8 @@ func TestNetworkInfo_WithdrawalFee(t *testing.T) {

func TestNetworkInfo_HomomorphicHashingDisabled(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.HomomorphicHashingDisabled() },
func(info *NetworkInfo, val any) {
func(x netmap.NetworkInfo) any { return x.HomomorphicHashingDisabled() },
func(info *netmap.NetworkInfo, val any) {
if val.(bool) {
info.DisableHomomorphicHashing()
}
Expand All @@ -237,8 +237,8 @@ func TestNetworkInfo_HomomorphicHashingDisabled(t *testing.T) {

func TestNetworkInfo_MaintenanceModeAllowed(t *testing.T) {
testConfigValue(t,
func(x NetworkInfo) any { return x.MaintenanceModeAllowed() },
func(info *NetworkInfo, val any) {
func(x netmap.NetworkInfo) any { return x.MaintenanceModeAllowed() },
func(info *netmap.NetworkInfo, val any) {
if val.(bool) {
info.AllowMaintenanceMode()
}
Expand All @@ -256,7 +256,7 @@ func TestNetworkInfo_MaintenanceModeAllowed(t *testing.T) {
func TestNetworkInfo_Marshal(t *testing.T) {
v := netmaptest.NetworkInfo()

var v2 NetworkInfo
var v2 netmap.NetworkInfo
require.NoError(t, v2.Unmarshal(v.Marshal()))

require.Equal(t, v, v2)
Expand Down
8 changes: 4 additions & 4 deletions netmap/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
"testing"

. "github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test"
"github.com/stretchr/testify/require"
)
Expand All @@ -25,7 +25,7 @@ SELECT 1 IN City FROM SPBSSD AS SPB
FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`,
}

var p PlacementPolicy
var p netmap.PlacementPolicy

for _, testCase := range testCases {
require.NoError(t, p.DecodeString(testCase))
Expand All @@ -49,7 +49,7 @@ func TestPlacementPolicyEncoding(t *testing.T) {
v := netmaptest.PlacementPolicy()

t.Run("binary", func(t *testing.T) {
var v2 PlacementPolicy
var v2 netmap.PlacementPolicy
require.NoError(t, v2.Unmarshal(v.Marshal()))

require.Equal(t, v, v2)
Expand All @@ -59,7 +59,7 @@ func TestPlacementPolicyEncoding(t *testing.T) {
data, err := v.MarshalJSON()
require.NoError(t, err)

var v2 PlacementPolicy
var v2 netmap.PlacementPolicy
require.NoError(t, v2.UnmarshalJSON(data))

require.Equal(t, v, v2)
Expand Down
16 changes: 7 additions & 9 deletions object/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ func NewAttributeFromV2(aV2 *object.Attribute) *Attribute {
return (*Attribute)(aV2)
}

// NewAttribute creates and initializes blank [Attribute].
//
// Works similar as NewAttributeFromV2(new(Attribute)).
//
// Defaults:
// - key: "";
// - value: "".
func NewAttribute() *Attribute {
return NewAttributeFromV2(new(object.Attribute))
// NewAttribute creates and initializes new [Attribute].
func NewAttribute(key, value string) *Attribute {
attr := new(object.Attribute)
attr.SetKey(key)
attr.SetValue(value)

return NewAttributeFromV2(attr)
}

// Key returns key to the object attribute.
Expand Down
27 changes: 18 additions & 9 deletions object/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
func TestAttribute(t *testing.T) {
key, val := "some key", "some value"

a := NewAttribute()
a.SetKey(key)
a.SetValue(val)
a := NewAttribute(key, val)

require.Equal(t, key, a.Key())
require.Equal(t, val, a.Value())
Expand All @@ -24,15 +22,13 @@ func TestAttribute(t *testing.T) {
}

func TestAttributeEncoding(t *testing.T) {
a := NewAttribute()
a.SetKey("key")
a.SetValue("value")
a := NewAttribute("key", "value")

t.Run("binary", func(t *testing.T) {
data, err := a.Marshal()
require.NoError(t, err)

a2 := NewAttribute()
a2 := NewAttribute("", "")
require.NoError(t, a2.Unmarshal(data))

require.Equal(t, a, a2)
Expand All @@ -42,7 +38,7 @@ func TestAttributeEncoding(t *testing.T) {
data, err := a.MarshalJSON()
require.NoError(t, err)

a2 := NewAttribute()
a2 := NewAttribute("", "")
require.NoError(t, a2.UnmarshalJSON(data))

require.Equal(t, a, a2)
Expand All @@ -67,7 +63,7 @@ func TestAttribute_ToV2(t *testing.T) {

func TestNewAttribute(t *testing.T) {
t.Run("default values", func(t *testing.T) {
a := NewAttribute()
a := NewAttribute("", "")

// check initial values
require.Empty(t, a.Key())
Expand All @@ -79,4 +75,17 @@ func TestNewAttribute(t *testing.T) {
require.Empty(t, aV2.GetKey())
require.Empty(t, aV2.GetValue())
})

t.Run("pre installed key and value", func(t *testing.T) {
a := NewAttribute("key", "value")

require.NotEmpty(t, a.Key())
require.NotEmpty(t, a.Value())

// convert to v2 message
aV2 := a.ToV2()

require.NotEmpty(t, aV2.GetKey())
require.NotEmpty(t, aV2.GetValue())
})
}
5 changes: 1 addition & 4 deletions object/test/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ func Range() object.Range {

// Attribute returns random object.Attribute.
func Attribute() object.Attribute {
x := object.NewAttribute()

x.SetKey("key")
x.SetValue("value")
x := object.NewAttribute("key", "value")

return *x
}
Expand Down
Loading
Loading