Procházet zdrojové kódy

pkg/units: Using 'case' instead of trickled ifs

Seems the perfect case for 'case' ;).

Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)
Francisco Carriedo před 11 roky
rodič
revize
03697f0a93
1 změnil soubory, kde provedl 11 přidání a 9 odebrání
  1. 11 9
      pkg/units/size.go

+ 11 - 9
pkg/units/size.go

@@ -42,15 +42,16 @@ func FromHumanSize(size string) (int64, error) {
 
 	unit := strings.ToLower(matches[2])
 
-	if unit == "k" {
+	switch unit {
+	case "k":
 		theSize *= 1000
-	} else if unit == "m" {
+	case "m":
 		theSize *= 1000 * 1000
-	} else if unit == "g" {
+	case "g":
 		theSize *= 1000 * 1000 * 1000
-	} else if unit == "t" {
+	case "t":
 		theSize *= 1000 * 1000 * 1000 * 1000
-	} else if unit == "p" {
+	case "p":
 		theSize *= 1000 * 1000 * 1000 * 1000 * 1000
 	}
 
@@ -80,13 +81,14 @@ func RAMInBytes(size string) (int64, error) {
 
 	unit := strings.ToLower(matches[2])
 
-	if unit == "k" {
+	switch unit {
+	case "k":
 		memLimit *= 1024
-	} else if unit == "m" {
+	case "m":
 		memLimit *= 1024 * 1024
-	} else if unit == "g" {
+	case "g":
 		memLimit *= 1024 * 1024 * 1024
-	} else if unit == "t" {
+	case "t":
 		memLimit *= 1024 * 1024 * 1024 * 1024
 	}