[#159] sdk: Define object range in object pkg

Define Range type in object package. Replace Range in client package with
the new one.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-25 15:51:34 +03:00 committed by Alex Vanin
parent 14fa89b819
commit d19e5418da
3 changed files with 80 additions and 40 deletions

View file

@ -19,10 +19,6 @@ import (
"github.com/pkg/errors"
)
type Range struct {
ln, off uint64
}
type PutObjectParams struct {
obj *object.Object
@ -48,7 +44,7 @@ type ObjectHeaderParams struct {
type RangeDataParams struct {
addr *object.Address
r *Range
r *object.Range
w io.Writer
}
@ -58,7 +54,7 @@ type RangeChecksumParams struct {
addr *object.Address
rs []*Range
rs []*object.Range
salt []byte
}
@ -93,11 +89,11 @@ const TZSize = 64
const searchQueryVersion uint32 = 1
func rangesToV2(rs []*Range) []*v2object.Range {
func rangesToV2(rs []*object.Range) []*v2object.Range {
r2 := make([]*v2object.Range, 0, len(rs))
for i := range rs {
r2 = append(r2, rs[i].toV2())
r2 = append(r2, rs[i].ToV2())
}
return r2
@ -114,34 +110,6 @@ func (t checksumType) toV2() v2refs.ChecksumType {
}
}
func (r *Range) WithLength(v uint64) *Range {
if r != nil {
r.ln = v
}
return r
}
func (r *Range) WithOffset(v uint64) *Range {
if r != nil {
r.off = v
}
return r
}
func (r *Range) toV2() *v2object.Range {
if r != nil {
r2 := new(v2object.Range)
r2.SetOffset(r.off)
r2.SetLength(r.ln)
return r2
}
return nil
}
func (w *putObjectV2Writer) Write(p []byte) (int, error) {
w.chunkPart.SetChunk(p)
@ -620,7 +588,7 @@ func (p *RangeDataParams) WithAddress(v *object.Address) *RangeDataParams {
return p
}
func (p *RangeDataParams) WithRange(v *Range) *RangeDataParams {
func (p *RangeDataParams) WithRange(v *object.Range) *RangeDataParams {
if p != nil {
p.r = v
}
@ -680,7 +648,7 @@ func (c *Client) objectPayloadRangeV2(ctx context.Context, p *RangeDataParams, o
// fill body fields
body.SetAddress(p.addr.ToV2())
body.SetRange(p.r.toV2())
body.SetRange(p.r.ToV2())
// sign the request
if err := signature.SignServiceMessage(c.key, req); err != nil {
@ -695,7 +663,7 @@ func (c *Client) objectPayloadRangeV2(ctx context.Context, p *RangeDataParams, o
var payload []byte
if p.w != nil {
payload = make([]byte, p.r.ln)
payload = make([]byte, p.r.GetLength())
}
for {
@ -736,7 +704,7 @@ func (p *RangeChecksumParams) WithAddress(v *object.Address) *RangeChecksumParam
return p
}
func (p *RangeChecksumParams) WithRangeList(rs ...*Range) *RangeChecksumParams {
func (p *RangeChecksumParams) WithRangeList(rs ...*object.Range) *RangeChecksumParams {
if p != nil {
p.rs = rs
}

47
pkg/object/range.go Normal file
View file

@ -0,0 +1,47 @@
package object
import (
"github.com/nspcc-dev/neofs-api-go/v2/object"
)
// Range represents v2-compatible object payload range.
type Range object.Range
// NewRangeFromV2 wraps v2 Range message to Range.
func NewRangeFromV2(rV2 *object.Range) *Range {
return (*Range)(rV2)
}
// NewRange creates and initializes blank Range.
func NewRange() *Range {
return NewRangeFromV2(new(object.Range))
}
// ToV2 converts Range to v2 Range message.
func (r *Range) ToV2() *object.Range {
return (*object.Range)(r)
}
// GetLength returns payload range size.
func (r *Range) GetLength() uint64 {
return (*object.Range)(r).
GetLength()
}
// SetLength sets payload range size.
func (r *Range) SetLength(v uint64) {
(*object.Range)(r).
SetLength(v)
}
// GetOffset sets payload range offset from start.
func (r *Range) GetOffset() uint64 {
return (*object.Range)(r).
GetOffset()
}
// SetOffset gets payload range offset from start.
func (r *Range) SetOffset(v uint64) {
(*object.Range)(r).
SetOffset(v)
}

25
pkg/object/range_test.go Normal file
View file

@ -0,0 +1,25 @@
package object
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestRange_SetOffset(t *testing.T) {
r := NewRange()
off := uint64(13)
r.SetOffset(off)
require.Equal(t, off, r.GetOffset())
}
func TestRange_SetLength(t *testing.T) {
r := NewRange()
ln := uint64(7)
r.SetLength(ln)
require.Equal(t, ln, r.GetLength())
}