[ARVADOS] updated: 1.1.4-527-gd128fe6bc

Git user git at public.curoverse.com
Fri Jun 29 16:22:18 EDT 2018


Summary of changes:
 sdk/go/arvados/byte_size.go      | 34 +++++++++++++++++++++++++++-------
 sdk/go/arvados/byte_size_test.go | 15 +++++++++++++--
 2 files changed, 40 insertions(+), 9 deletions(-)

       via  d128fe6bc8da01fe8e7829db988819dd1c159298 (commit)
      from  11ab2e23511c7e8962e0110c3aad44b74fea2dbd (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit d128fe6bc8da01fe8e7829db988819dd1c159298
Author: Tom Clegg <tclegg at veritasgenetics.com>
Date:   Fri Jun 29 16:21:52 2018 -0400

    13569: Accept all JSON numbers (decimals, exponents), not just ints.
    
    Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg at veritasgenetics.com>

diff --git a/sdk/go/arvados/byte_size.go b/sdk/go/arvados/byte_size.go
index db26a4cca..08cc83e12 100644
--- a/sdk/go/arvados/byte_size.go
+++ b/sdk/go/arvados/byte_size.go
@@ -7,6 +7,7 @@ package arvados
 import (
 	"encoding/json"
 	"fmt"
+	"math"
 	"strings"
 )
 
@@ -43,12 +44,21 @@ func (n *ByteSize) UnmarshalJSON(data []byte) error {
 	if err != nil {
 		return err
 	}
-	split := strings.LastIndexAny(s, "0123456789") + 1
+	split := strings.LastIndexAny(s, "0123456789.+-eE") + 1
 	if split == 0 {
 		return fmt.Errorf("invalid byte size %q", s)
 	}
-	var val int64
-	err = json.Unmarshal([]byte(s[:split]), &val)
+	if s[split-1] == 'E' {
+		// We accepted an E as if it started the exponent part
+		// of a json number, but if the next char isn't +, -,
+		// or digit, then the E must have meant Exa. Instead
+		// of "4.5E"+"iB" we want "4.5"+"EiB".
+		split--
+	}
+	var val json.Number
+	dec := json.NewDecoder(strings.NewReader(s[:split]))
+	dec.UseNumber()
+	err = dec.Decode(&val)
 	if err != nil {
 		return err
 	}
@@ -63,9 +73,19 @@ func (n *ByteSize) UnmarshalJSON(data []byte) error {
 	if !ok {
 		return fmt.Errorf("invalid unit %q", strings.Trim(s[split:], " "))
 	}
-	if pval > 1 && (val*pval)/pval != val {
-		return fmt.Errorf("size %q overflows int64", s)
+	if intval, err := val.Int64(); err == nil {
+		if pval > 1 && (intval*pval)/pval != intval {
+			return fmt.Errorf("size %q overflows int64", s)
+		}
+		*n = ByteSize(intval * pval)
+		return nil
+	} else if floatval, err := val.Float64(); err == nil {
+		if floatval*float64(pval) > math.MaxInt64 {
+			return fmt.Errorf("size %q overflows int64", s)
+		}
+		*n = ByteSize(int64(floatval * float64(pval)))
+		return nil
+	} else {
+		return fmt.Errorf("bug: json.Number for %q is not int64 or float64: %s", s, err)
 	}
-	*n = ByteSize(val * pval)
-	return nil
 }
diff --git a/sdk/go/arvados/byte_size_test.go b/sdk/go/arvados/byte_size_test.go
index 857ec7b00..7c4aff207 100644
--- a/sdk/go/arvados/byte_size_test.go
+++ b/sdk/go/arvados/byte_size_test.go
@@ -40,20 +40,31 @@ func (s *ByteSizeSuite) TestUnmarshal(c *check.C) {
 		{"4PiB", 4503599627370496},
 		{"4EB", 4000000000000000000},
 		{"4EiB", 4611686018427387904},
+		{"4.5EiB", 5188146770730811392},
+		{"1.5 GB", 1500000000},
+		{"1.5 GiB", 1610612736},
+		{"1.234 GiB", 1324997410}, // rounds down from 1324997410.816
+		{"1e2 KB", 100000},
+		{"20E-1 KiB", 2048},
+		{"1E0EB", 1000000000000000000},
+		{"1E-1EB", 100000000000000000},
+		{"1E-1EiB", 115292150460684704},
+		{"4.5E15 K", 4500000000000000000},
 	} {
 		var n ByteSize
 		err := yaml.Unmarshal([]byte(testcase.in+"\n"), &n)
+		c.Logf("%v => %v: %v", testcase.in, testcase.out, n)
 		c.Check(err, check.IsNil)
 		c.Check(int64(n), check.Equals, testcase.out)
 	}
 	for _, testcase := range []string{
 		"B", "K", "KB", "KiB", "4BK", "4iB", "4A", "b", "4b", "4mB", "4m", "4mib", "4KIB", "4K iB", "4Ki B", "BB", "4BB",
 		"400000 EB", // overflows int64
+		"4.11e4 EB", // ok as float64, but overflows int64
 	} {
 		var n ByteSize
 		err := yaml.Unmarshal([]byte(testcase+"\n"), &n)
-		c.Log(n)
-		c.Log(err)
+		c.Logf("%v => error: %v", n, err)
 		c.Check(err, check.NotNil)
 	}
 }

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list