ソースを参照

Use type switch instead of reflection

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
Michael Crosby 11 年 前
コミット
4ffc52385c
1 ファイル変更12 行追加16 行削除
  1. 12 16
      pkg/user/user.go

+ 12 - 16
pkg/user/user.go

@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"io"
 	"os"
-	"reflect"
 	"strconv"
 	"strings"
 )
@@ -39,27 +38,24 @@ func parseLine(line string, v ...interface{}) {
 			break
 		}
 
-		t := reflect.TypeOf(v[i])
-		if t.Kind() != reflect.Ptr {
-			// panic, because this is a programming/logic error, not a runtime one
-			panic("parseLine expects only pointers!  argument " + strconv.Itoa(i) + " is not a pointer!")
-		}
-
-		switch t.Elem().Kind() {
-		case reflect.String:
+		switch e := v[i].(type) {
+		case *string:
 			// "root", "adm", "/bin/bash"
-			*v[i].(*string) = p
-		case reflect.Int:
+			*e = p
+		case *int:
 			// "0", "4", "1000"
-			*v[i].(*int), _ = strconv.Atoi(p)
 			// ignore string to int conversion errors, for great "tolerance" of naughty configuration files
-		case reflect.Slice, reflect.Array:
+			*e, _ = strconv.Atoi(p)
+		case *[]string:
 			// "", "root", "root,adm,daemon"
-			list := []string{}
 			if p != "" {
-				list = strings.Split(p, ",")
+				*e = strings.Split(p, ",")
+			} else {
+				*e = []string{}
 			}
-			*v[i].(*[]string) = list
+		default:
+			// panic, because this is a programming/logic error, not a runtime one
+			panic("parseLine expects only pointers!  argument " + strconv.Itoa(i) + " is not a pointer!")
 		}
 	}
 }