Browse code

use less reflection in command line method invocation

Roger Peppe authored on 2013/09/23 09:06:31
Showing 3 changed files
... ...
@@ -56,13 +56,13 @@ type APIContainers struct {
56 56
 
57 57
 func (self *APIContainers) ToLegacy() APIContainersOld {
58 58
 	return APIContainersOld{
59
-		ID: self.ID,
60
-		Image: self.Image,
61
-		Command: self.Command,
62
-		Created: self.Created,
63
-		Status: self.Status,
64
-		Ports: displayablePorts(self.Ports),
65
-		SizeRw: self.SizeRw,
59
+		ID:         self.ID,
60
+		Image:      self.Image,
61
+		Command:    self.Command,
62
+		Created:    self.Created,
63
+		Status:     self.Status,
64
+		Ports:      displayablePorts(self.Ports),
65
+		SizeRw:     self.SizeRw,
66 66
 		SizeRootFs: self.SizeRootFs,
67 67
 	}
68 68
 }
... ...
@@ -566,7 +566,6 @@ func TestPostCommit(t *testing.T) {
566 566
 
567 567
 	srv := &Server{runtime: runtime}
568 568
 
569
-
570 569
 	// Create a container and remove a file
571 570
 	container, err := runtime.Create(
572 571
 		&Config{
... ...
@@ -36,9 +36,13 @@ var (
36 36
 	VERSION   string
37 37
 )
38 38
 
39
-func (cli *DockerCli) getMethod(name string) (reflect.Method, bool) {
39
+func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
40 40
 	methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
41
-	return reflect.TypeOf(cli).MethodByName(methodName)
41
+	method := reflect.ValueOf(cli).MethodByName(methodName)
42
+	if !method.IsValid() {
43
+		return nil, false
44
+	}
45
+	return method.Interface().(func(...string) error), true
42 46
 }
43 47
 
44 48
 func ParseCommands(proto, addr string, args ...string) error {
... ...
@@ -50,14 +54,7 @@ func ParseCommands(proto, addr string, args ...string) error {
50 50
 			fmt.Println("Error: Command not found:", args[0])
51 51
 			return cli.CmdHelp(args[1:]...)
52 52
 		}
53
-		ret := method.Func.CallSlice([]reflect.Value{
54
-			reflect.ValueOf(cli),
55
-			reflect.ValueOf(args[1:]),
56
-		})[0].Interface()
57
-		if ret == nil {
58
-			return nil
59
-		}
60
-		return ret.(error)
53
+		return method(args[1:]...)
61 54
 	}
62 55
 	return cli.CmdHelp(args...)
63 56
 }
... ...
@@ -68,10 +65,7 @@ func (cli *DockerCli) CmdHelp(args ...string) error {
68 68
 		if !exists {
69 69
 			fmt.Fprintf(cli.err, "Error: Command not found: %s\n", args[0])
70 70
 		} else {
71
-			method.Func.CallSlice([]reflect.Value{
72
-				reflect.ValueOf(cli),
73
-				reflect.ValueOf([]string{"--help"}),
74
-			})[0].Interface()
71
+			method("--help")
75 72
 			return nil
76 73
 		}
77 74
 	}