[#132] v2/refs: Implement stable unmarshaler on Address

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-02 14:40:20 +03:00 committed by Stanislav Bogatyrev
parent 9cba1e233d
commit 104604acae
2 changed files with 43 additions and 9 deletions

View file

@ -2,6 +2,7 @@ package refs
import ( import (
"github.com/nspcc-dev/neofs-api-go/util/proto" "github.com/nspcc-dev/neofs-api-go/util/proto"
refs "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc"
) )
const ( const (
@ -140,6 +141,21 @@ func (a *Address) StableSize() (size int) {
return size 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) { func (c *Checksum) StableMarshal(buf []byte) ([]byte, error) {
if c == nil { if c == nil {
return []byte{}, nil return []byte{}, nil

View file

@ -63,20 +63,14 @@ func TestObjectID_StableMarshal(t *testing.T) {
} }
func TestAddress_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) addressFrom := generateAddress(cid, oid)
cnr.SetValue([]byte("Container ID"))
objectID := new(refs.ObjectID)
objectID.SetValue([]byte("Object ID"))
addressTransport := new(grpc.Address) addressTransport := new(grpc.Address)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
addressFrom.SetContainerID(cnr)
addressFrom.SetObjectID(objectID)
wire, err := addressFrom.StableMarshal(nil) wire, err := addressFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
@ -154,3 +148,27 @@ func generateVersion(maj, min uint32) *refs.Version {
return 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)
}