1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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:
Michael Crosby 2014-01-17 13:41:38 -08:00 committed by Tianon Gravi
parent ee93f6185b
commit 4ffc52385c

View file

@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
)
@ -39,28 +38,25 @@ func parseLine(line string, v ...interface{}) {
break
}
t := reflect.TypeOf(v[i])
if t.Kind() != reflect.Ptr {
switch e := v[i].(type) {
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("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
}
}
}