[#187] sdk: Implement IsSupportedVersion function

Implement function that checks the compatibility of the specified version
with the current NeoFS revision.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-03 11:29:41 +03:00 committed by Alex Vanin
parent 352e99d9b9
commit 89a7a946dc
2 changed files with 50 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/pkg/errors"
) )
// Version represents v2-compatible version. // Version represents v2-compatible version.
@ -65,3 +66,19 @@ func (v *Version) ToV2() *refs.Version {
func (v *Version) String() string { func (v *Version) String() string {
return fmt.Sprintf("v%d.%d", v.GetMajor(), v.GetMinor()) return fmt.Sprintf("v%d.%d", v.GetMajor(), v.GetMinor())
} }
// IsSupportedVersion returns error if v is not supported by current SDK.
func IsSupportedVersion(v *Version) error {
switch mjr := v.GetMajor(); mjr {
case 2:
switch mnr := v.GetMinor(); mnr {
case 0:
return nil
}
}
return errors.Errorf("unsupported version %d.%d",
v.GetMajor(),
v.GetMinor(),
)
}

View file

@ -29,3 +29,36 @@ func TestSDKVersion(t *testing.T) {
require.Equal(t, uint32(sdkMjr), v.GetMajor()) require.Equal(t, uint32(sdkMjr), v.GetMajor())
require.Equal(t, uint32(sdkMnr), v.GetMinor()) require.Equal(t, uint32(sdkMnr), v.GetMinor())
} }
func TestIsSupportedVersion(t *testing.T) {
require.Error(t, IsSupportedVersion(nil))
v := NewVersion()
v.SetMajor(1)
require.Error(t, IsSupportedVersion(v))
v.SetMajor(3)
require.Error(t, IsSupportedVersion(v))
for _, item := range []struct {
mjr, maxMnr uint32
}{
{
mjr: 2,
maxMnr: 0,
},
} {
v.SetMajor(item.mjr)
for i := uint32(0); i < item.maxMnr; i++ {
v.SetMinor(i)
require.NoError(t, IsSupportedVersion(v))
}
v.SetMinor(item.maxMnr + 1)
require.Error(t, IsSupportedVersion(v))
}
}