Add parsing of string for sizes

Checks the suffix of the string to create the size of the int64
This commit is contained in:
John Hooks
2022-01-11 22:13:20 -05:00
parent d10c306585
commit 12f8179fd8
2 changed files with 59 additions and 3 deletions

View File

@@ -1691,12 +1691,40 @@ func getInterfaceValue(v interface{}) (int64, error) {
return 0, fmt.Errorf("must be int64 or string")
}
i, err := strconv.Atoi(v.(string))
num, err := stringStorageSize(v.(string))
if err != nil {
return 0, fmt.Errorf("must represent an int64")
return 0, err
}
return int64(i), nil
return int64(num), nil
}
func stringStorageSize(s string) (int, error) {
suffix := strings.ToUpper(s[len(s)-1:])
prefix := s[:len(s)-1]
var num int
var err error
switch suffix {
case "K":
num, err = strconv.Atoi(prefix)
num = num * 1024
case "M":
num, err = strconv.Atoi(prefix)
num = num * 1048576
case "G":
num, err = strconv.Atoi(prefix)
num = num * 1073741824
case "T":
num, err = strconv.Atoi(prefix)
num = num * 1099511627776
default:
return 0, fmt.Errorf("sizes defined as strings must end in K, M, G, T")
}
if err != nil {
return 0, err
}
return num, nil
}
// Parse enablement of jetstream for a server.

View File

@@ -3256,3 +3256,31 @@ func TestMaxSubTokens(t *testing.T) {
t.Fatal("Did not get the permissions error")
}
}
func TestStringStoreSize(t *testing.T) {
tt := []struct {
input string
want int64
err bool
}{
{"1K", 1024, false},
{"1M", 1048576, false},
{"1G", 1073741824, false},
{"1T", 1099511627776, false},
{"1L", 0, true},
{"TT", 0, true},
}
for _, v := range tt {
var testErr bool
got, err := getInterfaceValue(v.input)
if err != nil {
testErr = true
}
if got != v.want || v.err != testErr {
t.Errorf("Got: %v, want %v with error: %v", got, v.want, testErr)
}
}
}