added some helper functions
This commit is contained in:
parent
ffe10b11a3
commit
b7f6d544ec
@ -16,6 +16,15 @@ package sizes
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnfit = errors.New("cannot fit into integer")
|
||||
)
|
||||
|
||||
func (b BiByteUnit) AsByte() ByteUnit {
|
||||
return ByteUnit(b)
|
||||
}
|
||||
@ -28,6 +37,27 @@ func (b BiByteUnit) AsBiByte() BiByteUnit {
|
||||
return b
|
||||
}
|
||||
|
||||
func (b BiByteUnit) Int() (int, error) {
|
||||
if int64(b) > math.MaxInt || int64(b) < math.MinInt {
|
||||
return 0, ErrUnfit
|
||||
}
|
||||
|
||||
return int(b), nil
|
||||
}
|
||||
|
||||
func (b BiByteUnit) MustInt() int {
|
||||
var (
|
||||
result int
|
||||
err error
|
||||
)
|
||||
|
||||
if result, err = b.Int(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (b BiByteUnit) Kibi() float64 {
|
||||
return float64(b / KibiByte)
|
||||
}
|
||||
|
||||
@ -16,6 +16,8 @@ package sizes
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import "math"
|
||||
|
||||
func (b ByteUnit) AsByte() ByteUnit {
|
||||
return b
|
||||
}
|
||||
@ -28,6 +30,27 @@ func (b ByteUnit) AsBiByte() BiByteUnit {
|
||||
return BiByteUnit(b)
|
||||
}
|
||||
|
||||
func (b ByteUnit) Int() (int, error) {
|
||||
if int64(b) > math.MaxInt || int64(b) < math.MinInt {
|
||||
return 0, ErrUnfit
|
||||
}
|
||||
|
||||
return int(b), nil
|
||||
}
|
||||
|
||||
func (b ByteUnit) MustInt() int {
|
||||
var (
|
||||
result int
|
||||
err error
|
||||
)
|
||||
|
||||
if result, err = b.Int(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (b ByteUnit) Kilo() float64 {
|
||||
return float64(b / KiloByte)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user