[#44] version: move package from neofs-api-go

Also remove `Version` from constructors and rename `SDKVersion` to
`Current`.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-10-27 10:41:05 +03:00 committed by Alex Vanin
parent 1ac6b819c5
commit ecc3d0fcf1
2 changed files with 179 additions and 0 deletions

89
version/version.go Normal file
View file

@ -0,0 +1,89 @@
package version
import (
"fmt"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
)
// Version represents v2-compatible version.
type Version refs.Version
const sdkMjr, sdkMnr = 2, 10
// NewFromV2 wraps v2 Version message to Version.
//
// Nil refs.Version converts to nil.
func NewFromV2(v *refs.Version) *Version {
return (*Version)(v)
}
// New creates and initializes blank Version.
//
// Defaults:
// - major: 0;
// - minor: 0.
func New() *Version {
return NewFromV2(new(refs.Version))
}
// Current returns Version instance that
// initialized to current SDK revision number.
func Current() *Version {
v := New()
v.SetMajor(sdkMjr)
v.SetMinor(sdkMnr)
return v
}
// Major returns major number of the revision.
func (v *Version) Major() uint32 {
return (*refs.Version)(v).GetMajor()
}
// SetMajor sets major number of the revision.
func (v *Version) SetMajor(val uint32) {
(*refs.Version)(v).SetMajor(val)
}
// Minor returns minor number of the revision.
func (v *Version) Minor() uint32 {
return (*refs.Version)(v).GetMinor()
}
// SetMinor sets minor number of the revision.
func (v *Version) SetMinor(val uint32) {
(*refs.Version)(v).SetMinor(val)
}
// ToV2 converts Version to v2 Version message.
//
// Nil Version converts to nil.
func (v *Version) ToV2() *refs.Version {
return (*refs.Version)(v)
}
func (v *Version) String() string {
return fmt.Sprintf("v%d.%d", v.Major(), v.Minor())
}
// Marshal marshals Version into a protobuf binary form.
func (v *Version) Marshal() ([]byte, error) {
return (*refs.Version)(v).StableMarshal(nil)
}
// 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)
}

90
version/version_test.go Normal file
View file

@ -0,0 +1,90 @@
package version
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require"
)
func TestNewVersion(t *testing.T) {
t.Run("default values", func(t *testing.T) {
v := New()
// check initial values
require.Zero(t, v.Major())
require.Zero(t, v.Minor())
// convert to v2 message
vV2 := v.ToV2()
require.Empty(t, vV2.GetMajor())
require.Empty(t, vV2.GetMinor())
})
t.Run("setting values", func(t *testing.T) {
v := New()
var mjr, mnr uint32 = 1, 2
v.SetMajor(mjr)
v.SetMinor(mnr)
require.Equal(t, mjr, v.Major())
require.Equal(t, mnr, v.Minor())
ver := v.ToV2()
require.Equal(t, mjr, ver.GetMajor())
require.Equal(t, mnr, ver.GetMinor())
})
}
func TestSDKVersion(t *testing.T) {
v := Current()
require.Equal(t, uint32(sdkMjr), v.Major())
require.Equal(t, uint32(sdkMnr), v.Minor())
}
func TestVersionEncoding(t *testing.T) {
v := New()
v.SetMajor(1)
v.SetMinor(2)
t.Run("binary", func(t *testing.T) {
data, err := v.Marshal()
require.NoError(t, err)
v2 := New()
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 := New()
require.NoError(t, v2.UnmarshalJSON(data))
require.Equal(t, v, v2)
})
}
func TestNewVersionFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *refs.Version
require.Nil(t, NewFromV2(x))
})
}
func TestVersion_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Version
require.Nil(t, x.ToV2())
})
}