[#132] v2/refs: Implement stable unmarshaler on Address
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
9cba1e233d
commit
104604acae
2 changed files with 43 additions and 9 deletions
|
@ -2,6 +2,7 @@ package refs
|
|||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-api-go/util/proto"
|
||||
refs "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -140,6 +141,21 @@ func (a *Address) StableSize() (size int) {
|
|||
return size
|
||||
}
|
||||
|
||||
func (a *Address) StableUnmarshal(data []byte) error {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
addrGRPC := new(refs.Address)
|
||||
if err := addrGRPC.Unmarshal(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*a = *AddressFromGRPCMessage(addrGRPC)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Checksum) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if c == nil {
|
||||
return []byte{}, nil
|
||||
|
|
|
@ -63,20 +63,14 @@ func TestObjectID_StableMarshal(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAddress_StableMarshal(t *testing.T) {
|
||||
addressFrom := new(refs.Address)
|
||||
cid := []byte("Container ID")
|
||||
oid := []byte("Object ID")
|
||||
|
||||
cnr := new(refs.ContainerID)
|
||||
cnr.SetValue([]byte("Container ID"))
|
||||
|
||||
objectID := new(refs.ObjectID)
|
||||
objectID.SetValue([]byte("Object ID"))
|
||||
addressFrom := generateAddress(cid, oid)
|
||||
|
||||
addressTransport := new(grpc.Address)
|
||||
|
||||
t.Run("non empty", func(t *testing.T) {
|
||||
addressFrom.SetContainerID(cnr)
|
||||
addressFrom.SetObjectID(objectID)
|
||||
|
||||
wire, err := addressFrom.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
@ -154,3 +148,27 @@ func generateVersion(maj, min uint32) *refs.Version {
|
|||
|
||||
return version
|
||||
}
|
||||
|
||||
func generateAddress(bCid, bOid []byte) *refs.Address {
|
||||
addr := new(refs.Address)
|
||||
|
||||
cid := new(refs.ContainerID)
|
||||
cid.SetValue(bCid)
|
||||
|
||||
oid := new(refs.ObjectID)
|
||||
oid.SetValue(bOid)
|
||||
|
||||
return addr
|
||||
}
|
||||
|
||||
func TestAddress_StableUnmarshal(t *testing.T) {
|
||||
addr := generateAddress([]byte("container id"), []byte("object id"))
|
||||
|
||||
data, err := addr.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr2 := new(refs.Address)
|
||||
require.NoError(t, addr2.StableUnmarshal(data))
|
||||
|
||||
require.Equal(t, addr, addr2)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue