Skip to content

Commit 54b53b7

Browse files
committed
Add memory size parser
1 parent 5be916e commit 54b53b7

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

core/util/memory_size.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
// MemorySize is a custom type for parsing memory sizes (e.g., "128KB", "2MB")
10+
type MemorySize int64
11+
12+
// String returns the string representation of the memory size
13+
func (m *MemorySize) String() string {
14+
return fmt.Sprintf("%d", *m)
15+
}
16+
17+
// ToBytes returns the memory size as an int64 in bytes.
18+
func (m *MemorySize) ToBytes() int64 {
19+
return int64(*m)
20+
}
21+
22+
// Set parses a string like "128KB" or "2MB" and converts it to bytes
23+
func (m *MemorySize) Set(value string) error {
24+
multiplier := int64(1)
25+
26+
value = strings.ToUpper(strings.TrimSpace(value))
27+
28+
switch {
29+
case strings.HasSuffix(value, "KB"):
30+
multiplier = 1024
31+
value = strings.TrimSuffix(value, "KB")
32+
case strings.HasSuffix(value, "MB"):
33+
multiplier = 1024 * 1024
34+
value = strings.TrimSuffix(value, "MB")
35+
case strings.HasSuffix(value, "GB"):
36+
multiplier = 1024 * 1024 * 1024
37+
value = strings.TrimSuffix(value, "GB")
38+
}
39+
40+
size, err := strconv.ParseInt(value, 10, 64)
41+
if err != nil || size < 0 {
42+
return fmt.Errorf("invalid memory size: %s", value)
43+
}
44+
45+
*m = MemorySize(size * multiplier)
46+
return nil
47+
}
48+

core/util/memory_size_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package util
2+
3+
import "testing"
4+
import . "github.com/onsi/gomega"
5+
6+
func TestMemorySize_Set(t *testing.T) {
7+
RegisterTestingT(t) // Register Gomega for the test
8+
9+
t.Run("valid inputs", func(t *testing.T) {
10+
var ms MemorySize
11+
12+
// Test inputs with valid values
13+
err := ms.Set("128KB")
14+
Expect(err).To(BeNil())
15+
Expect(ms).To(Equal(MemorySize(128 * 1024)))
16+
17+
err = ms.Set("2MB")
18+
Expect(err).To(BeNil())
19+
Expect(ms).To(Equal(MemorySize(2 * 1024 * 1024)))
20+
21+
err = ms.Set("1GB")
22+
Expect(err).To(BeNil())
23+
Expect(ms).To(Equal(MemorySize(1 * 1024 * 1024 * 1024)))
24+
25+
err = ms.Set("1024") // No suffix, treat as bytes
26+
Expect(err).To(BeNil())
27+
Expect(ms).To(Equal(MemorySize(1024)))
28+
29+
err = ms.Set(" 64MB ") // Test with leading/trailing spaces
30+
Expect(err).To(BeNil())
31+
Expect(ms).To(Equal(MemorySize(64 * 1024 * 1024)))
32+
})
33+
34+
t.Run("invalid inputs", func(t *testing.T) {
35+
var ms MemorySize
36+
37+
// Test inputs with invalid values
38+
Expect(ms.Set("10XYZ")).To(Not(BeNil())) // Unknown unit
39+
Expect(ms.Set("ABC")).To(Not(BeNil())) // Non-numeric input
40+
Expect(ms.Set("")).To(Not(BeNil())) // Empty input
41+
Expect(ms.Set("-5MB")).To(Not(BeNil())) // Negative value
42+
})
43+
44+
t.Run("boundary cases", func(t *testing.T) {
45+
var ms MemorySize
46+
47+
// Test extremely large numbers
48+
err := ms.Set("1099511627776GB") // 1 PB (petabyte, very large)
49+
Expect(err).To(BeNil())
50+
51+
// Overflow handling (in practice, you'd want to handle overflow explicitly)
52+
err = ms.Set("9223372036854775808") // Larger than int64 max
53+
Expect(err).To(Not(BeNil()))
54+
})
55+
}
56+
57+
func TestMemorySize_AsBytes(t *testing.T) {
58+
RegisterTestingT(t) // Register Gomega for the test
59+
60+
var ms MemorySize
61+
62+
// Set a value and check its string representation
63+
ms = 128 * 1024
64+
Expect(ms.ToBytes()).To(Equal(int64(131072)))
65+
66+
ms = 2 * 1024 * 1024
67+
Expect(ms.ToBytes()).To(Equal(int64(2097152)))
68+
}

0 commit comments

Comments
 (0)