Skip to content

Commit 5e76f74

Browse files
crosbymichaeltianon
authored andcommitted
Use type switch instead of reflection
Docker-DCO-1.1-Signed-off-by: Michael Crosby <[email protected]> (github: crosbymichael)
1 parent 9331e29 commit 5e76f74

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

user/user.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"os"
8-
"reflect"
98
"strconv"
109
"strings"
1110
)
@@ -39,27 +38,24 @@ func parseLine(line string, v ...interface{}) {
3938
break
4039
}
4140

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:
41+
switch e := v[i].(type) {
42+
case *string:
5043
// "root", "adm", "/bin/bash"
51-
*v[i].(*string) = p
52-
case reflect.Int:
44+
*e = p
45+
case *int:
5346
// "0", "4", "1000"
54-
*v[i].(*int), _ = strconv.Atoi(p)
5547
// ignore string to int conversion errors, for great "tolerance" of naughty configuration files
56-
case reflect.Slice, reflect.Array:
48+
*e, _ = strconv.Atoi(p)
49+
case *[]string:
5750
// "", "root", "root,adm,daemon"
58-
list := []string{}
5951
if p != "" {
60-
list = strings.Split(p, ",")
52+
*e = strings.Split(p, ",")
53+
} else {
54+
*e = []string{}
6155
}
62-
*v[i].(*[]string) = list
56+
default:
57+
// panic, because this is a programming/logic error, not a runtime one
58+
panic("parseLine expects only pointers! argument " + strconv.Itoa(i) + " is not a pointer!")
6359
}
6460
}
6561
}

0 commit comments

Comments
 (0)