You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by cb...@apache.org on 2018/05/18 12:09:00 UTC

[incubator-openwhisk] branch master updated: Fixes asymmetry between reading and writing of ByteSize. (#3668)

This is an automated email from the ASF dual-hosted git repository.

cbickel pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 7082e94  Fixes asymmetry between reading and writing of ByteSize. (#3668)
7082e94 is described below

commit 7082e947cedafe35bac90df7f54fbce98596d853
Author: rodric rabbah <ro...@gmail.com>
AuthorDate: Fri May 18 08:08:54 2018 -0400

    Fixes asymmetry between reading and writing of ByteSize. (#3668)
    
    Constructing a ByteSize form String will accept MB or M, KB or K (and is case insenstive).
    This is to match the toString method which writes out the units as MB or KB (so that a serialized value can be parsed back to the identity).
---
 .../src/main/scala/whisk/core/entity/Size.scala    | 25 ++++++++++-------
 .../scala/whisk/core/entity/test/SizeTests.scala   | 32 +++++++++++++++++-----
 2 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/core/entity/Size.scala b/common/scala/src/main/scala/whisk/core/entity/Size.scala
index 8273418..61e27b4 100644
--- a/common/scala/src/main/scala/whisk/core/entity/Size.scala
+++ b/common/scala/src/main/scala/whisk/core/entity/Size.scala
@@ -79,18 +79,23 @@ case class ByteSize(size: Long, unit: SizeUnits.Unit) extends Ordered[ByteSize]
 }
 
 object ByteSize {
+  private val regex = """(?i)\s?(\d+)\s?(MB|KB|B|M|K)\s?""".r.pattern
+  protected[entity] val formatError = """Size Unit not supported. Only "B", "K[B]" and "M[B]" are supported."""
+
   def fromString(sizeString: String): ByteSize = {
-    val unitprefix = sizeString.takeRight(1).toUpperCase
-    val size = sizeString.dropRight(1).trim.toLong
-
-    val unit = unitprefix match {
-      case "B" => SizeUnits.BYTE
-      case "K" => SizeUnits.KB
-      case "M" => SizeUnits.MB
-      case _   => throw new IllegalArgumentException("""Size Unit not supported. Only "B", "K" and "M" are supported.""")
-    }
+    val matcher = regex.matcher(sizeString)
+    if (matcher.matches()) {
+      val size = matcher.group(1).toInt
+      val unit = matcher.group(2).charAt(0).toUpper match {
+        case 'B' => SizeUnits.BYTE
+        case 'K' => SizeUnits.KB
+        case 'M' => SizeUnits.MB
+      }
 
-    ByteSize(size, unit)
+      ByteSize(size, unit)
+    } else {
+      throw new IllegalArgumentException(formatError)
+    }
   }
 }
 
diff --git a/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala b/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala
index 16bca03..250cec0 100644
--- a/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala
+++ b/tests/src/test/scala/whisk/core/entity/test/SizeTests.scala
@@ -119,24 +119,42 @@ class SizeTests extends FlatSpec with Matchers {
 
   // Create ObjectSize from String
   it should "create ObjectSize from String 3B" in {
-    val fromString = ByteSize.fromString("3B")
-    fromString equals (3 B)
+    ByteSize.fromString("3b") equals (3 B)
+    ByteSize.fromString("3B") equals (3 B)
+    ByteSize.fromString("3 b") equals (3 B)
+    ByteSize.fromString("3 B") equals (3 B)
   }
 
   it should "create ObjectSize from String 7K" in {
-    val fromString = ByteSize.fromString("7K")
-    fromString equals (7 KB)
+    ByteSize.fromString("7k") equals (7 KB)
+    ByteSize.fromString("7K") equals (7 KB)
+    ByteSize.fromString("7KB") equals (7 KB)
+    ByteSize.fromString("7kB") equals (7 KB)
+    ByteSize.fromString("7kb") equals (7 KB)
+    ByteSize.fromString("7 k") equals (7 KB)
+    ByteSize.fromString("7 K") equals (7 KB)
+    ByteSize.fromString("7 KB") equals (7 KB)
+    ByteSize.fromString("7 kB") equals (7 KB)
+    ByteSize.fromString("7 kb") equals (7 KB)
   }
 
   it should "create ObjectSize from String 120M" in {
-    val fromString = ByteSize.fromString("120M")
-    fromString equals (120 MB)
+    ByteSize.fromString("120m") equals (120 MB)
+    ByteSize.fromString("120M") equals (120 MB)
+    ByteSize.fromString("120MB") equals (120 MB)
+    ByteSize.fromString("120mB") equals (120 MB)
+    ByteSize.fromString("120mb") equals (120 MB)
+    ByteSize.fromString("120 m") equals (120 MB)
+    ByteSize.fromString("120 M") equals (120 MB)
+    ByteSize.fromString("120 MB") equals (120 MB)
+    ByteSize.fromString("120 mB") equals (120 MB)
+    ByteSize.fromString("120 mb") equals (120 MB)
   }
 
   it should "throw error on creating ObjectSize from String 120A" in {
     the[IllegalArgumentException] thrownBy {
       ByteSize.fromString("120A")
-    } should have message """Size Unit not supported. Only "B", "K" and "M" are supported."""
+    } should have message ByteSize.formatError
   }
 
   it should "throw error on creating ByteSize object with negative size" in {

-- 
To stop receiving notification emails like this one, please contact
cbickel@apache.org.