From 89a7a946dcd500b01b772a2f418c8adf0ea9a5bd Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 3 Nov 2020 11:29:41 +0300 Subject: [PATCH] [#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 --- pkg/version.go | 17 +++++++++++++++++ pkg/version_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pkg/version.go b/pkg/version.go index 6ffc54c..a1c8316 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neofs-api-go/v2/refs" + "github.com/pkg/errors" ) // Version represents v2-compatible version. @@ -65,3 +66,19 @@ func (v *Version) ToV2() *refs.Version { func (v *Version) String() string { 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(), + ) +} diff --git a/pkg/version_test.go b/pkg/version_test.go index 18e842a..a5cc7fa 100644 --- a/pkg/version_test.go +++ b/pkg/version_test.go @@ -29,3 +29,36 @@ func TestSDKVersion(t *testing.T) { require.Equal(t, uint32(sdkMjr), v.GetMajor()) 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)) + } +}