From e0c34a51f2ea030aab6d4a9bf971dc6cfcdf30b1 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 10 Sep 2020 14:29:11 +0300 Subject: [PATCH] [#140] sdk: Refactor version type Signed-off-by: Leonard Lyubich --- pkg/client/accounting.go | 2 +- pkg/client/client.go | 4 +-- pkg/client/container.go | 10 +++--- pkg/client/object.go | 12 +++---- pkg/client/opts.go | 6 ++-- pkg/version.go | 69 +++++++++++++++++++++++++++++++--------- pkg/version_test.go | 31 ++++++++++++++++++ 7 files changed, 102 insertions(+), 32 deletions(-) create mode 100644 pkg/version_test.go diff --git a/pkg/client/accounting.go b/pkg/client/accounting.go index 4642c9d..92c5e12 100644 --- a/pkg/client/accounting.go +++ b/pkg/client/accounting.go @@ -25,7 +25,7 @@ func (c Client) GetSelfBalance(ctx context.Context, opts ...CallOption) (*accoun func (c Client) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.getBalanceV2(ctx, owner, opts...) default: diff --git a/pkg/client/client.go b/pkg/client/client.go index 8187afd..e5e8e3b 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -18,7 +18,7 @@ type ( TransportProtocol uint32 TransportInfo struct { - Version pkg.Version + Version *pkg.Version Protocol TransportProtocol } ) @@ -42,7 +42,7 @@ func New(key *ecdsa.PrivateKey, opts ...ClientOption) (*Client, error) { return &Client{ key: key, remoteNode: TransportInfo{ - Version: pkg.SDKVersion, + Version: pkg.SDKVersion(), Protocol: GRPC, }, opts: clientOptions, diff --git a/pkg/client/container.go b/pkg/client/container.go index a874af8..2888ebc 100644 --- a/pkg/client/container.go +++ b/pkg/client/container.go @@ -26,7 +26,7 @@ func (c delContainerSignWrapper) SignedDataSize() int { } func (c Client) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (*container.ID, error) { - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.putContainerV2(ctx, cnr, opts...) default: @@ -35,7 +35,7 @@ func (c Client) PutContainer(ctx context.Context, cnr *container.Container, opts } func (c Client) GetContainer(ctx context.Context, id *container.ID, opts ...CallOption) (*container.Container, error) { - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.getContainerV2(ctx, id, opts...) default: @@ -44,7 +44,7 @@ func (c Client) GetContainer(ctx context.Context, id *container.ID, opts ...Call } func (c Client) ListContainers(ctx context.Context, owner *owner.ID, opts ...CallOption) ([]*container.ID, error) { - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.listContainerV2(ctx, owner, opts...) default: @@ -65,7 +65,7 @@ func (c Client) ListSelfContainers(ctx context.Context, opts ...CallOption) ([]* } func (c Client) DeleteContainer(ctx context.Context, id *container.ID, opts ...CallOption) error { - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.delContainerV2(ctx, id, opts...) default: @@ -84,7 +84,7 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op } // set transport version - cnr.SetVersion(c.remoteNode.Version.ToV2Version()) + cnr.SetVersion(c.remoteNode.Version.ToV2()) // if container owner is not set, then use client key as owner if cnr.GetOwnerID() == nil { diff --git a/pkg/client/object.go b/pkg/client/object.go index 5ef8e59..e7edcd0 100644 --- a/pkg/client/object.go +++ b/pkg/client/object.go @@ -165,7 +165,7 @@ func (p *PutObjectParams) WithPayloadReader(v io.Reader) *PutObjectParams { func (c *Client) PutObject(ctx context.Context, p *PutObjectParams, opts ...CallOption) (*object.ID, error) { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.putObjectV2(ctx, p, opts...) default: @@ -277,7 +277,7 @@ func (p *DeleteObjectParams) WithAddress(v *object.Address) *DeleteObjectParams func (c *Client) DeleteObject(ctx context.Context, p *DeleteObjectParams, opts ...CallOption) error { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.deleteObjectV2(ctx, p, opts...) default: @@ -350,7 +350,7 @@ func (p *GetObjectParams) WithPayloadWriter(w io.Writer) *GetObjectParams { func (c *Client) GetObject(ctx context.Context, p *GetObjectParams, opts ...CallOption) (*object.Object, error) { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.getObjectV2(ctx, p, opts...) default: @@ -481,7 +481,7 @@ func (p *ObjectHeaderParams) WithMainFields() *ObjectHeaderParams { func (c *Client) GetObjectHeader(ctx context.Context, p *ObjectHeaderParams, opts ...CallOption) (*object.Object, error) { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.getObjectHeaderV2(ctx, p, opts...) default: @@ -609,7 +609,7 @@ func (p *RangeDataParams) WithDataWriter(v io.Writer) *RangeDataParams { func (c *Client) ObjectPayloadRangeData(ctx context.Context, p *RangeDataParams, opts ...CallOption) ([]byte, error) { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.objectPayloadRangeV2(ctx, p, opts...) default: @@ -744,7 +744,7 @@ func (c *Client) ObjectPayloadRangeTZ(ctx context.Context, p *RangeChecksumParam func (c *Client) objectPayloadRangeHash(ctx context.Context, p *RangeChecksumParams, opts ...CallOption) (interface{}, error) { // check remote node version - switch c.remoteNode.Version.Major { + switch c.remoteNode.Version.GetMajor() { case 2: return c.objectPayloadRangeHashV2(ctx, p, opts...) default: diff --git a/pkg/client/opts.go b/pkg/client/opts.go index ba7202c..de37e2c 100644 --- a/pkg/client/opts.go +++ b/pkg/client/opts.go @@ -23,7 +23,7 @@ type ( } callOptions struct { - version pkg.Version + version *pkg.Version xHeaders []xHeader ttl uint32 epoch uint64 @@ -49,7 +49,7 @@ type ( func defaultCallOptions() callOptions { return callOptions{ ttl: 2, - version: pkg.SDKVersion, + version: pkg.SDKVersion(), } } @@ -93,7 +93,7 @@ func WithEpoch(epoch uint64) CallOption { func v2MetaHeaderFromOpts(options callOptions) *v2session.RequestMetaHeader { meta := new(v2session.RequestMetaHeader) - meta.SetVersion(options.version.ToV2Version()) + meta.SetVersion(options.version.ToV2()) meta.SetTTL(options.ttl) meta.SetEpoch(options.epoch) diff --git a/pkg/version.go b/pkg/version.go index 333e597..6ffc54c 100644 --- a/pkg/version.go +++ b/pkg/version.go @@ -6,23 +6,62 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" ) -type ( - Version struct { - Major uint32 - Minor uint32 - } -) +// Version represents v2-compatible version. +type Version refs.Version -var SDKVersion = Version{2, 0} +const sdkMjr, sdkMnr = 2, 0 -func (v Version) String() string { - return fmt.Sprintf("v%d.%d", v.Major, v.Minor) +// NewVersionFromV2 wraps v2 Version message to Version. +func NewVersionFromV2(v *refs.Version) *Version { + return (*Version)(v) } -func (v Version) ToV2Version() *refs.Version { - result := new(refs.Version) - result.SetMajor(v.Major) - result.SetMinor(v.Minor) - - return result +// NewVersion creates and initializes blank Version. +// +// Works similar as NewVersionFromV2(new(Version)). +func NewVersion() *Version { + return NewVersionFromV2(new(refs.Version)) +} + +// SDKVersion returns Version instance that +// initialized to current SDK revision number. +func SDKVersion() *Version { + v := NewVersion() + v.SetMajor(sdkMjr) + v.SetMinor(sdkMnr) + + return v +} + +// GetMajor returns major number of the revision. +func (v *Version) GetMajor() uint32 { + return (*refs.Version)(v). + GetMajor() +} + +// SetMajor sets major number of the revision. +func (v *Version) SetMajor(val uint32) { + (*refs.Version)(v). + SetMajor(val) +} + +// GetMinor returns minor number of the revision. +func (v *Version) GetMinor() 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. +func (v *Version) ToV2() *refs.Version { + return (*refs.Version)(v) +} + +func (v *Version) String() string { + return fmt.Sprintf("v%d.%d", v.GetMajor(), v.GetMinor()) } diff --git a/pkg/version_test.go b/pkg/version_test.go new file mode 100644 index 0000000..18e842a --- /dev/null +++ b/pkg/version_test.go @@ -0,0 +1,31 @@ +package pkg + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewVersionFromV2(t *testing.T) { + v := NewVersion() + + var mjr, mnr uint32 = 1, 2 + + v.SetMajor(mjr) + v.SetMinor(mnr) + + require.Equal(t, mjr, v.GetMajor()) + require.Equal(t, mnr, v.GetMinor()) + + ver := v.ToV2() + + require.Equal(t, mjr, ver.GetMajor()) + require.Equal(t, mnr, ver.GetMinor()) +} + +func TestSDKVersion(t *testing.T) { + v := SDKVersion() + + require.Equal(t, uint32(sdkMjr), v.GetMajor()) + require.Equal(t, uint32(sdkMnr), v.GetMinor()) +}