Represent -1 as "off" for SIZE values

This commit is contained in:
Nick Craig-Wood 2016-06-03 21:51:39 +01:00
parent 5723d788a4
commit e818b7c206
2 changed files with 10 additions and 0 deletions

View File

@ -102,6 +102,8 @@ func (x SizeSuffix) String() string {
scaled := float64(0) scaled := float64(0)
suffix := "" suffix := ""
switch { switch {
case x < 0:
return "off"
case x == 0: case x == 0:
return "0" return "0"
case x < 1024: case x < 1024:
@ -128,6 +130,10 @@ func (x *SizeSuffix) Set(s string) error {
if len(s) == 0 { if len(s) == 0 {
return fmt.Errorf("Empty string") return fmt.Errorf("Empty string")
} }
if strings.ToLower(s) == "off" {
*x = -1
return nil
}
suffix := s[len(s)-1] suffix := s[len(s)-1]
suffixLen := 1 suffixLen := 1
var multiplier float64 var multiplier float64

View File

@ -18,6 +18,8 @@ func TestSizeSuffixString(t *testing.T) {
{1024 * 1024 * 1024, "1G"}, {1024 * 1024 * 1024, "1G"},
{10 * 1024 * 1024 * 1024, "10G"}, {10 * 1024 * 1024 * 1024, "10G"},
{10.1 * 1024 * 1024 * 1024, "10.100G"}, {10.1 * 1024 * 1024 * 1024, "10.100G"},
{-1, "off"},
{-100, "off"},
} { } {
ss := SizeSuffix(test.in) ss := SizeSuffix(test.in)
got := ss.String() got := ss.String()
@ -44,6 +46,8 @@ func TestSizeSuffixSet(t *testing.T) {
{"1M", 1024 * 1024, false}, {"1M", 1024 * 1024, false},
{"1.g", 1024 * 1024 * 1024, false}, {"1.g", 1024 * 1024 * 1024, false},
{"10G", 10 * 1024 * 1024 * 1024, false}, {"10G", 10 * 1024 * 1024 * 1024, false},
{"off", -1, false},
{"OFF", -1, false},
{"", 0, true}, {"", 0, true},
{"1p", 0, true}, {"1p", 0, true},
{"1.p", 0, true}, {"1.p", 0, true},