From 181b9badddf3571da395509d152eb244d87b6883 Mon Sep 17 00:00:00 2001 From: Roger Peppe Date: Mon, 23 Sep 2013 01:06:31 +0100 Subject: [PATCH] use less reflection in command line method invocation --- api_params.go | 14 +++++++------- api_test.go | 1 - commands.go | 22 ++++++++-------------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/api_params.go b/api_params.go index 6403bc6a26..5f1a338057 100644 --- a/api_params.go +++ b/api_params.go @@ -56,13 +56,13 @@ type APIContainers struct { func (self *APIContainers) ToLegacy() APIContainersOld { return APIContainersOld{ - ID: self.ID, - Image: self.Image, - Command: self.Command, - Created: self.Created, - Status: self.Status, - Ports: displayablePorts(self.Ports), - SizeRw: self.SizeRw, + ID: self.ID, + Image: self.Image, + Command: self.Command, + Created: self.Created, + Status: self.Status, + Ports: displayablePorts(self.Ports), + SizeRw: self.SizeRw, SizeRootFs: self.SizeRootFs, } } diff --git a/api_test.go b/api_test.go index d24cf7cfda..0144513c05 100644 --- a/api_test.go +++ b/api_test.go @@ -566,7 +566,6 @@ func TestPostCommit(t *testing.T) { srv := &Server{runtime: runtime} - // Create a container and remove a file container, err := runtime.Create( &Config{ diff --git a/commands.go b/commands.go index df33dfc306..5ce21b4e28 100644 --- a/commands.go +++ b/commands.go @@ -36,9 +36,13 @@ var ( VERSION string ) -func (cli *DockerCli) getMethod(name string) (reflect.Method, bool) { +func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) { methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:]) - return reflect.TypeOf(cli).MethodByName(methodName) + method := reflect.ValueOf(cli).MethodByName(methodName) + if !method.IsValid() { + return nil, false + } + return method.Interface().(func(...string) error), true } func ParseCommands(proto, addr string, args ...string) error { @@ -50,14 +54,7 @@ func ParseCommands(proto, addr string, args ...string) error { fmt.Println("Error: Command not found:", args[0]) return cli.CmdHelp(args[1:]...) } - ret := method.Func.CallSlice([]reflect.Value{ - reflect.ValueOf(cli), - reflect.ValueOf(args[1:]), - })[0].Interface() - if ret == nil { - return nil - } - return ret.(error) + return method(args[1:]...) } return cli.CmdHelp(args...) } @@ -68,10 +65,7 @@ func (cli *DockerCli) CmdHelp(args ...string) error { if !exists { fmt.Fprintf(cli.err, "Error: Command not found: %s\n", args[0]) } else { - method.Func.CallSlice([]reflect.Value{ - reflect.ValueOf(cli), - reflect.ValueOf([]string{"--help"}), - })[0].Interface() + method("--help") return nil } }