Browse code

Merge pull request #1963 from rogpeppe/master

Client: Use less reflection in command-line method invocation

Victor Vieux authored on 2013/10/25 10:03:20
Showing 2 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
 }
... ...
@@ -41,9 +41,13 @@ var (
41 41
 	ErrConnectionRefused = errors.New("Can't connect to docker daemon. Is 'docker -d' running on this host?")
42 42
 )
43 43
 
44
-func (cli *DockerCli) getMethod(name string) (reflect.Method, bool) {
44
+func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
45 45
 	methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
46
-	return reflect.TypeOf(cli).MethodByName(methodName)
46
+	method := reflect.ValueOf(cli).MethodByName(methodName)
47
+	if !method.IsValid() {
48
+		return nil, false
49
+	}
50
+	return method.Interface().(func(...string) error), true
47 51
 }
48 52
 
49 53
 func ParseCommands(proto, addr string, args ...string) error {
... ...
@@ -55,14 +59,7 @@ func ParseCommands(proto, addr string, args ...string) error {
55 55
 			fmt.Println("Error: Command not found:", args[0])
56 56
 			return cli.CmdHelp(args[1:]...)
57 57
 		}
58
-		ret := method.Func.CallSlice([]reflect.Value{
59
-			reflect.ValueOf(cli),
60
-			reflect.ValueOf(args[1:]),
61
-		})[0].Interface()
62
-		if ret == nil {
63
-			return nil
64
-		}
65
-		return ret.(error)
58
+		return method(args[1:]...)
66 59
 	}
67 60
 	return cli.CmdHelp(args...)
68 61
 }
... ...
@@ -73,10 +70,7 @@ func (cli *DockerCli) CmdHelp(args ...string) error {
73 73
 		if !exists {
74 74
 			fmt.Fprintf(cli.err, "Error: Command not found: %s\n", args[0])
75 75
 		} else {
76
-			method.Func.CallSlice([]reflect.Value{
77
-				reflect.ValueOf(cli),
78
-				reflect.ValueOf([]string{"--help"}),
79
-			})[0].Interface()
76
+			method("--help")
80 77
 			return nil
81 78
 		}
82 79
 	}