From d19e5418da42dafa2b8cdf5ac6d0966ecaab348e Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 25 Sep 2020 15:51:34 +0300 Subject: [PATCH] [#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 --- pkg/client/object.go | 48 +++++++--------------------------------- pkg/object/range.go | 47 +++++++++++++++++++++++++++++++++++++++ pkg/object/range_test.go | 25 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 40 deletions(-) create mode 100644 pkg/object/range.go create mode 100644 pkg/object/range_test.go diff --git a/pkg/client/object.go b/pkg/client/object.go index 8bcedb2..04724d1 100644 --- a/pkg/client/object.go +++ b/pkg/client/object.go @@ -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 } diff --git a/pkg/object/range.go b/pkg/object/range.go new file mode 100644 index 0000000..901bd3f --- /dev/null +++ b/pkg/object/range.go @@ -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) +} diff --git a/pkg/object/range_test.go b/pkg/object/range_test.go new file mode 100644 index 0000000..4cce11d --- /dev/null +++ b/pkg/object/range_test.go @@ -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()) +}