From c0f3ac51d464dfcade85b8492711fbd034e9518b Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 16:07:32 +0300 Subject: [PATCH] [#168] refs: Implement binary/JSON encoders/decoders on ContainerID Signed-off-by: Leonard Lyubich --- pkg/container/id.go | 32 ++++++++++++++++++++++++++++++++ pkg/container/id_test.go | 25 +++++++++++++++++++++++++ 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, 103 insertions(+), 4 deletions(-) diff --git a/pkg/container/id.go b/pkg/container/id.go index 2649deff..659828fd 100644 --- a/pkg/container/id.go +++ b/pkg/container/id.go @@ -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) +} diff --git a/pkg/container/id_test.go b/pkg/container/id_test.go index 3942a708..33469751 100644 --- a/pkg/container/id_test.go +++ b/pkg/container/id_test.go @@ -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) + }) +} diff --git a/v2/refs/json.go b/v2/refs/json.go index 37cb6f37..d2fd4edf 100644 --- a/v2/refs/json.go +++ b/v2/refs/json.go @@ -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 +} diff --git a/v2/refs/json_test.go b/v2/refs/json_test.go index 196dc6ca..b56b1d09 100644 --- a/v2/refs/json_test.go +++ b/v2/refs/json_test.go @@ -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) +} diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index e860b045..37b950d1 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -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 diff --git a/v2/refs/marshal_test.go b/v2/refs/marshal_test.go index 268dc283..d0eb75f4 100644 --- a/v2/refs/marshal_test.go +++ b/v2/refs/marshal_test.go @@ -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) }) }