forked from TrueCloudLab/frostfs-sdk-go
[#344] netmap: Add method Clone
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
This commit is contained in:
parent
f70c0c9081
commit
749b4e9ab5
6 changed files with 141 additions and 0 deletions
|
@ -1,6 +1,9 @@
|
|||
package netmap
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
||||
)
|
||||
|
@ -382,6 +385,18 @@ func (a *Attribute) SetParents(parent []string) {
|
|||
a.parents = parent
|
||||
}
|
||||
|
||||
// Clone returns a copy of Attribute.
|
||||
func (a *Attribute) Clone() *Attribute {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
return &Attribute{
|
||||
parents: slices.Clone(a.parents),
|
||||
value: a.value,
|
||||
key: a.key,
|
||||
}
|
||||
}
|
||||
|
||||
func (ni *NodeInfo) GetPublicKey() []byte {
|
||||
if ni != nil {
|
||||
return ni.publicKey
|
||||
|
@ -465,6 +480,23 @@ func (ni *NodeInfo) SetState(state NodeState) {
|
|||
ni.state = state
|
||||
}
|
||||
|
||||
// Clone returns a copy of NodeInfo.
|
||||
func (ni *NodeInfo) Clone() *NodeInfo {
|
||||
if ni == nil {
|
||||
return nil
|
||||
}
|
||||
dst := NodeInfo{
|
||||
addresses: slices.Clone(ni.addresses),
|
||||
publicKey: bytes.Clone(ni.publicKey),
|
||||
state: ni.state,
|
||||
attributes: make([]Attribute, len(ni.attributes)),
|
||||
}
|
||||
for i, v := range ni.attributes {
|
||||
dst.attributes[i] = *v.Clone()
|
||||
}
|
||||
return &dst
|
||||
}
|
||||
|
||||
func (l *LocalNodeInfoResponseBody) GetVersion() *refs.Version {
|
||||
if l != nil {
|
||||
return l.version
|
||||
|
|
48
api/netmap/types_test.go
Normal file
48
api/netmap/types_test.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package netmap
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNodeInfo_Clone(t *testing.T) {
|
||||
var ni NodeInfo
|
||||
ni.publicKey = []byte{2}
|
||||
attr := Attribute{
|
||||
key: "key",
|
||||
value: "value",
|
||||
parents: []string{"parent", "parent2"},
|
||||
}
|
||||
ni.attributes = []Attribute{attr}
|
||||
ni.addresses = []string{"5", "6"}
|
||||
|
||||
c := ni.Clone()
|
||||
|
||||
require.True(t, c != &ni)
|
||||
require.True(t, bytes.Equal(c.publicKey, ni.publicKey))
|
||||
require.True(t, &(c.publicKey[0]) != &(ni.publicKey[0]))
|
||||
require.True(t, &(c.attributes[0]) != &(ni.attributes[0]))
|
||||
require.True(t, slices.Compare(c.addresses, ni.addresses) == 0)
|
||||
require.True(t, &(c.addresses[0]) != &(ni.addresses[0]))
|
||||
}
|
||||
|
||||
func TestAttribute_Clone(t *testing.T) {
|
||||
attr := Attribute{
|
||||
key: "key",
|
||||
value: "value",
|
||||
parents: []string{"parent1", "parent2"},
|
||||
}
|
||||
|
||||
c := attr.Clone()
|
||||
|
||||
require.True(t, c != &attr)
|
||||
require.True(t, c.key == attr.key)
|
||||
require.True(t, &(c.key) != &(attr.key))
|
||||
require.True(t, &(c.value) != &(attr.value))
|
||||
require.True(t, c.value == attr.value)
|
||||
require.True(t, &(c.parents[0]) != &(attr.parents[0]))
|
||||
require.True(t, slices.Compare(c.parents, attr.parents) == 0)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue