|
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 | 38 | break |
40 | 39 | } |
41 | 40 |
|
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: |
50 | 43 | // "root", "adm", "/bin/bash" |
51 | | - *v[i].(*string) = p |
52 | | - case reflect.Int: |
| 44 | + *e = p |
| 45 | + case *int: |
53 | 46 | // "0", "4", "1000" |
54 | | - *v[i].(*int), _ = strconv.Atoi(p) |
55 | 47 | // 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: |
57 | 50 | // "", "root", "root,adm,daemon" |
58 | | - list := []string{} |
59 | 51 | if p != "" { |
60 | | - list = strings.Split(p, ",") |
| 52 | + *e = strings.Split(p, ",") |
| 53 | + } else { |
| 54 | + *e = []string{} |
61 | 55 | } |
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!") |
63 | 59 | } |
64 | 60 | } |
65 | 61 | } |
|
0 commit comments