Browse code

Use type switch instead of reflection

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/01/18 06:41:38
Showing 1 changed files
... ...
@@ -5,7 +5,6 @@ import (
5 5
 	"fmt"
6 6
 	"io"
7 7
 	"os"
8
-	"reflect"
9 8
 	"strconv"
10 9
 	"strings"
11 10
 )
... ...
@@ -39,27 +38,24 @@ func parseLine(line string, v ...interface{}) {
39 39
 			break
40 40
 		}
41 41
 
42
-		t := reflect.TypeOf(v[i])
43
-		if t.Kind() != reflect.Ptr {
44
-			// panic, because this is a programming/logic error, not a runtime one
45
-			panic("parseLine expects only pointers!  argument " + strconv.Itoa(i) + " is not a pointer!")
46
-		}
47
-
48
-		switch t.Elem().Kind() {
49
-		case reflect.String:
42
+		switch e := v[i].(type) {
43
+		case *string:
50 44
 			// "root", "adm", "/bin/bash"
51
-			*v[i].(*string) = p
52
-		case reflect.Int:
45
+			*e = p
46
+		case *int:
53 47
 			// "0", "4", "1000"
54
-			*v[i].(*int), _ = strconv.Atoi(p)
55 48
 			// ignore string to int conversion errors, for great "tolerance" of naughty configuration files
56
-		case reflect.Slice, reflect.Array:
49
+			*e, _ = strconv.Atoi(p)
50
+		case *[]string:
57 51
 			// "", "root", "root,adm,daemon"
58
-			list := []string{}
59 52
 			if p != "" {
60
-				list = strings.Split(p, ",")
53
+				*e = strings.Split(p, ",")
54
+			} else {
55
+				*e = []string{}
61 56
 			}
62
-			*v[i].(*[]string) = list
57
+		default:
58
+			// panic, because this is a programming/logic error, not a runtime one
59
+			panic("parseLine expects only pointers!  argument " + strconv.Itoa(i) + " is not a pointer!")
63 60
 		}
64 61
 	}
65 62
 }