diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index 7d52c60a..2f3f265e 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -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 diff --git a/v2/refs/marshal_test.go b/v2/refs/marshal_test.go index f4e13508..5ba39942 100644 --- a/v2/refs/marshal_test.go +++ b/v2/refs/marshal_test.go @@ -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) +}