service: get rid of bytefmt

- add ByteSize type + Stringer
- add test coverage
- cleanup modules

closes #22
This commit is contained in:
Evgeniy Kulikov 2019-11-26 15:48:55 +03:00
parent 999fcb5927
commit ac44e4bb9f
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2
4 changed files with 84 additions and 24 deletions

View file

@ -2,12 +2,43 @@ package object
import (
"io"
"strconv"
"code.cloudfoundry.org/bytefmt"
"github.com/nspcc-dev/neofs-proto/session"
"github.com/pkg/errors"
)
// ByteSize used to format bytes
type ByteSize uint64
// String represents ByteSize in string format
func (b ByteSize) String() string {
var (
dec int64
unit string
num = int64(b)
)
switch {
case num > UnitsTB:
unit = "TB"
dec = UnitsTB
case num > UnitsGB:
unit = "GB"
dec = UnitsGB
case num > UnitsMB:
unit = "MB"
dec = UnitsMB
case num > UnitsKB:
unit = "KB"
dec = UnitsKB
case num > UnitsB:
dec = 1
}
return strconv.FormatFloat(float64(num)/float64(dec), 'g', 6, 64) + unit
}
// MakePutRequestHeader combines object and session token value
// into header of object put request.
func MakePutRequestHeader(obj *Object, token *session.Token) *PutRequest {
@ -26,7 +57,7 @@ func MakePutRequestChunk(chunk []byte) *PutRequest {
}
func errMaxSizeExceeded(size uint64) error {
return errors.Errorf("object payload size exceed: %s", bytefmt.ByteSize(size))
return errors.Errorf("object payload size exceed: %s", ByteSize(size).String())
}
// ReceiveGetResponse receives object by chunks from the protobuf stream