forked from TrueCloudLab/frostfs-api-go
[#140] sdk: Refactor version type
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
524280a5e8
commit
e0c34a51f2
7 changed files with 102 additions and 32 deletions
|
@ -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:
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
31
pkg/version_test.go
Normal file
31
pkg/version_test.go
Normal file
|
@ -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())
|
||||
}
|
Loading…
Reference in a new issue