client: Accept rng object.Range into PrmObjectRange

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
This commit is contained in:
Evgenii Baidakov 2023-05-04 11:28:31 +04:00
parent 626532d7dd
commit 04ea0e8f6a
No known key found for this signature in database
GPG key ID: 8733EE3D72CDB4DE
4 changed files with 87 additions and 8 deletions

View file

@ -485,17 +485,23 @@ type PrmObjectRange struct {
}
// SetOffset sets offset of the payload range to be read.
// Zero by default.
// Zero by default. It is an alternative to [PrmObjectRange.SetRange].
func (x *PrmObjectRange) SetOffset(off uint64) {
x.rng.SetOffset(off)
}
// SetLength sets length of the payload range to be read.
// Must be positive.
// Must be positive. It is an alternative to [PrmObjectRange.SetRange].
func (x *PrmObjectRange) SetLength(ln uint64) {
x.rng.SetLength(ln)
}
// SetRange sets range of the payload to be read.
// It is an alternative to [PrmObjectRange.SetOffset], [PrmObjectRange.SetLength].
func (x *PrmObjectRange) SetRange(rng object.Range) {
x.rng = *rng.ToV2()
}
// UseSigner specifies private signer to sign the requests.
// If signer is not provided, then Client default signer is used.
func (x *PrmObjectRange) UseSigner(signer neofscrypto.Signer) {

View file

@ -2,10 +2,12 @@ package client
import (
"bytes"
"math/rand"
"testing"
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/stretchr/testify/require"
)
@ -48,3 +50,37 @@ func TestPrmObjectRead_ByAddress(t *testing.T) {
require.True(t, bytes.Equal(cidV2.GetValue(), prm.addr.GetContainerID().GetValue()))
})
}
func TestPrmObjectRange_SetRange(t *testing.T) {
var prm PrmObjectRange
var (
ln = rand.Uint64()
off = rand.Uint64()
rng *object.Range
)
t.Run("SetLength", func(t *testing.T) {
prm.SetLength(ln)
rng = object.NewRangeFromV2(&prm.rng)
require.Equal(t, ln, rng.GetLength())
})
t.Run("SetOffset", func(t *testing.T) {
prm.SetOffset(off)
rng = object.NewRangeFromV2(&prm.rng)
require.Equal(t, off, rng.GetOffset())
})
t.Run("SetRange", func(t *testing.T) {
var tmp object.Range
tmp.SetLength(ln)
tmp.SetOffset(off)
prm.SetRange(tmp)
require.Equal(t, ln, tmp.ToV2().GetLength())
require.Equal(t, off, tmp.ToV2().GetOffset())
})
}

View file

@ -829,8 +829,7 @@ func (c *clientWrapper) objectRange(ctx context.Context, prm PrmObjectRange) (Re
var cliPrm sdkClient.PrmObjectRange
cliPrm.ByAddress(prm.addr)
cliPrm.SetOffset(prm.off)
cliPrm.SetLength(prm.ln)
cliPrm.SetRange(prm.rng)
if prm.stoken != nil {
cliPrm.WithinSession(*prm.stoken)
@ -1298,8 +1297,8 @@ func (x *PrmObjectHead) MarkRaw() {
type PrmObjectRange struct {
prmCommon
addr oid.Address
off, ln uint64
addr oid.Address
rng object.Range
}
// SetAddress specifies NeoFS address of the object.
@ -1308,13 +1307,21 @@ func (x *PrmObjectRange) SetAddress(addr oid.Address) {
}
// SetOffset sets offset of the payload range to be read.
// Zero by default. It is an alternative to [PrmObjectRange.SetRange].
func (x *PrmObjectRange) SetOffset(offset uint64) {
x.off = offset
x.rng.SetOffset(offset)
}
// SetLength sets length of the payload range to be read.
// Must be positive. It is an alternative to [PrmObjectRange.SetRange].
func (x *PrmObjectRange) SetLength(length uint64) {
x.ln = length
x.rng.SetLength(length)
}
// SetRange sets range of the payload to be read.
// It is an alternative to [PrmObjectRange.SetOffset], [PrmObjectRange.SetLength].
func (x *PrmObjectRange) SetRange(rng object.Range) {
x.rng = rng
}
// PrmObjectSearch groups parameters of SearchObjects operation.

View file

@ -3,6 +3,7 @@ package pool
import (
"context"
"errors"
"math/rand"
"strconv"
"testing"
"time"
@ -655,3 +656,32 @@ func TestSwitchAfterErrorThreshold(t *testing.T) {
_, err = conn.objectGet(ctx, PrmObjectGet{})
require.NoError(t, err)
}
func TestPrmObjectRange_SetRange(t *testing.T) {
var prm PrmObjectRange
ln := rand.Uint64()
off := rand.Uint64()
t.Run("SetLength", func(t *testing.T) {
prm.SetLength(ln)
require.Equal(t, ln, prm.rng.ToV2().GetLength())
})
t.Run("SetOffset", func(t *testing.T) {
prm.SetOffset(off)
require.Equal(t, off, prm.rng.ToV2().GetOffset())
})
t.Run("SetRange", func(t *testing.T) {
var tmp object.Range
tmp.SetLength(ln)
tmp.SetOffset(off)
prm.SetRange(tmp)
require.Equal(t, ln, tmp.ToV2().GetLength())
require.Equal(t, off, tmp.ToV2().GetOffset())
})
}