From 1519a02d63ed7526703e18620e89342c9d7736ad Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 16:19:16 +0300 Subject: [PATCH] [#168] refs: Implement binary/JSON encoders/decoders on Version Signed-off-by: Leonard Lyubich --- pkg/version.go | 32 ++++++++++++++++++++++++++++++++ pkg/version_test.go | 26 ++++++++++++++++++++++++++ v2/refs/json.go | 20 ++++++++++++++++++++ v2/refs/json_test.go | 12 ++++++++++++ v2/refs/marshal.go | 11 +++++++++++ v2/refs/marshal_test.go | 6 ++---- 6 files changed, 103 insertions(+), 4 deletions(-) diff --git a/pkg/version.go b/pkg/version.go index a1c83165..c72722dd 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -82,3 +82,35 @@ func IsSupportedVersion(v *Version) error { v.GetMinor(), ) } + +// Marshal marshals Version into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (v *Version) Marshal(b ...[]byte) ([]byte, error) { + var buf []byte + if len(b) > 0 { + buf = b[0] + } + + return (*refs.Version)(v). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of Version. +func (v *Version) Unmarshal(data []byte) error { + return (*refs.Version)(v). + Unmarshal(data) +} + +// MarshalJSON encodes Version to protobuf JSON format. +func (v *Version) MarshalJSON() ([]byte, error) { + return (*refs.Version)(v). + MarshalJSON() +} + +// UnmarshalJSON decodes Version from protobuf JSON format. +func (v *Version) UnmarshalJSON(data []byte) error { + return (*refs.Version)(v). + UnmarshalJSON(data) +} diff --git a/pkg/version_test.go b/pkg/version_test.go index a5cc7fa3..ce8b8838 100644 --- a/pkg/version_test.go +++ b/pkg/version_test.go @@ -62,3 +62,29 @@ func TestIsSupportedVersion(t *testing.T) { require.Error(t, IsSupportedVersion(v)) } } + +func TestVersionEncoding(t *testing.T) { + v := NewVersion() + v.SetMajor(1) + v.SetMinor(2) + + t.Run("binary", func(t *testing.T) { + data, err := v.Marshal() + require.NoError(t, err) + + v2 := NewVersion() + require.NoError(t, v2.Unmarshal(data)) + + require.Equal(t, v, v2) + }) + + t.Run("json", func(t *testing.T) { + data, err := v.MarshalJSON() + require.NoError(t, err) + + v2 := NewVersion() + require.NoError(t, v2.UnmarshalJSON(data)) + + require.Equal(t, v, v2) + }) +} diff --git a/v2/refs/json.go b/v2/refs/json.go index e552ae1e..3fe253ea 100644 --- a/v2/refs/json.go +++ b/v2/refs/json.go @@ -84,3 +84,23 @@ func (o *OwnerID) UnmarshalJSON(data []byte) error { return nil } + +func (v *Version) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + VersionToGRPCMessage(v), + ) +} + +func (v *Version) UnmarshalJSON(data []byte) error { + msg := new(refs.Version) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *v = *VersionFromGRPCMessage(msg) + + return nil +} diff --git a/v2/refs/json_test.go b/v2/refs/json_test.go index c5684e91..ae41e9fe 100644 --- a/v2/refs/json_test.go +++ b/v2/refs/json_test.go @@ -57,3 +57,15 @@ func TestOwnerIDJSON(t *testing.T) { require.Equal(t, o, o2) } + +func TestVersionSON(t *testing.T) { + v := generateVersion(1, 2) + + data, err := v.MarshalJSON() + require.NoError(t, err) + + v2 := new(refs.Version) + require.NoError(t, v2.UnmarshalJSON(data)) + + require.Equal(t, v, v2) +} diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index 0a2484a8..353729ca 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -305,3 +305,14 @@ func (v *Version) StableSize() (size int) { return size } + +func (v *Version) Unmarshal(data []byte) error { + m := new(refs.Version) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *v = *VersionFromGRPCMessage(m) + + return nil +} diff --git a/v2/refs/marshal_test.go b/v2/refs/marshal_test.go index 4c12ea2b..f92e3612 100644 --- a/v2/refs/marshal_test.go +++ b/v2/refs/marshal_test.go @@ -111,16 +111,14 @@ func TestSignature_StableMarshal(t *testing.T) { func TestVersion_StableMarshal(t *testing.T) { versionFrom := generateVersion(2, 0) - transport := new(grpc.Version) t.Run("non empty", func(t *testing.T) { wire, err := versionFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + versionTo := new(refs.Version) + require.NoError(t, versionTo.Unmarshal(wire)) - versionTo := refs.VersionFromGRPCMessage(transport) require.Equal(t, versionFrom, versionTo) }) }