From 7289ff6c6458b41302193691646400d6f3d062e1 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 16:04:09 +0300 Subject: [PATCH] [#168] refs: Implement binary/JSON encoders/decoders on ObjectID Signed-off-by: Leonard Lyubich --- pkg/object/id.go | 32 ++++++++++++++++++++++++++++++++ pkg/object/id_test.go | 24 ++++++++++++++++++++++++ v2/refs/json.go | 20 ++++++++++++++++++++ v2/refs/json_test.go | 13 +++++++++++++ v2/refs/marshal.go | 11 +++++++++++ v2/refs/marshal_test.go | 6 ++---- 6 files changed, 102 insertions(+), 4 deletions(-) diff --git a/pkg/object/id.go b/pkg/object/id.go index 1fcb595..c62451d 100644 --- a/pkg/object/id.go +++ b/pkg/object/id.go @@ -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) +} diff --git a/pkg/object/id_test.go b/pkg/object/id_test.go index e56ff96..14ab6c6 100644 --- a/pkg/object/id_test.go +++ b/pkg/object/id_test.go @@ -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) + }) +} diff --git a/v2/refs/json.go b/v2/refs/json.go index f8c20d6..37cb6f3 100644 --- a/v2/refs/json.go +++ b/v2/refs/json.go @@ -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 +} diff --git a/v2/refs/json_test.go b/v2/refs/json_test.go index ce40de7..196dc6c 100644 --- a/v2/refs/json_test.go +++ b/v2/refs/json_test.go @@ -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) +} diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index c39ce81..e860b04 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -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 diff --git a/v2/refs/marshal_test.go b/v2/refs/marshal_test.go index 3d8bfac..268dc28 100644 --- a/v2/refs/marshal_test.go +++ b/v2/refs/marshal_test.go @@ -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) }) }