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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 16:04:09 +03:00 committed by Alex Vanin
parent 4ce751f13c
commit 7289ff6c64
6 changed files with 102 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.ObjectID)(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.ObjectID)(id).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of ID.
func (id *ID) Unmarshal(data []byte) error {
return (*refs.ObjectID)(id).
Unmarshal(data)
}
// MarshalJSON encodes ID to protobuf JSON format.
func (id *ID) MarshalJSON() ([]byte, error) {
return (*refs.ObjectID)(id).
MarshalJSON()
}
// UnmarshalJSON decodes ID from protobuf JSON format.
func (id *ID) UnmarshalJSON(data []byte) error {
return (*refs.ObjectID)(id).
UnmarshalJSON(data)
}

View file

@ -83,3 +83,27 @@ func TestID_String(t *testing.T) {
}
})
}
func TestObjectIDEncoding(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

@ -24,3 +24,23 @@ func (a *Address) UnmarshalJSON(data []byte) error {
return nil
}
func (o *ObjectID) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
ObjectIDToGRPCMessage(o),
)
}
func (o *ObjectID) UnmarshalJSON(data []byte) error {
msg := new(refs.ObjectID)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*o = *ObjectIDFromGRPCMessage(msg)
return nil
}

View file

@ -18,3 +18,16 @@ func TestAddressJSON(t *testing.T) {
require.Equal(t, a, a2)
}
func TestObjectIDJSON(t *testing.T) {
o := new(refs.ObjectID)
o.SetValue([]byte{1})
data, err := o.MarshalJSON()
require.NoError(t, err)
o2 := new(refs.ObjectID)
require.NoError(t, o2.UnmarshalJSON(data))
require.Equal(t, o, o2)
}

View file

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

View file

@ -47,7 +47,6 @@ func TestContainerID_StableMarshal(t *testing.T) {
func TestObjectID_StableMarshal(t *testing.T) {
objectIDFrom := new(refs.ObjectID)
objectIDTransport := new(grpc.ObjectID)
t.Run("non empty", func(t *testing.T) {
objectIDFrom.SetValue([]byte("Object ID"))
@ -55,10 +54,9 @@ func TestObjectID_StableMarshal(t *testing.T) {
wire, err := objectIDFrom.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, objectIDTransport)
require.NoError(t, err)
objectIDTo := new(refs.ObjectID)
require.NoError(t, objectIDTo.Unmarshal(wire))
objectIDTo := refs.ObjectIDFromGRPCMessage(objectIDTransport)
require.Equal(t, objectIDFrom, objectIDTo)
})
}