Use type switch instead of reflection
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
ac97c2a2f1
commit
91ac5f5f60
1 changed files with 16 additions and 20 deletions
36
user/user.go
36
user/user.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -39,28 +38,25 @@ func parseLine(line string, v ...interface{}) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
t := reflect.TypeOf(v[i])
|
switch e := v[i].(type) {
|
||||||
if t.Kind() != reflect.Ptr {
|
case *string:
|
||||||
|
// "root", "adm", "/bin/bash"
|
||||||
|
*e = p
|
||||||
|
case *int:
|
||||||
|
// "0", "4", "1000"
|
||||||
|
// ignore string to int conversion errors, for great "tolerance" of naughty configuration files
|
||||||
|
*e, _ = strconv.Atoi(p)
|
||||||
|
case *[]string:
|
||||||
|
// "", "root", "root,adm,daemon"
|
||||||
|
if p != "" {
|
||||||
|
*e = strings.Split(p, ",")
|
||||||
|
} else {
|
||||||
|
*e = []string{}
|
||||||
|
}
|
||||||
|
default:
|
||||||
// panic, because this is a programming/logic error, not a runtime one
|
// 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!")
|
panic("parseLine expects only pointers! argument " + strconv.Itoa(i) + " is not a pointer!")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch t.Elem().Kind() {
|
|
||||||
case reflect.String:
|
|
||||||
// "root", "adm", "/bin/bash"
|
|
||||||
*v[i].(*string) = p
|
|
||||||
case reflect.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:
|
|
||||||
// "", "root", "root,adm,daemon"
|
|
||||||
list := []string{}
|
|
||||||
if p != "" {
|
|
||||||
list = strings.Split(p, ",")
|
|
||||||
}
|
|
||||||
*v[i].(*[]string) = list
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue