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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 16:07:32 +03:00 committed by Alex Vanin
parent 7289ff6c64
commit c0f3ac51d4
6 changed files with 103 additions and 4 deletions

View file

@ -65,3 +65,35 @@ func (id *ID) Parse(s string) error {
func (id *ID) String() string {
return base58.Encode((*refs.ContainerID)(id).GetValue())
}
// 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.ContainerID)(id).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of ID.
func (id *ID) Unmarshal(data []byte) error {
return (*refs.ContainerID)(id).
Unmarshal(data)
}
// MarshalJSON encodes ID to protobuf JSON format.
func (id *ID) MarshalJSON() ([]byte, error) {
return (*refs.ContainerID)(id).
MarshalJSON()
}
// UnmarshalJSON decodes ID from protobuf JSON format.
func (id *ID) UnmarshalJSON(data []byte) error {
return (*refs.ContainerID)(id).
UnmarshalJSON(data)
}

View file

@ -90,3 +90,28 @@ func TestID_String(t *testing.T) {
}
})
}
func TestContainerIDEncoding(t *testing.T) {
id := NewID()
id.SetSHA256(randSHA256Checksum(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

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

View file

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

View file

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

View file

@ -29,7 +29,6 @@ func TestOwnerID_StableMarshal(t *testing.T) {
func TestContainerID_StableMarshal(t *testing.T) {
cnrFrom := new(refs.ContainerID)
cnrTransport := new(grpc.ContainerID)
t.Run("non empty", func(t *testing.T) {
cnrFrom.SetValue([]byte("Container ID"))
@ -37,10 +36,9 @@ func TestContainerID_StableMarshal(t *testing.T) {
wire, err := cnrFrom.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, cnrTransport)
require.NoError(t, err)
cnrTo := new(refs.ContainerID)
require.NoError(t, cnrTo.Unmarshal(wire))
cnrTo := refs.ContainerIDFromGRPCMessage(cnrTransport)
require.Equal(t, cnrFrom, cnrTo)
})
}