Skip to content

Commit 4475901

Browse files
committed
return uint64 for parseSize
Signed-off-by: allencloud <allen.sun@daocloud.io>
1 parent f2d77a6 commit 4475901

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

size.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,34 @@ func BytesSize(size float64) string {
6363

6464
// FromHumanSize returns an integer from a human-readable specification of a
6565
// size using SI standard (eg. "44kB", "17MB").
66-
func FromHumanSize(size string) (int64, error) {
66+
func FromHumanSize(size string) (uint64, error) {
6767
return parseSize(size, decimalMap)
6868
}
6969

7070
// RAMInBytes parses a human-readable string representing an amount of RAM
7171
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
72-
// returns the number of bytes, or -1 if the string is unparseable.
72+
// returns the number of bytes.
7373
// Units are case-insensitive, and the 'b' suffix is optional.
74-
func RAMInBytes(size string) (int64, error) {
74+
func RAMInBytes(size string) (uint64, error) {
7575
return parseSize(size, binaryMap)
7676
}
7777

7878
// Parses the human-readable size string into the amount it represents.
79-
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
79+
func parseSize(sizeStr string, uMap unitMap) (uint64, error) {
8080
matches := sizeRegex.FindStringSubmatch(sizeStr)
8181
if len(matches) != 4 {
82-
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
82+
return 0, fmt.Errorf("invalid size: '%s'", sizeStr)
8383
}
8484

8585
size, err := strconv.ParseFloat(matches[1], 64)
8686
if err != nil {
87-
return -1, err
87+
return 0, err
8888
}
8989

9090
unitPrefix := strings.ToLower(matches[3])
9191
if mul, ok := uMap[unitPrefix]; ok {
9292
size *= float64(mul)
9393
}
9494

95-
return int64(size), nil
95+
return uint64(size), nil
9696
}

size_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ func assertEquals(t *testing.T, expected, actual interface{}) {
142142
}
143143

144144
// func that maps to the parse function signatures as testing abstraction
145-
type parseFn func(string) (int64, error)
145+
type parseFn func(string) (uint64, error)
146146

147147
// Define 'String()' for pretty-print
148148
func (fn parseFn) String() string {
149149
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
150150
return fnName[strings.LastIndex(fnName, ".")+1:]
151151
}
152152

153-
func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
153+
func assertSuccessEquals(t *testing.T, expected uint64, fn parseFn, arg string) {
154154
res, err := fn(arg)
155155
if err != nil || res != expected {
156156
t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)

0 commit comments

Comments
 (0)