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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 16:19:16 +03:00 committed by Alex Vanin
parent 9ec57ee9f8
commit 1519a02d63
6 changed files with 103 additions and 4 deletions

View file

@ -82,3 +82,35 @@ func IsSupportedVersion(v *Version) error {
v.GetMinor(), 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)
}

View file

@ -62,3 +62,29 @@ func TestIsSupportedVersion(t *testing.T) {
require.Error(t, IsSupportedVersion(v)) 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)
})
}

View file

@ -84,3 +84,23 @@ func (o *OwnerID) UnmarshalJSON(data []byte) error {
return nil 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
}

View file

@ -57,3 +57,15 @@ func TestOwnerIDJSON(t *testing.T) {
require.Equal(t, o, o2) 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)
}

View file

@ -305,3 +305,14 @@ func (v *Version) StableSize() (size int) {
return size 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
}

View file

@ -111,16 +111,14 @@ func TestSignature_StableMarshal(t *testing.T) {
func TestVersion_StableMarshal(t *testing.T) { func TestVersion_StableMarshal(t *testing.T) {
versionFrom := generateVersion(2, 0) versionFrom := generateVersion(2, 0)
transport := new(grpc.Version)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
wire, err := versionFrom.StableMarshal(nil) wire, err := versionFrom.StableMarshal(nil)
require.NoError(t, err) require.NoError(t, err)
err = goproto.Unmarshal(wire, transport) versionTo := new(refs.Version)
require.NoError(t, err) require.NoError(t, versionTo.Unmarshal(wire))
versionTo := refs.VersionFromGRPCMessage(transport)
require.Equal(t, versionFrom, versionTo) require.Equal(t, versionFrom, versionTo)
}) })
} }