]> source.dussan.org Git - gitea.git/commitdiff
Use type switch (#5122)
authorOleg Kovalov <iamolegkovalov@gmail.com>
Fri, 19 Oct 2018 18:54:26 +0000 (20:54 +0200)
committertechknowlogick <hello@techknowlogick.com>
Fri, 19 Oct 2018 18:54:26 +0000 (14:54 -0400)
modules/base/tool.go
modules/base/tool_test.go

index d5ec9e83fcbea6a8b04a96ebdebe50b292661285..5b79a844ab352a7791f3af5f52601befc82c09e9 100644 (file)
@@ -441,41 +441,41 @@ func Subtract(left interface{}, right interface{}) interface{} {
        var rleft, rright int64
        var fleft, fright float64
        var isInt = true
-       switch left.(type) {
+       switch left := left.(type) {
        case int:
-               rleft = int64(left.(int))
+               rleft = int64(left)
        case int8:
-               rleft = int64(left.(int8))
+               rleft = int64(left)
        case int16:
-               rleft = int64(left.(int16))
+               rleft = int64(left)
        case int32:
-               rleft = int64(left.(int32))
+               rleft = int64(left)
        case int64:
-               rleft = left.(int64)
+               rleft = left
        case float32:
-               fleft = float64(left.(float32))
+               fleft = float64(left)
                isInt = false
        case float64:
-               fleft = left.(float64)
+               fleft = left
                isInt = false
        }
 
-       switch right.(type) {
+       switch right := right.(type) {
        case int:
-               rright = int64(right.(int))
+               rright = int64(right)
        case int8:
-               rright = int64(right.(int8))
+               rright = int64(right)
        case int16:
-               rright = int64(right.(int16))
+               rright = int64(right)
        case int32:
-               rright = int64(right.(int32))
+               rright = int64(right)
        case int64:
-               rright = right.(int64)
+               rright = right
        case float32:
-               fright = float64(right.(float32))
+               fright = float64(right)
                isInt = false
        case float64:
-               fright = right.(float64)
+               fright = right
                isInt = false
        }
 
index f99edd5fbfee0a7fc83d179a4391f67a96369fe9..04cd68290718760b52798cbccf491f4234ddae24 100644 (file)
@@ -299,21 +299,21 @@ func TestFileSize(t *testing.T) {
 
 func TestSubtract(t *testing.T) {
        toFloat64 := func(n interface{}) float64 {
-               switch n.(type) {
+               switch n := n.(type) {
                case int:
-                       return float64(n.(int))
+                       return float64(n)
                case int8:
-                       return float64(n.(int8))
+                       return float64(n)
                case int16:
-                       return float64(n.(int16))
+                       return float64(n)
                case int32:
-                       return float64(n.(int32))
+                       return float64(n)
                case int64:
-                       return float64(n.(int64))
+                       return float64(n)
                case float32:
-                       return float64(n.(float32))
+                       return float64(n)
                case float64:
-                       return n.(float64)
+                       return n
                default:
                        return 0.0
                }