You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/05/18 12:08:56 UTC

[GitHub] cbickel closed pull request #3668: Fixes asymmetry between reading and writing of ByteSize.

cbickel closed pull request #3668: Fixes asymmetry between reading and writing of ByteSize.
URL: https://github.com/apache/incubator-openwhisk/pull/3668
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 8273418ce0..61e27b4faf 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 16bca03c97..250cec0479 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 {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services