Skip to content

Commit 058e16e

Browse files
im-kulikovvishr
authored andcommitted
[FIX] Cleanup code (#16)
* [FIX] Cleanup code - cleanup code - fix bytes package * work around bytes package and tests * Fix #12
1 parent 8cfb965 commit 058e16e

File tree

5 files changed

+107
-47
lines changed

5 files changed

+107
-47
lines changed

bytes/bytes.go

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
)
88

99
type (
10-
Bytes struct {
11-
}
10+
// Bytes struct
11+
Bytes struct{}
1212
)
1313

1414
const (
15-
B = 1 << (10 * iota)
15+
_ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier
1616
KB
1717
MB
1818
GB
@@ -22,7 +22,7 @@ const (
2222
)
2323

2424
var (
25-
pattern = regexp.MustCompile(`(?i)^(-?\d+)([KMGTP]B?|B)$`)
25+
pattern = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)([KMGTPE]B?|B?)$`)
2626
global = New()
2727
)
2828

@@ -38,29 +38,31 @@ func (*Bytes) Format(b int64) string {
3838
value := float64(b)
3939

4040
switch {
41-
case b < KB:
42-
return strconv.FormatInt(b, 10) + "B"
43-
case b < MB:
44-
value /= KB
45-
multiple = "KB"
46-
case b < MB:
47-
value /= KB
48-
multiple = "KB"
49-
case b < GB:
50-
value /= MB
51-
multiple = "MB"
52-
case b < TB:
53-
value /= GB
54-
multiple = "GB"
55-
case b < PB:
56-
value /= TB
57-
multiple = "TB"
58-
case b < EB:
41+
case b >= EB:
42+
value /= EB
43+
multiple = "EB"
44+
case b >= PB:
5945
value /= PB
6046
multiple = "PB"
47+
case b >= TB:
48+
value /= TB
49+
multiple = "TB"
50+
case b >= GB:
51+
value /= GB
52+
multiple = "GB"
53+
case b >= MB:
54+
value /= MB
55+
multiple = "MB"
56+
case b >= KB:
57+
value /= KB
58+
multiple = "KB"
59+
case b == 0:
60+
return "0"
61+
default:
62+
return strconv.FormatInt(b, 10) + "B"
6163
}
6264

63-
return fmt.Sprintf("%.02f%s", value, multiple)
65+
return fmt.Sprintf("%.2f%s", value, multiple)
6466
}
6567

6668
// Parse parses human readable bytes string to bytes integer.
@@ -72,27 +74,27 @@ func (*Bytes) Parse(value string) (i int64, err error) {
7274
}
7375
bytesString := parts[1]
7476
multiple := parts[2]
75-
bytes, err := strconv.ParseInt(bytesString, 10, 64)
77+
bytes, err := strconv.ParseFloat(bytesString, 64)
7678
if err != nil {
7779
return
7880
}
7981

8082
switch multiple {
81-
case "B":
82-
return bytes * B, nil
83+
default:
84+
return int64(bytes), nil
8385
case "K", "KB":
84-
return bytes * KB, nil
86+
return int64(bytes * KB), nil
8587
case "M", "MB":
86-
return bytes * MB, nil
88+
return int64(bytes * MB), nil
8789
case "G", "GB":
88-
return bytes * GB, nil
90+
return int64(bytes * GB), nil
8991
case "T", "TB":
90-
return bytes * TB, nil
92+
return int64(bytes * TB), nil
9193
case "P", "PB":
92-
return bytes * PB, nil
94+
return int64(bytes * PB), nil
95+
case "E", "EB":
96+
return int64(bytes * EB), nil
9397
}
94-
95-
return
9698
}
9799

98100
// Format wraps global Bytes's Format function.

bytes/bytes_test.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package bytes
22

33
import (
4+
"math"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
)
89

910
func TestBytesFormat(t *testing.T) {
1011
// B
11-
b := Format(515)
12+
b := Format(0)
13+
assert.Equal(t, "0", b)
14+
// B
15+
b = Format(515)
1216
assert.Equal(t, "515B", b)
1317

1418
// KB
@@ -30,16 +34,63 @@ func TestBytesFormat(t *testing.T) {
3034
// PB
3135
b = Format(9923232398434432)
3236
assert.Equal(t, "8.81PB", b)
37+
38+
// EB
39+
b = Format(math.MaxInt64)
40+
assert.Equal(t, "8.00EB", b)
41+
}
42+
43+
func TestBytesParseErrors(t *testing.T) {
44+
_, err := Parse("B999")
45+
if assert.Error(t, err) {
46+
assert.EqualError(t, err, "error parsing value=B999")
47+
}
48+
}
49+
50+
func TestFloats(t *testing.T) {
51+
// From string:
52+
str := "12.25KB"
53+
value, err := Parse(str)
54+
assert.NoError(t, err)
55+
assert.Equal(t, int64(12544), value)
56+
57+
str2 := Format(value)
58+
assert.Equal(t, str, str2)
59+
60+
// To string:
61+
val := int64(13233029)
62+
str = Format(val)
63+
assert.Equal(t, "12.62MB", str)
64+
65+
val2, err := Parse(str)
66+
assert.NoError(t, err)
67+
assert.Equal(t, val, val2)
3368
}
3469

3570
func TestBytesParse(t *testing.T) {
3671
// B
37-
b, err := Parse("515B")
72+
b, err := Parse("999")
73+
if assert.NoError(t, err) {
74+
assert.Equal(t, int64(999), b)
75+
}
76+
b, err = Parse("-100")
77+
if assert.NoError(t, err) {
78+
assert.Equal(t, int64(-100), b)
79+
}
80+
b, err = Parse("100.1")
81+
if assert.NoError(t, err) {
82+
assert.Equal(t, int64(100), b)
83+
}
84+
b, err = Parse("515B")
3885
if assert.NoError(t, err) {
3986
assert.Equal(t, int64(515), b)
4087
}
4188

4289
// KB
90+
b, err = Parse("12.25KB")
91+
if assert.NoError(t, err) {
92+
assert.Equal(t, int64(12544), b)
93+
}
4394
b, err = Parse("12KB")
4495
if assert.NoError(t, err) {
4596
assert.Equal(t, int64(12288), b)
@@ -88,4 +139,14 @@ func TestBytesParse(t *testing.T) {
88139
if assert.NoError(t, err) {
89140
assert.Equal(t, int64(10133099161583616), b)
90141
}
142+
143+
// EB
144+
b, err = Parse("8EB")
145+
if assert.NoError(t, err) {
146+
assert.True(t, math.MaxInt64 == b-1)
147+
}
148+
b, err = Parse("8E")
149+
if assert.NoError(t, err) {
150+
assert.True(t, math.MaxInt64 == b-1)
151+
}
91152
}

email/email.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"bytes"
55
"crypto/tls"
66
"html/template"
7+
"net"
78
"net/mail"
89
"net/smtp"
9-
10-
"net"
11-
1210
"time"
1311

1412
"github.com/labstack/gommon/random"

log/log.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import (
88
"os"
99
"path"
1010
"runtime"
11+
"strconv"
1112
"sync"
1213
"time"
1314

14-
"strconv"
15-
1615
"github.com/mattn/go-isatty"
1716
"github.com/valyala/fasttemplate"
1817

random/random.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ type (
1313

1414
// Charsets
1515
const (
16-
Uppercase string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
17-
Lowercase = "abcdefghijklmnopqrstuvwxyz"
18-
Alphabetic = Uppercase + Lowercase
19-
Numeric = "0123456789"
20-
Alphanumeric = Alphabetic + Numeric
21-
Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
22-
Hex = Numeric + "abcdef"
16+
Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
17+
Lowercase = "abcdefghijklmnopqrstuvwxyz"
18+
Alphabetic = Uppercase + Lowercase
19+
Numeric = "0123456789"
20+
Alphanumeric = Alphabetic + Numeric
21+
Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
22+
Hex = Numeric + "abcdef"
2323
)
2424

2525
var (

0 commit comments

Comments
 (0)