[#168] refs: Implement binary/JSON encoders/decoders on OwnerID

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 16:14:00 +03:00 committed by Alex Vanin
parent c0f3ac51d4
commit 9ec57ee9f8
6 changed files with 110 additions and 6 deletions

View file

@ -71,3 +71,35 @@ func (id *ID) Parse(s string) error {
return nil return nil
} }
// Marshal marshals ID into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (id *ID) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*refs.OwnerID)(id).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of ID.
func (id *ID) Unmarshal(data []byte) error {
return (*refs.OwnerID)(id).
Unmarshal(data)
}
// MarshalJSON encodes ID to protobuf JSON format.
func (id *ID) MarshalJSON() ([]byte, error) {
return (*refs.OwnerID)(id).
MarshalJSON()
}
// UnmarshalJSON decodes ID from protobuf JSON format.
func (id *ID) UnmarshalJSON(data []byte) error {
return (*refs.OwnerID)(id).
UnmarshalJSON(data)
}

View file

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestIDV2(t *testing.T) { func randID(t *testing.T) *ID {
id := NewID() id := NewID()
wallet := new(NEO3Wallet) wallet := new(NEO3Wallet)
@ -20,9 +20,15 @@ func TestIDV2(t *testing.T) {
id.SetNeo3Wallet(wallet) id.SetNeo3Wallet(wallet)
return id
}
func TestIDV2(t *testing.T) {
id := randID(t)
idV2 := id.ToV2() idV2 := id.ToV2()
require.Equal(t, wallet.Bytes(), idV2.GetValue()) require.Equal(t, id, NewIDFromV2(idV2))
} }
func TestNewIDFromNeo3Wallet(t *testing.T) { func TestNewIDFromNeo3Wallet(t *testing.T) {
@ -63,3 +69,27 @@ func TestID_Parse(t *testing.T) {
} }
}) })
} }
func TestIDEncoding(t *testing.T) {
id := randID(t)
t.Run("binary", func(t *testing.T) {
data, err := id.Marshal()
require.NoError(t, err)
id2 := NewID()
require.NoError(t, id2.Unmarshal(data))
require.Equal(t, id, id2)
})
t.Run("json", func(t *testing.T) {
data, err := id.MarshalJSON()
require.NoError(t, err)
a2 := NewID()
require.NoError(t, a2.UnmarshalJSON(data))
require.Equal(t, id, a2)
})
}

View file

@ -64,3 +64,23 @@ func (c *ContainerID) UnmarshalJSON(data []byte) error {
return nil return nil
} }
func (o *OwnerID) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
OwnerIDToGRPCMessage(o),
)
}
func (o *OwnerID) UnmarshalJSON(data []byte) error {
msg := new(refs.OwnerID)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*o = *OwnerIDFromGRPCMessage(msg)
return nil
}

View file

@ -44,3 +44,16 @@ func TestContainerIDJSON(t *testing.T) {
require.Equal(t, cid, cid2) require.Equal(t, cid, cid2)
} }
func TestOwnerIDJSON(t *testing.T) {
o := new(refs.OwnerID)
o.SetValue([]byte{1})
data, err := o.MarshalJSON()
require.NoError(t, err)
o2 := new(refs.OwnerID)
require.NoError(t, o2.UnmarshalJSON(data))
require.Equal(t, o, o2)
}

View file

@ -51,6 +51,17 @@ func (o *OwnerID) StableSize() int {
return proto.BytesSize(ownerIDValField, o.val) return proto.BytesSize(ownerIDValField, o.val)
} }
func (o *OwnerID) Unmarshal(data []byte) error {
m := new(refs.OwnerID)
if err := goproto.Unmarshal(data, m); err != nil {
return err
}
*o = *OwnerIDFromGRPCMessage(m)
return nil
}
func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) { func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) {
if c == nil { if c == nil {
return []byte{}, nil return []byte{}, nil

View file

@ -11,7 +11,6 @@ import (
func TestOwnerID_StableMarshal(t *testing.T) { func TestOwnerID_StableMarshal(t *testing.T) {
ownerFrom := new(refs.OwnerID) ownerFrom := new(refs.OwnerID)
ownerTransport := new(grpc.OwnerID)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
ownerFrom.SetValue([]byte("Owner ID")) ownerFrom.SetValue([]byte("Owner ID"))
@ -19,10 +18,9 @@ func TestOwnerID_StableMarshal(t *testing.T) {
wire, err := ownerFrom.StableMarshal(nil) wire, err := ownerFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
err = goproto.Unmarshal(wire, ownerTransport) ownerTo := new(refs.OwnerID)
require.NoError(t, err) require.NoError(t, ownerTo.Unmarshal(wire))
ownerTo := refs.OwnerIDFromGRPCMessage(ownerTransport)
require.Equal(t, ownerFrom, ownerTo) require.Equal(t, ownerFrom, ownerTo)
}) })
} }