mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-17 03:24:40 -07:00
Add parsing of string for sizes
Checks the suffix of the string to create the size of the int64
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user